Yang Zhang avatar Yang Zhang committed 6a28b39

update API and add more feature for update_comic

Comments (0)

Files changed (5)

 class JsonProxy:
     pass
 
-
-@app.route('/api/json/booklist/<int:listid>')
-@jsonify
-def get_listbook(listid):
-    books = Book.get_jsonify_books(offset = listid-1)
+def make_book_json_proxy(books):
     proxys = []
     for book in books:
         proxy = JsonProxy()
         proxys.append(proxy)
     return {'content':proxys}
 
+@app.route('/api/json/booklist/<int:listid>')
+@jsonify
+def get_listbook(listid):
+    books = Book.get_jsonify_books(offset = listid-1)
+    return make_book_json_proxy(books)
+
 @app.route('/api/json/chapters/<int:bookid>')
 @jsonify
 def get_chapterlist(bookid):
     pages = Chapter.get_jsonify_pages_bychaperid(chapterid)
     return {'content':pages,'length':len(pages)}
 
+@app.route('/api/json/label/<name>')
+@jsonify
+def get_label_booklist(name):
+    try:
+        page = int(request.args.get("page",1))
+    except:
+        abort(404)
+    label = name
+    if(label is None): abort(404)
+    if(label ==u'最近更新'):
+        books = Book.get_jsonify_last_updatedbooks(offset = page -1)
+    else:
+        books = Book.get_jsonify_books_bylabel(label, offset = page -1)
+    return make_book_json_proxy(books)
 
+@app.route('/api/json/search/<keyword>')
+@jsonify
+def get_search_booklist(keyword):
+    if(keyword is None): abort(404)
+    if(len(keyword) < 2): abort(404)
+    books = Book.get_jsonify_books_bykeyword(keyword)
+    return make_book_json_proxy(books)
 '''test'''
 @app.route('/test')
 def test():

dbscript/_init_count.py

 c = con.cursor()
 c.execute("SELECT chapterid from page");
 results = c.fetchall()
-print "first part"
+#print "first part"
 i = 0
 page_dic = {}
 for result in results:
         page_dic[key] = 1
     else:
         page_dic[key] = page_dic[key] + 1
-print "second part"
+#print "second part"
 for key in page_dic:
     c.execute("UPDATE chapter SET counter=:counter WHERE id=:id",\
     {'counter':page_dic[key], 'id':key})
     i +=1
-    if(i % 1000 == 0):
-        print i,":" ,time.time()
 con.commit()
 con.close()
 
 def update():
     'update all comic'
     import update_comic
+    import sqlite3
+    import time
+    con = sqlite3.connect(MAINDB)
+    con.row_factory = sqlite3.Row
+    c = con.cursor()
+    c.execute("SELECT chapterid from page");
+    results = c.fetchall()
+    #print "first part"
+    i = 0
+    page_dic = {}
+    for result in results:
+        key = result['chapterid']
+        if( key not in page_dic):
+            page_dic[key] = 1
+        else:
+            page_dic[key] = page_dic[key] + 1
+    #print "second part"
+    for key in page_dic:
+        c.execute("UPDATE chapter SET counter=:counter WHERE id=:id",\
+        {'counter':page_dic[key], 'id':key})
+        i +=1
+    con.commit()
+    con.close()
+
+    
+    
+    
 
 
 if __name__ == "__main__":
             tempbooks.append(temppyb)
         return tempbooks
 
-        
+
     @staticmethod
     def get_last_updatedbooks(limit = 15, offset = 0):
         c = g.db.cursor()
             temppyb.id = result['id']
             tempbooks.append(temppyb)
         return tempbooks
-        
+
+    @staticmethod
+    def get_jsonify_last_updatedbooks(limit = 15, offset = 0):
+        c = g.db.cursor()
+        c.execute("SELECT id,name ,author,intro,last_chapter FROM book \
+                        WHERE update_date is not null \
+                        ORDER BY update_date DESC LIMIT :limit OFFSET :offset",{'limit':limit,'offset':limit*offset})
+        results = c.fetchall()
+        tempbooks = []
+        if(results is None):
+            return []
+        for result in results:
+            temppyb = Book(result['name'], None,result['author'],result['intro'],None,result['last_chapter'])
+            temppyb.id = result['id']
+            tempbooks.append(temppyb)
+        return tempbooks
+
     @staticmethod
     def get_jsonify_books(limit = 15, offset = 0):
         c = g.db.cursor()
         return tempbooks
 
     @staticmethod
+    def get_jsonify_books_bykeyword(keyword, offset = 0):
+        c = g.db.cursor()
+        c.execute("SELECT id,name ,intro, author,last_chapter FROM book \
+            WHERE name LIKE :keyword OR author LIKE :keyword ORDER BY hot_score DESC ",\
+                {'keyword':'%'+keyword+'%'})
+        results = c.fetchall()
+        tempbooks = []
+        if(results is None):
+            return []
+        for result in results:
+            temppyb = Book(result['name'], None,result['author'],result['intro'],None,result['last_chapter'])
+            temppyb.id = result['id']
+            tempbooks.append(temppyb)
+        return tempbooks
+
+    @staticmethod
     def get_books_bylabel(label,limit = 15, offset = 0):
         c = g.db.cursor()
         c.execute("SELECT b.id AS id,b.name AS name,b.last_chapter AS last_chapter \
             temppyb.id = result['id']
             tempbooks.append(temppyb)
         return tempbooks
+
+    @staticmethod
+    def get_jsonify_books_bylabel(label,limit = 15, offset = 0):
+        c = g.db.cursor()
+        c.execute("SELECT b.id AS id,b.name AS name,b.last_chapter AS last_chapter, \
+                b.author as author, b.intro as intro\
+                FROM book b, label l WHERE l.name=:label AND l.bookid = b.id \
+                ORDER BY hot_score DESC LIMIT :limit OFFSET :offset",{'label':label,'limit':limit,'offset':limit*offset})
+        results = c.fetchall()
+        tempbooks = []
+        if(results is None):
+            return []
+        for result in results:
+            temppyb = Book(result['name'], None,result['author'],result['intro'],None,result['last_chapter'])
+            temppyb.id = result['id']
+            tempbooks.append(temppyb)
+        return tempbooks
+
     @staticmethod
     def get_book_byid(id):
         c = g.db.cursor()
 User-agent: *
 Disallow: /showchapter/
 Disallow: /api/
-Disallow: /admin/
+Disallow: /admin/
+Disallow: /search
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.