forked from llSourcell/bitcoin_prediction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_bitcoin_new.py
33 lines (26 loc) · 1022 Bytes
/
get_bitcoin_new.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import requests, json
from time import sleep
from datetime import datetime
import sys
import traceback
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("outputfile", nargs='?', default="bitcoin_price.json")
parser.add_argument("errorfile", nargs='?', default="bitcoin_price_error.txt")
args = parser.parse_args()
def getBitcoinPrice():
URL = 'https://www.bitstamp.net/api/ticker/'
try:
r = requests.get(URL)
bitcoindata = json.loads(r.text)
bitcoindata['datetime'] = datetime.utcfromtimestamp(int(bitcoindata['timestamp'])).strftime('%Y-%m-%d-%H-%M-%S')
with open(args.outputfile, mode='a') as file:
file.write('{},\n'.format(json.dumps(bitcoindata)))
except:
exc_type, exc_value, exc_traceback = sys.exc_info()
with open(args.errorfile, mode='a') as file:
traceback.print_exc(file=file)
file.write(('-'*100)+'\n\n')
while True:
getBitcoinPrice()
sleep(10)