class RewardCrawler(threading.Thread): def __init__(self, bot): threading.Thread.__init__(self) self.db = MongoConnector() self.db.connect(cp['DATABASE']['Address'], cp['DATABASE']['Name']) self.collection = cp['DATABASE']['MonitoringCollection'] self.telegram_bot = bot self.running = True print('Reward crawler started') def terminate(self): self.running = False def run(self): while self.running: success, result = self.db.find(self.collection, {}, many=True) if not success: continue for entry in result: new_transactions = blockchain.get_new_transactions(entry['address'], entry['last_transaction']) for transaction in reversed(new_transactions): timestamp = int(transaction[2]) received = round(float(transaction[1]) - float(transaction[0]), 7) entry['balance'] += received if entry['last_transaction'] < timestamp: entry['last_transaction'] = timestamp message = NEW_TRANSACTION_MESSAGE_TEMPLATE.format(entry['name'], timestamp_to_date(timestamp), float(received)) try: self.telegram_bot.send_message(chat_id=entry['telegram_id'], text=message) except Exception as e: print("User blocked bot by id:", entry['telegram_id']) # entry['total_transactions'] = blockchain.get_total_transactions(entry['address']) db.update(self.collection, {'_id': entry['_id']}, entry) time.sleep(0.1) global last_checked last_checked = datetime.datetime.utcnow() time.sleep(CRAWLER_SLEEP_TIME)
import logging import datetime import re # Enable logging logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) logger = logging.getLogger(__name__) cp = ConfigParser() cp.optionxform = str cp.read('../config.ini') db = MongoConnector() db.connect(cp['DATABASE']['Address'], cp['DATABASE']['Name']) blockchain = BlockchainConnector() blockchain.connect(cp['POSTGRES']) monitoring_collection = cp['DATABASE']['MonitoringCollection'] CRAWLER_SLEEP_TIME = 120 NEW_TRANSACTION_MESSAGE_TEMPLATE = 'New transaction for "{}" ({}): {} XSN' telegram_bot_token = cp['TELEGRAM']['SecretKey'] DATE_FORMAT = '%d/%m/%Y %H:%M:%S' ADD_ADDRESS_MESSAGE = 'Enter address for ' ADD_NAME_MESSAGE = 'Enter monitor name' STATISTICS_MESSAGE_TEMPLATE = 'This bot monitors {} addresses from {} users!'