generated from amitmerchant1990/reverie
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhantomStats.py
32 lines (26 loc) · 883 Bytes
/
PhantomStats.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
import requests
import json
import datetime
def getlisteners(url):
stats = json.loads(requests.get(url).text)
print(stats)
try:
listeners = stats['icestats']['source'][0]['listeners']
except KeyError:
listeners = stats['icestats']['source']['listeners']
return listeners
def now():
return datetime.datetime.utcnow()
newdata = {}
newdata['time'] = str(now())
newdata['listeners'] = getlisteners('https://phantommedia.radioca.st/status-json.xsl')
print(newdata)
try:
with open('_data/PhantomListeners.json','r', encoding='utf-8', newline='\n') as f:
olddata = json.loads(f.read())
except FileNotFoundError:
old_data = []
olddata.append(newdata)
with open('_data/PhantomListeners.json','wt', encoding='utf-8', newline='\n') as f:
f.write(json.dumps(olddata,indent=4)+"\n")
print(now(),"PhantomListeners.json saved")