-
Notifications
You must be signed in to change notification settings - Fork 0
/
txfmtracker.py
executable file
·46 lines (35 loc) · 1.21 KB
/
txfmtracker.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
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python3
"""
TXFMTrackService
(C) 2015
David Rieger
v1.2
"""
import urllib.request
import json
from storagemanager import StorageManager
def now_playing():
# New TXFM website has a nicer API that makes things much easier
URL = "http://www.txfm.ie/assets/includes/ajax/player_info.php?type=On+Air¤tstationID=11"
req = urllib.request.Request(url=URL, headers={'User-Agent': 'Mozilla/5.0'})
# It returns a JSON which has a key called "data" which contains - get this - HTML
jsonstr = urllib.request.urlopen(req).readall().decode("UTF-8")
onairinfo = json.loads(jsonstr)
# Try to obtain current song info from JSON response
try:
title = onairinfo["currentTitle"]
artist = onairinfo["currentArtist"]
except IndexError:
print("API Error")
pass
# If either song or title info not available - set show title as title
if not (artist and title):
artist = ""
try:
title = onairinfo["title"]
except IndexError:
title = "ERROR RETRIEVING SONG INFO"
return {"title": title, "artist": artist}
if __name__ == "__main__":
print(now_playing())
StorageManager().add_song(now_playing())