Skip to content

Instantly share code, notes, and snippets.

@henryyang42
Last active June 7, 2017 13:58
Show Gist options
  • Save henryyang42/495dbc4d34a370293becc07be857e488 to your computer and use it in GitHub Desktop.
Save henryyang42/495dbc4d34a370293becc07be857e488 to your computer and use it in GitHub Desktop.
import datetime
import requests
import time
import logging
import os
import sys
from bs4 import BeautifulSoup
content_template = '過去24小時收益: {:.6} ETH ({:.6} {})'
title_template = '魚池礦機狀態 {}'
subtitle_template = '15分鐘/1小時均速: {}/{} Mhash/s'
email_content = '乾礦機壞掉了{} 快去看: {}'
class F2PoolMonitor:
def __init__(self):
self.f2pool_url = 'https://www.f2pool.com/eth/0xc41056c46803b297a363db6468f803120e2210d1'
self.currency = 'TWD' # Can be ['TWD', 'USD', 'BTC',...]
self.timeout = 60
self.logger = logging.getLogger()
self.logger.setLevel(logging.INFO)
self.logger.addHandler(logging.FileHandler('f2pool_monitor.log'))
self.logger.addHandler(logging.StreamHandler())
def send_email(self, miner_status):
content = email_content.format(miner_status, self.f2pool_url)
requests.post(
"https://api.mailgun.net/v3/sandboxa???????????????.mailgun.org/messages",
auth=("api", "key-??????????????"),
data={"from": "Mailgun Sandbox <postmaster@sandbox????????????????.mailgun.org>",
"to": "Henry Yang <henryyang42@gmail.com>",
"subject": "礦機壞惹QQ",
"text": content})
self.logger.info('send email with content {}'.format(content))
def check_status(self):
def notify(content, title, subtitle):
os.system("""
osascript -e 'display notification "{}" with title "{}" subtitle "{}"'
""".format(content, title, subtitle))
self.logger.info('{}\n{}\n{}'.format(title, subtitle, content))
try:
r_f2pool = requests.get(self.f2pool_url)
r_rate = requests.get(
'https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms={}'.format(self.currency))
now = datetime.datetime.now()
if r_f2pool.status_code == 200 and r_rate.status_code == 200:
currency_rate = float(r_rate.json()[self.currency])
soup = BeautifulSoup(r_f2pool.text, 'html.parser')
balance, workers = soup.find_all('table')[1:3] # Dirty AF
daily_return, total_return, paid_balance, account_balance = [
float(td.text[:-4]) for td in balance.tbody.find_all('td')]
_, hashrate_15min, hashrate_1hr, _, _, _, miner_status = [
th.text for th in workers.tfoot.find_all('th')]
content = content_template.format(
daily_return, daily_return * currency_rate, self.currency, paid_balance, paid_balance * currency_rate, self.currency)
title = title_template.format(miner_status)
subtitle = subtitle_template.format(
hashrate_15min[:-8], hashrate_1hr[:-8])
notify(content, title, subtitle)
if 1 != eval(miner_status):
self.send_email(miner_status)
else:
self.logger.error(
str(now) + ' fail to get response. Try again.')
except Exception as e:
self.logger.error(str(now) + ' get error. Try again.')
print(e)
def looping(self):
while True:
self.check_status()
time.sleep(self.timeout)
if __name__ == '__main__':
monitor = F2PoolMonitor()
monitor.looping()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment