# -*- coding: utf-8 -*- import sys import os import sqlite3 import time def path( relative ): return relative; #sys.path[0] + '/' + relative; def log( message ): connection = sqlite3.connect( path( 'resource/base.db' ) ); cursor = connection.cursor(); cursor.execute( "INSERT INTO log VALUES ( ?, ?, ? )", ( time.time(), message, os.getpid() ) ); connection.commit(); cursor.close(); def getLastTimelineId ( user_id ): connection = sqlite3.connect( path( 'resource/base.db' ) ); cursor = connection.cursor(); cursor.execute( "SELECT last_fetched_timeline_id FROM user WHERE id = ?", ( user_id, ) ); row = cursor.fetchone(); cursor.close(); if None == row: return -1; else: return row[0]; def setLastTimelineId ( user_id, timeline_id ): connection = sqlite3.connect( path( 'resource/base.db' ) ); cursor = connection.cursor(); if -1 == getLastTimelineId( user_id ): cursor.execute( "INSERT INTO user VALUES ( ?, ?, ? )", ( user_id, timeline_id, 0 ) ); else: cursor.execute( "UPDATE user SET last_fetched_timeline_id = ? WHERE id = ?", ( timeline_id, user_id ) ); connection.commit(); cursor.close(); def getLastDMId ( user_id ): connection = sqlite3.connect( path( 'resource/base.db' ) ); cursor = connection.cursor(); cursor.execute( "SELECT last_fetched_dm_id FROM user WHERE id = ?", ( user_id, ) ); row = cursor.fetchone(); cursor.close(); if None == row: return -1; else: return row[0]; def setLastDMId ( user_id, dm_id ): connection = sqlite3.connect( path( 'resource/base.db' ) ); cursor = connection.cursor(); if -1 == getLastTimelineId( user_id ): cursor.execute( "INSERT INTO user VALUES ( ?, ?, ? )", ( user_id, 0, dm_id ) ); else: cursor.execute( "UPDATE user SET last_fetched_dm_id = ? WHERE id = ?", ( dm_id, user_id ) ); connection.commit(); cursor.close(); def saveTweet ( user_id, tweet ): connection = sqlite3.connect( path( 'resource/base.db' ) ); cursor = connection.cursor(); cursor.execute( "INSERT OR REPLACE INTO tweet VALUES ( ?, ?, ?, ?, ?, ?, ? )", ( tweet['id'], tweet['text'], tweet['created_at'], tweet['user']['id'], tweet['user']['screen_name'], tweet['user']['profile_image_url'], user_id ) ); connection.commit(); cursor.close(); def getLastSavedTweets ( user_id ): connection = sqlite3.connect( path( 'resource/base.db' ) ); cursor = connection.cursor(); cursor.execute( "SELECT * FROM tweet WHERE local_user_id = ? ORDER BY id ASC", ( user_id, ) ); ret = []; counter = 0; for row in cursor: ++counter; if counter > 30: break; ret.append( { 'created_at': row[2], 'id': row[0], 'text': row[1], 'user': { 'id': row[3], 'screen_name': row[4], 'profile_image_url': row[5] } } ); cursor.close(); return ret;