pyferea/sqlite_db.py

108 lines
4.1 KiB
Python
Raw Normal View History

2012-07-01 09:39:25 +00:00
import sqlite3
def dbcreate(conn):
conn.execute("""
CREATE TABLE IF NOT EXISTS feeds (
feed TEXT NOT NULL,
title TEXT,
favicon BLOB,
etag TEXT,
lastmodified TEXT,
unread INTEGER
)
""")
conn.execute("""
CREATE UNIQUE INDEX IF NOT EXISTS feedidx ON feeds (feed)
""")
conn.execute("""
CREATE TABLE IF NOT EXISTS entries (
feed TEXT NOT NULL,
entry TEXT NOT NULL,
title TEXT,
content TEXT,
link TEXT,
date INTEGER,
unread INTEGER,
categories TEXT
)""")
conn.execute("""
CREATE UNIQUE INDEX IF NOT EXISTS entridx ON entries (feed,entry)
""")
conn.commit()
def convert(filename):
conn = sqlite3.connect(filename)
conn.text_factory = str
dbcreate(conn)
import shelve
feeddb = shelve.open("pyferea.db")
for feed, fvalues in feeddb.items():
conn.execute("""REPLACE INTO feeds (feed, title, favicon, etag, lastmodified, unread) VALUES (?,?,?,?,?,?)""",
(feed, fvalues.get('title'), fvalues.get('favicon'), fvalues.get('etag'), fvalues.get('lastmodified'), fvalues.get('unread')))
for entry, evalues in fvalues['items'].items():
conn.execute("""REPLACE INTO entries (feed, entry, title, content, link, date, unread, categories) VALUES (?, ?, ?, ?, ?, ?, ?, ?)""",
(feed, entry, evalues.get('title'), evalues.get('content'), evalues.get('link'), evalues.get('date'), evalues.get('unread'), ', '.join(evalues.get('categories') or [])))
conn.commit()
conn.close()
feeddb.close()
class SQLStorage():
def __init__(self, filename=':memory:'):
self.conn = sqlite3.connect(filename)
self.conn.text_factory = str
dbcreate(self.conn)
def get_feed(self, feed):
2012-07-01 12:29:04 +00:00
result = self.conn.execute("""SELECT title, favicon, etag, lastmodified, unread FROM feeds WHERE feed=?""", (feed,)).fetchone()
if result:
return dict(zip(('title', 'favicon', 'etag', 'lastmodified', 'unread'), result))
else:
return dict()
2012-07-01 09:39:25 +00:00
def get_entry(self, feed, entry):
2012-07-01 12:29:04 +00:00
result = self.conn.execute("""SELECT title, content, link, date, unread, categories FROM entries WHERE feed=? AND entry=?""", (feed, entry)).fetchone()
if result:
return dict(zip(('title', 'content', 'link', 'date', 'unread', 'categories'), result))
else:
return dict()
2012-07-01 09:39:25 +00:00
def get_entries_all(self, feed):
2012-07-01 12:29:04 +00:00
result = self.conn.execute(
"""SELECT entry, title, date, unread FROM entries WHERE feed=? ORDER BY date DESC""",
(feed,)).fetchall()
if result:
return [dict(zip(('entry', 'title', 'date', 'unread'), c)) for c in result]
else:
return list()
2012-07-01 09:39:25 +00:00
def add_entry(self, feed, entry, values):
self.conn.execute("""REPLACE INTO entries (feed, entry, title, content, link, date, unread, categories) VALUES (?, ?, ?, ?, ?, ?, ?, ?)""",
(feed, entry, values['title'], values['content'], values['link'], values['date'], values['unread'], values['categories']))
self.conn.commit()
def update_feed(self, feed, values):
2012-07-01 13:37:49 +00:00
self.conn.execute("""REPLACE INTO feeds (feed, title, favicon, etag, lastmodified, unread) VALUES (?,?,?,?,?,?)""",
(feed, values['title'], values.get('favicon'), values.get('etag'), values.get('lastmodified'), values['unread']))
2012-07-01 09:39:25 +00:00
self.conn.commit()
def set_favicon(self, feed, favicon):
self.conn.execute("""UPDATE feeds SET favicon=? WHERE feed=?""", (favicon, feed))
self.conn.commit()
def mark_read(self, feed, entry):
self.conn.execute("""UPDATE entries SET unread=0 WHERE feed=? AND entry=?""", (feed, entry))
self.conn.execute("""UPDATE feeds SET unread=unread-1 WHERE feed=?""", (feed,))
self.conn.commit()
def mark_read_feed(self, feed):
self.conn.execute("""UPDATE entries SET unread=0 WHERE unread=1""")
self.conn.execute("""UPDATE feeds set unread=0 WHERE feed=?""", (feed,))
self.conn.commit()
def close(self):
self.conn.close()
if __name__ == "__main__":
convert("pyferea.sqlite")