def InitDB(self): needInitTables = True if (os.path.exists(self.__defDBPath)): needInitTables = False self.__conn = sqlite3.connect(self.__defDBPath) if (needInitTables): try: cursor = self.__conn.cursor() cursor.execute(''' create table Info (key char[255], strvalue char[255], numvalue long) ''') # Init default values cursor.execute(''' insert into Info (key, numvalue) values('{0}', '{1}') '''.format(self.__keyDBSchemaVer, self.__DBSchemaVer)) self.__conn.commit() except: Logger.ErrorLog('Failed to init database') self.__conn.rollback() os.remove(self.__defDBPath) return False return True
def __init__(self, conn, name): assert (conn) assert (len(name) > 0) self.__conn = conn self.__name = name self.__dbname = self.__tblprefixData + self.__name self.__keyLastFetchTS = self.__keyprefixLastFetchTS + name if (self.__isTblExist()): return try: cursor = self.__conn.cursor() cursor.execute(''' create table {0} (fbid char[255], data text) '''.format(self.__dbname)) cursor.execute(''' insert into Info (key, numvalue) values ('{0}', 0) '''.format(self.__keyLastFetchTS)) self.__conn.commit() except: Logger.ErrorLog( 'Failed to init cache data db {0}'.format(name)) self.__conn.rollback() print 'Exception catched:\nType: %s\nDesc:%s' % ( sys.exc_info()[0], sys.exc_info()[1])
def feed(self): data = self.__cache.getData() if (len(data) == 0): return True token = UDRFeeder.__createToken(self.__authKey, self.__group_cid) feedData = { 'udrId': self.__cache.getName(), 'group': self.__group, 'cid': self.__cid, 'authId': self.__authId, 'authToken': token[0], 'timestamp': token[1], 'list': [], } for d in data: feedData['list'].append({'key': d[0], 'value': d[1]}) urlParseResult = urlparse.urlparse(self.__ERUDRFeedAPI) conn = None if (urlParseResult.scheme == 'https'): conn = httplib.HTTPSConnection(urlParseResult.netloc) else: conn = httplib.HTTPConnection(urlParseResult.netloc) feedDataJSON = json.dumps(feedData) conn.request('POST', urlParseResult.path, feedDataJSON, {'Content-Type': 'application/json'}) res = conn.getresponse() ret = False if (res.status == 200): result = res.read() if (result == 'success'): Logger.DbgLog('API server processed completely.') self.__cache.clearData() ret = True else: Logger.ErrorLog('Response from ER API server: %s' % result) else: Logger.ErrorLog('Response status: %d' % res.status) return ret
def defParser(cls, response): ''' Default parser - It [in] response: A HTTP response ''' resultJSON = json.load(response) Logger.DbgLog('Parse fetcher response status: %s' % resultJSON['status']) return resultJSON['result']
def test(db): udrdata1 = db.createCacheData('FBUDR1') udrdata1.setLastFetchTS(time.time()) Logger.DbgLog('Fetched TS: {0}'.format(udrdata1.getLastFetchTS())) ret = udrdata1.clearData() assert (ret) ret = udrdata1.setData([('u001', '"pid1", "pid2"'), ('u002', '"pid3", "pid4"')]) assert (ret) ret = udrdata1.getData() assert (len(ret) > 0) print ret
def getData(self): ret = [] cursor = self.__conn.cursor() try: cursor.execute(''' select fbid, data from {0} '''.format(self.__dbname)) ret = cursor.fetchall() except: Logger.ErrorLog('Failed ot get cache data {0}'.format( self.__dbname)) print 'Exception catched:\nType: %s\nDesc:%s' % ( sys.exc_info()[0], sys.exc_info()[1]) return [] return ret
def setData(self, data): assert (data) if len(data) <= 0: return True cursor = self.__conn.cursor() try: cursor.executemany( ''' insert into {0} values (?, ?) '''.format(self.__dbname), data) self.__conn.commit() except: Logger.ErrorLog('Failed to insert cache data {0}'.format( self.__dbname)) self.__conn.rollback() print 'Exception catched:\nType: %s\nDesc:%s' % ( sys.exc_info()[0], sys.exc_info()[1]) return False return True
def fetch(self, requestData='[]', requestDataType='application/json'): urlParseResult = urlparse.urlparse(self.__api) conn = None if (urlParseResult.scheme=='https'): conn = httplib.HTTPSConnection(urlParseResult.netloc) else: conn = httplib.HTTPConnection(urlParseResult.netloc) conn.request('POST', urlParseResult.path, requestData, {'Content-Type': requestDataType}) # conn.request('GET', urlParseResult.path) res = conn.getresponse() if (res.status==200): data = self.__parser(res) self.__cache.setData(data) self.__cache.setLastFetchTS(time.time()) else: Logger.ErrorLog('Response status: %d' % res.status) return False return True