diff options
Diffstat (limited to 'db.py')
-rw-r--r-- | db.py | 103 |
1 files changed, 92 insertions, 11 deletions
@@ -9,15 +9,25 @@ import time import tweepy +_db = None + +def open_db(): + """Open the database.""" + + global _db + + _db = sqlite3.connect('HTT.db', detect_types=sqlite3.PARSE_DECLTYPES) + + class LikeMemory(): """Track all liked Tweets.""" def __init__(self, api): """Build the Python object.""" - self._api = api + global _db - self._db = sqlite3.connect('HTT.db', detect_types=sqlite3.PARSE_DECLTYPES) + self._api = api sqlite3.register_adapter(bool, int) sqlite3.register_converter("BOOLEAN", lambda v: bool(int(v))) @@ -32,9 +42,9 @@ class LikeMemory(): ) ''' - cursor = self._db.cursor() + cursor = _db.cursor() cursor.execute(sql) - self._db.commit() + _db.commit() def _compute_content_fingerprint(self, content): @@ -78,17 +88,19 @@ class LikeMemory(): if pos != -1: base = base[:pos] - return hashlib.md5(base.rstrip(' ').encode('utf-8')).hexdigest() + return hashlib.md5(base.rstrip(' ').lower().encode('utf-8')).hexdigest() def is_original_content(self, content): """Ensure that a given content has never been seen.""" + global _db + fingerprint = self._compute_content_fingerprint(content) values = (fingerprint, ) - cursor = self._db.cursor() + cursor = _db.cursor() cursor.execute('SELECT sid FROM LikedTweets WHERE fingerprint = ?', values) found = cursor.fetchone() @@ -99,25 +111,29 @@ class LikeMemory(): def save_liked_status(self, sid, username, content): """Remember a given liked status.""" + global _db + fingerprint = self._compute_content_fingerprint(content) timestamp = int(time.time()) values = (sid, username, fingerprint, timestamp, False) - cursor = self._db.cursor() + cursor = _db.cursor() cursor.execute('INSERT INTO LikedTweets VALUES (?, ?, ?, ?, ?)', values) - self._db.commit() + _db.commit() def purge_old_status(self): """Purge old seen statuses.""" + global _db + timestamp = int(time.time()) - max_age * 24 * 60 * 60 values = (timestamp, False) - cursor = self._db.cursor() + cursor = _db.cursor() cursor.execute('SELECT sid FROM LikedTweets WHERE timestamp < ? AND purged = ?', values) rows = cursor.fetchall() @@ -137,9 +153,74 @@ class LikeMemory(): values = (True, sid) - cursor = self._db.cursor() + cursor = _db.cursor() cursor.execute('UPDATE LikedTweets SET purged = ? WHERE sid = ?', values) - self._db.commit() + _db.commit() print('Purged %d liked Tweet%s!' % (len(rows), '' if len(rows) <= 1 else 's')) + + +class TrackMemory(): + """Remember last seen Tweet for users.""" + + def __init__(self): + """Build the Python object.""" + + global _db + + sql = ''' + CREATE TABLE IF NOT EXISTS TrackedUsers( + uid INTEGER PRIMARY KEY, + username TEXT, + last INTEGER + ) + ''' + + cursor = _db.cursor() + cursor.execute(sql) + _db.commit() + + + def get_last_seen_for(self, uid): + """Get the status id of the last Tweet for a given user.""" + + global _db + + values = (uid, ) + + cursor = _db.cursor() + cursor.execute('SELECT last FROM TrackedUsers WHERE uid = ?', values) + + found = cursor.fetchone() + + if found is None: + last = None + else: + last = found[0] + + return last + + + def set_last_seen_for(self, uid, name, sid): + """Set the status id of the last seen Tweet for a given user.""" + + global _db + + since = self.get_last_seen_for(uid) + + if since is None: + + values = (uid, name, sid) + + cursor = _db.cursor() + cursor.execute('INSERT INTO TrackedUsers VALUES (?, ?, ?)', values) + + else: + + values = (sid, uid) + + cursor = _db.cursor() + cursor.execute('UPDATE TrackedUsers SET last = ? WHERE uid = ?', values) + + _db.commit() |