Skip to content

Instantly share code, notes, and snippets.

@andrew12 andrew12/stats.py Secret
Created Sep 27, 2013

Embed
What would you like to do?
import requests
import json
from time import strptime
from decimal import Decimal
from calendar import timegm
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument('filename')
parser.add_argument('-v', '--verbose', action='store_true')
args = parser.parse_args()
try:
with open(args.filename, 'r') as f:
history = json.load(f)
except FileNotFoundError:
history = []
try:
data = requests.get('http://middlecoin.com/json', timeout=10).json()
timestamp = timegm(strptime(data['time'], '%Y-%m-%d %H:%M:%S'))
if len(history) == 0 or history[-1][0] != timestamp:
immature = Decimal(data['totalImmatureBalance'])
unexchanged = Decimal(data['totalUnexchangedBalance'])
balance = Decimal(data['totalBalance'])
paid = Decimal(data['totalPaidOut'])
new = [
timestamp,
float(data['totalImmatureBalance']),
float(data['totalUnexchangedBalance']),
float(data['totalBalance']),
float(data['totalPaidOut'])
]
if args.verbose:
print(new)
history.append(new)
with open(args.filename, 'w') as f:
json.dump(history, f)
else:
if args.verbose:
print('no change')
except requests.exceptions.Timeout:
if args.verbose:
raise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.