def check_price(config): msg = [] log_msg = '' global price_dict Log.log_debug('Check price...') for desc in price_dict: currency_handle = price_dict[desc] price = currency_handle['get_price']() if price >= 0: log_msg += '{} {} {:.2%};'.format( desc, price, price / currency_handle['last_report'] - 1) if need_report(price, currency_handle['last_report'], currency_handle['tendency'], config['THRESHOLD']): msg.append( Message('{} current {}, last report {}, {:.2%}'.format( desc, price, currency_handle['last_report'], price / currency_handle['last_report'] - 1))) currency_handle[ 'tendency'] = price - currency_handle['last_report'] currency_handle['last_report'] = price price_dict.update({desc: currency_handle}) Log.log_debug(log_msg) if msg == []: return None return msg
def push(self, msg): try: Client(self.config['KEY']).send_message(msg, title = self.config['TITLE']) except Exception: Log.log_error(traceback.format_exc()) return False return True
def err_check(r): result = r.json() if 'Response' in result: Log.log_error('Request failed, error message: {}'.format( result['Message'])) return True return False
def get_cny_exchange_rate(): now = time.time() # Update CNY exchange rate per hour. if now - cny_exchange_rate[1] < 3600: return cny_exchange_rate[0] Log.log_debug('Update CNY exchange rate...') rate = get_exchange_rate() if rate >= 0: cny_exchange_rate[0] = rate cny_exchange_rate[1] = now Log.log_debug('USD:CNY = {}'.format(cny_exchange_rate[0])) return cny_exchange_rate[0]
def offline_checker(config): global last_hashrate Log.log_debug('Offline check...') current_hashrate = get_report_hashrate(config) if current_hashrate >= 0: Log.log_debug('Current hashrate is {}'.format(current_hashrate)) high = last_hashrate * (100 + config['THRESHOLD']) / 100 low = last_hashrate * (100 - config['THRESHOLD']) / 100 last_hashrate = current_hashrate if current_hashrate > high or current_hashrate < low: return Message(current_hashrate, True) return None
def push(self, msg): try: ret = self.app.notify(event_name=self.config['EVENT_NAME'], trackers={self.config['TRACKERS']: str(msg)}) except Exception: Log.log_error(traceback.format_exc()) return False if ret == None: Log.log_error('Unknown error.') return False if ret['error']: Log.log_error('Push error, msg: {}'.format(ret['msg'])) return False return True
def init_price_dict(config): global price_dict Log.log_debug('Price init...') msg = '' # Go through all handles for handle in config['HANDLE']: handle_impl = import_module('plugin.{}'.format(handle['HANDLE_NAME'])) for currency_pair in handle['CURRENCY']: c1 = currency_pair['source_currency'] c2 = currency_pair['target_currency'] get_price = handle_impl.get_price_generator(c1, c2) price = get_price() desc = '{} {}:{}'.format(handle['HANDLE_NAME'], c1, c2) msg += '{} {}; '.format(desc, price) price_dict.update({ desc: { 'get_price': get_price, 'last_report': price, 'tendency': 0 } }) Log.log_debug(msg) Log.log_debug('Price init finished.')
import traceback import json import sys import time from importlib import import_module from PeriodicPusher import PeriodicPusher, Message from PeriodicPusher.Utils import Log, HttpHelper from plugin.exchange import * if __name__ != '__main__': exit() if len(sys.argv[1]) < 2: Log.log_error('Missing config file.') exit() pp = PeriodicPusher(sys.argv[1]) price_dict = dict() cny_exchange_rate = [0, 0] def get_cny_exchange_rate(): now = time.time() # Update CNY exchange rate per hour. if now - cny_exchange_rate[1] < 3600: return cny_exchange_rate[0] Log.log_debug('Update CNY exchange rate...') rate = get_exchange_rate() if rate >= 0: cny_exchange_rate[0] = rate cny_exchange_rate[1] = now
def err_check(self, r): res = r.json() if res['code'] != 0: Log.log_error('Request failed, message: {}'.format(res['message'])) return True return False
def __init__(self, config): Log.log_debug('Pusher data: {}'.format(json.dumps(config, indent=4)))
def push(self, msg): Log.log_debug('Get push message: {}'.format(msg)) TestCallback.callback.put(msg) return True
def demo_get_notification(config): Log.log_debug('From get_notification, config: {}'.format( json.dumps(config, indent=4))) # Add your notification here, config contains private_data in json file. return Message('Test')
def demo_init(config): Log.log_debug('From init, config: {}'.format(json.dumps(config, indent=4)))
def init(config): global last_hashrate last_hashrate = get_report_hashrate(config) if last_hashrate < 0: raise Exception('Init hashrate failed.') Log.log_debug('Init hashrate is {}'.format(last_hashrate))
def err_check(r): result = r.json() if not result['status']: Log.log_error('Api call failed, error: {}'.format(result['error'])) return True return False
def err_check(r): result = r.json() if result['result'] != "true": Log.log_error('Request failed, error message: {}'.format(result['message'])) return True return False