#!/usr/bin/python3 #encoding:UTF-8 import sys, time from contextlib import suppress import pymysql # init db try: conn = pymysql.connect( host = "steem-lightdb.com", user = "steem", password = "steem", database = "steemdb", charset = 'utf8', cursorclass = pymysql.cursors.DictCursor) #conn.autocommit(True) except Exception as e: print('[warning] DB connection failed', e) sys.exit()
def getLatestBlockNumFromDB(): global conn sql = ''' Select block_num from blocks Order by block_num desc limit 1; ''' try: with conn.cursor() as cursor: cursor.execute(sql) result = cursor.fetchone() if result: return int(result['block_num']) + 1 else: return 1 conn.commit() except Exception as e: print('[warning]get latest block num error', e) return 1
def run(): while True: latest_block_num = getLatestBlockNumFromDB() print(latest_block_num) time.sleep(3)
if __name__ == '__main__': with suppress(KeyboardInterrupt): run()