from flask.ext.script import Command
from app.packages.models import Package, Downloads, DbFlags
#from app import db
import requests
import datetime
import sys
import traceback
import re
import time

atom_link = "https://atom.io/api/packages?page="



class Load(Command):
    def __init__(self, app, db):
        self.app = app
        self.db = db
        self.start_time = time.time()
        self.no_of_updates = 0
        self.no_of_packages = 0
        self.new_packages = 0
        self.skipped_packages = 0

    def flag_db(self, status):
        entry = DbFlags.query.filter(DbFlags.id == 1).all()
        if not entry:
            # create a default entry
            entry = DbFlags(date=datetime.date.today(), flag=False)
            self.db.session.add(entry)
            self.db.session.commit()

        # update the entry with status and date
        if status is False:
            entry.date = datetime.date.today()
        entry.flag = status
        self.db.session.commit()

    def run(self):
        i = 1
        self.flag_db(True)
        while True:
            req = requests.get(atom_link + repr(i))
            i = i + 1
            if (req.status_code != requests.codes.ok) or (req.json() == []):
                end_time = time.time()
                lapse = end_time - self.start_time
                self.app.logger.info("\nTotal Pages: %d\n"
                    "Total Packages: %d\n"
                    " Updated : %d\n"
                    " New     : %d\n"
                    " Skipped : %d\n"
                    "Run time: %dm %ds\n",
                    i-1, self.no_of_packages, self.no_of_updates, self.new_packages, self.skipped_packages, lapse/60, lapse%60)
                break

            for value in req.json():
                name = value.get('name')
                self.no_of_packages += 1
                if name is not None and type(name) is dict:
                    name = name.get('name')

                meta = value.get('metadata')
                if meta is None:
                    self.skipped_packages += 1
                    continue

                link = meta.get('repository')
                if link is not None and type(link) is dict:
                    link = link.get('url')

                if meta.get('author') is not None:
                    if type(meta.get('author')) is dict:
                        author = meta.get('author')
                        author = author.get('author') or author.get('name')
                    else:
                        author = meta.get('author')
                elif link is not None:
                    m = re.match(r'^.*com?[:\/](?P<name>.*)/', link)
                    if m is None:
                        # no author so skip
                        self.skipped_packages += 1
                        self.app.logger.error("Author: %s", link)
                        continue
                    author = m.groupdict()['name']
                else:
                    print("author error", name)

                down_no = 0 if value.get('downloads') is None else value.get('downloads')

                query = Package.query.filter_by(name=name).first()
                if query is None:
                    self.new_packages += 1
                    query = Package(name=name,
                                    author=author,
                                    link=link,
                                    description=meta.get('description')
                                    )
                else:
                    self.no_of_updates += 1

                self.db.session.add(query)
                downl_model = Downloads(downloads=down_no, package=query)

                self.db.session.add(downl_model)
                self.db.session.commit()

        # we are done updating db, unflag db
        self.flag_db(False)

@@ -89,16 +89,25 @@
  DbFlags
+, Version
 %0A#from a
@@ -201,16 +201,41 @@
 rt time%0A
+import semantic_version%0A%0A
 %0Aatom_li
@@ -278,17 +278,16 @@
 age=%22%0A%0A%0A
-%0A
 class Lo
@@ -299,16 +299,17 @@
 mmand):%0A
+%0A
     def 
@@ -632,11 +632,13 @@
  1).
-all
+first
 ()%0A 
@@ -1021,24 +1021,344 @@
 n.commit()%0A%0A
+    def version_update(self, sem_ver, package):%0A        ver = package.version.order_by(Version.id.desc()).first()%0A        if (ver is None) or (sem_ver %3E semantic_version.Version(ver.number, partial=True)):%0A            ver_model = Version(number=str(sem_ver), package=package)%0A            self.db.session.add(ver_model)%0A%0A
     def run(
@@ -1703,16 +1703,37 @@
 er.info(
+%0A                    
 %22%5CnTotal
@@ -1970,11 +1970,15 @@
     
-i-1
+(i - 1)
 , se
@@ -2015,16 +2015,36 @@
 updates,
+%0A                   
  self.ne
@@ -2082,26 +2082,69 @@
 es, 
+(
 lapse
-/60, lapse%2560
+ / 60),%0A                    lapse %25 60%0A                
 )%0A  
@@ -3495,13 +3495,8 @@
 o = 
-0 if 
 valu
@@ -3493,32 +3493,79 @@
 _no = value.get(
+'downloads') if value.get(%0A                    
 'downloads') is 
@@ -3560,24 +3560,28 @@
 nloads') is 
+not 
 None else va
@@ -3578,37 +3578,203 @@
 ne else 
-value.get('downloads'
+0%0A                ver_no = meta.get('version') if meta.get(%0A                    'version') is not None else '0.0.0'%0A                sem_ver = semantic_version.Version(ver_no, partial=True
 )%0A%0A     
@@ -4351,17 +4351,16 @@
 =query)%0A
-%0A
         
@@ -4400,16 +4400,69 @@
 _model)%0A
+                self.version_update(sem_ver, query)%0A%0A
         

Add version info to db Check based on semver