From c68030ada8a1c7bf0b90be17aa37315d8e2ad89b Mon Sep 17 00:00:00 2001 From: Mike Crute Date: Wed, 4 Jun 2014 13:22:11 -0400 Subject: Clean up a few bugs --- app.py | 36 ++++++++++++----------- templates/items.html | 83 +++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 95 insertions(+), 24 deletions(-) diff --git a/app.py b/app.py index 755d55c..1740f51 100755 --- a/app.py +++ b/app.py @@ -8,6 +8,7 @@ from collections import defaultdict from flask import Flask, render_template, jsonify, request, make_response +DB_PATH = "/home/mcrute/.newsbeuter/cache.db" app = Flask(__name__) @@ -95,15 +96,14 @@ class DBReader(object): ''' def __init__(self, db_path): - self.db_path = db_path + self.con = sqlite3.connect(db_path) + self.con.row_factory = RSSItem.from_db_row - def _get_connection(self): - conn = sqlite3.connect(self.db_path) - conn.row_factory = RSSItem.from_db_row - return conn + def close(self): + return self.con.close() def _fetch(self, where=None, params=()): - with self._get_connection() as con: + with self.con as con: curs = con.cursor() args = { 'where': '', 'where_cond': '' } @@ -114,7 +114,7 @@ class DBReader(object): return curs.fetchall() def update_unread(self, id, unread=True): - with self._get_connection() as con: + with self.con as con: con.execute( 'UPDATE rss_item SET unread = ? WHERE id = ?', [1 if unread else 0, id]) @@ -139,14 +139,14 @@ class DBReader(object): return self._fetch("i.feedurl = ?", [RSSFeed.parse_token(token)]) def get_feeds(self): - with self._get_connection() as con: + with self.con as con: con.row_factory = RSSFeed.from_db_row curs = con.cursor() curs.execute('SELECT rssurl, url, title FROM rss_feed') return curs.fetchall() def get_feed(self, token): - with self._get_connection() as con: + with self.con as con: con.row_factory = RSSFeed.from_db_row curs = con.cursor() curs.execute('SELECT rssurl, url, title FROM rss_feed WHERE rssurl = ?', [RSSFeed.parse_token(token)]) @@ -159,38 +159,40 @@ def json_list(data): @app.route('/') def index(): - reader = DBReader('../../cache.db') + reader = DBReader(DB_PATH) return render_template('items.html', items=reader.get_unread()) @app.route('/feed/') def feed_list(): - reader = DBReader('../../cache.db') + reader = DBReader(DB_PATH) return jsonify({ 'feeds': json_list(reader.get_feeds()) }) @app.route('/feed/') def feed(token): - reader = DBReader('../../cache.db') + reader = DBReader(DB_PATH) return jsonify(reader.get_feed(token).to_json()) @app.route('/feed//items') def feed_items(token): - reader = DBReader('../../cache.db') - return jsonify({ 'items': json_list(reader.get_unread_for_feed(token)) }) + reader = DBReader(DB_PATH) + unread = reader.get_unread_for_feed(token) + return jsonify({ 'items': json_list(unread), "count": len(unread) }) @app.route('/feed//items/unread') def unread_feed_items(token): - reader = DBReader('../../cache.db') - return jsonify({ 'items': json_list(reader.get_unread_for_feed(token, True)) }) + reader = DBReader(DB_PATH) + unread = reader.get_unread_for_feed(token, True) + return jsonify({ 'items': json_list(unread), "count": len(unread) }) @app.route("/item/", methods=["GET", "POST"]) def item(entry_id): #post read=1 - reader = DBReader('../../cache.db') + reader = DBReader(DB_PATH) if request.method == 'POST': try: diff --git a/templates/items.html b/templates/items.html index 093b211..22adbba 100644 --- a/templates/items.html +++ b/templates/items.html @@ -63,11 +63,72 @@ ;(function(a){if(typeof define==='function'&&define.amd){define(['jquery'],a)}else{a(jQuery)}}(function($){var j=$.scrollTo=function(a,b,c){return $(window).scrollTo(a,b,c)};j.defaults={axis:'xy',duration:parseFloat($.fn.jquery)>=1.3?0:1,limit:true};j.window=function(a){return $(window)._scrollable()};$.fn._scrollable=function(){return this.map(function(){var a=this,isWin=!a.nodeName||$.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!isWin)return a;var b=(a.contentWindow||a).document||a.ownerDocument||a;return/webkit/i.test(navigator.userAgent)||b.compatMode=='BackCompat'?b.body:b.documentElement})};$.fn.scrollTo=function(f,g,h){if(typeof g=='object'){h=g;g=0}if(typeof h=='function')h={onAfter:h};if(f=='max')f=9e9;h=$.extend({},j.defaults,h);g=g||h.duration;h.queue=h.queue&&h.axis.length>1;if(h.queue)g/=2;h.offset=both(h.offset);h.over=both(h.over);return this._scrollable().each(function(){if(f==null)return;var d=this,$elem=$(d),targ=f,toff,attr={},win=$elem.is('html,body');switch(typeof targ){case'number':case'string':if(/^([+-]=?)?\d+(\.\d+)?(px|%)?$/.test(targ)){targ=both(targ);break}targ=win?$(targ):$(targ,this);if(!targ.length)return;case'object':if(targ.is||targ.style)toff=(targ=$(targ)).offset()}var e=$.isFunction(h.offset)&&h.offset(d,targ)||h.offset;$.each(h.axis.split(''),function(i,a){var b=a=='x'?'Left':'Top',pos=b.toLowerCase(),key='scroll'+b,old=d[key],max=j.max(d,a);if(toff){attr[key]=toff[pos]+(win?0:old-$elem.offset()[pos]);if(h.margin){attr[key]-=parseInt(targ.css('margin'+b))||0;attr[key]-=parseInt(targ.css('border'+b+'Width'))||0}attr[key]+=e[pos]||0;if(h.over[pos])attr[key]+=targ[a=='x'?'width':'height']()*h.over[pos]}else{var c=targ[pos];attr[key]=c.slice&&c.slice(-1)=='%'?parseFloat(c)/100*max:c}if(h.limit&&/^\d+$/.test(attr[key]))attr[key]=attr[key]<=0?0:Math.min(attr[key],max);if(!i&&h.queue){if(old!=attr[key])animate(h.onAfterFirst);delete attr[key]}});animate(h.onAfter);function animate(a){$elem.animate(attr,g,h.easing,a&&function(){a.call(this,targ,h)})}}).end()};j.max=function(a,b){var c=b=='x'?'Width':'Height',scroll='scroll'+c;if(!$(a).is('html,body'))return a[scroll]-$(a)[c.toLowerCase()]();var d='client'+c,html=a.ownerDocument.documentElement,body=a.ownerDocument.body;return Math.max(html[scroll],body[scroll])-Math.min(html[d],body[d])};function both(a){return $.isFunction(a)||typeof a=='object'?a:{top:a,left:a}};return j})); + +