def calcAvg(self): table = PluginTable(); row = table.execute('''select count(*) as items_count, sum(ratings) as total_ratings, sum(ratings_count) as total_ratings_count, sum(downloads) as total_downloads from scripts''')[0]; avg_score = row['total_ratings'] / float(row['total_ratings_count']) avg_persons=row['total_ratings_count'] / float(row['items_count']) return {'score':avg_score, 'persons':avg_persons}
def info(): table = PluginTable() row = table.getLast() update_date = row['update_date'] total = table.count() print ''' vim plugins info total: %d, last updated at %s ''' % (total, update_date)
def search(keyword, more=False): table=PluginTable() condition="(name like '%"+keyword+"%'" if more: condition+="or desc like '%"+keyword+"%')" else: condition+=")" rows = table.query(condition) for row in rows: print string.Template("$name \t$score \t$desc").substitute(row)
def detail(name): try: id = int(name) except: id = 0 table = PluginTable() if id > 0: plugin = table.findById(id) else: plugin = table.findByName(name) if plugin: printPlugin(plugin) else: print "Sorry, we cannot find plugin %s for you" % name
def top(sort, year, limit): table=PluginTable() if year != 0: date = datetime.date.fromtimestamp(time.time() - year*365 * 86400).strftime("%Y-%m-%d") condition = "create_date > '"+date+"'" else: condition = '' rows = table.query(condition, sort, limit) template = ("%-5d %-30s %-15s %s") i=0 print "%-5s %-30s %-15s %s" % ("rank", "name", sort, 'desc') for row in rows: i=i+1 print template % (i, row['name'], str(row[sort]), row['desc'])
def fetchSelected(self, idList): model = PluginTable() count = 0 for id in idList: try: data = self.fetchPlugin(int(id)) if not data: print "plugin not exist ", id continue model.save(data) count+=1 print "fetch plugin success ", id except: print "fetch plugin error ", id traceback.print_exc() continue return count
def plugin_query(keyword, type, year, sort, page, size): table = PluginTable() condition='' if len(keyword) > 0: condition="(name like '%"+keyword+"%' or desc like '%"+keyword+"%')" if type != 'all': if len(condition) > 0: condition += ' AND ' condition="type='"+type+"'" if year != 0: if len(condition) > 0: condition += ' AND ' date = datetime.date.fromtimestamp(time.time() - year*365 * 86400).strftime("%Y-%m-%d") condition += "create_date > '"+date+"'" limit = size offset = (page-1) * size rows = table.query(condition, sort, limit, offset) return rows
def calc(self, id=0, data=None): table = PluginTable() table.connect() id = int(id) avg = self.calcAvg() if id > 0: data = table.findById(id) if data: score = self.gradePlugin(data, avg) table.updateScore(id, score) return score rows=table.query(None, None, 0) for row in rows: score = self.gradePlugin(row, avg) table.updateScore(row['id'], score) return len(rows)
def grade(name=None): grader = Grader() if not name: count = grader.calc() print "jobs done, scores of %d plugins updated" % count return try: id = int(name) except: id = 0 table = PluginTable() if id: plugin = table.findById(id) else: plugin = table.findByName(name) if plugin: grader.calc(data=plugin) else: print "Sorry, we cannot find plugin %s" % name
def fetchAll(self, start=1, end=0): if end <= 0: end = self.getTotal() if end < start: return False pool=0 pool_size=20 model = PluginTable() count = end - start for id in xrange(start, end): try: if pool <= pool_size: pid=os.fork() else: time.sleep(pool-pool_size) pid=os.fork() except: print "fork error" traceback.print_exc() os._exit(-1) if pid: #parent pool=pool+1 #print "fork pool=%d ", pid, id while True: result=os.waitpid(-1, os.WNOHANG) if not result[0]: break; pool=pool-1 continue else: #child print "fetch script", id try: data=self.fetchPlugin(id) if data: #print "save script", id model.save(data) os._exit(0) else: print "cannot get script", id os._exit(-2) except: print "error in getting script", id traceback.print_exc() os._exit(-1) time.sleep(3) while True: try: result = os.waitpid(-1, os.WNOHANG) if not result[0]: print "Task finished" break if result[1] < 0: count-=1 except: break; return count
def fetchNew(self): table = PluginTable() last_id, last_date=table.getLast() print last_id, last_date return self.fetchAll(last_id)