2 """A simple tool to query the Ö1 7 Tage archive."""
5 __author__
= "Stefan Huber"
10 import dateutil
.parser
19 self
.json
= read_json("http://audioapi.orf.at/oe1/json/2.0/broadcasts/")
22 return map(_json_to_day
, self
.json
)
24 def get_broadcasts(self
, day
):
25 bjson
= self
.json
[day
]['broadcasts']
26 return map(_json_to_broadcast
, bjson
)
28 def get_broadcast(self
, day
, broadcast
):
29 return _json_to_broadcast(self
.json
[day
]['broadcasts'][broadcast
])
31 def get_broadcast_subtitle(self
, day
, broadcast
):
32 return self
.json
[day
]['broadcasts'][broadcast
]['subtitle']
34 def get_broadcast_url(self
, day
, broadcast
):
35 date
= self
.json
[day
]['day']
36 pk
= self
.json
[day
]['broadcasts'][broadcast
]['programKey']
38 burl
= 'https://audioapi.orf.at/oe1/api/json/current/broadcast/%s/%d'
39 bjson
= read_json(burl
% (pk
, date
))
41 sjson
= bjson
['streams']
45 sid
= sjson
[0]['loopStreamId']
46 surl
= 'http://loopstream01.apa.at/?channel=oe1&shoutcast=0&id=%s'
49 def get_broadcasts_by_regex(self
, key
):
50 rex
= re
.compile(key
, re
.IGNORECASE
)
53 for d
, djson
in enumerate(self
.json
):
54 for b
, bjson
in enumerate(djson
['broadcasts']):
55 if rex
.search(bjson
['title']) is not None:
57 elif bjson
['subtitle'] is not None and rex
.search(bjson
['subtitle']) is not None:
61 def _json_to_day(djson
):
62 return dateutil
.parser
.parse(djson
['dateISO'])
64 def _json_to_broadcast(bjson
):
65 dt
= dateutil
.parser
.parse(bjson
['startISO'])
66 return (dt
, bjson
['title'])
70 with urllib
.request
.urlopen(url
) as f
:
71 dec
= simplejson
.JSONDecoder()
72 return dec
.decode(f
.read())
74 def input_index(prompt
, li
):
77 idx
= int(input(prompt
))
78 if idx
< 0 or idx
>= len(li
):
79 print("Out out range!")
84 print("Unknown input.")
92 {0} -s, --search TITLE""".format(sys
.argv
[0]))
97 print("Choose a date:")
98 days
= list(a
.get_days())
99 for i
, date
in enumerate(days
):
100 print(" [%d] %s" % (i
, date
.strftime("%a %d. %b %Y")))
101 day
= input_index("Date: ", days
)
104 print("Choose a broadcast:")
105 broadcasts
= list(a
.get_broadcasts(day
))
106 for i
, b
in enumerate(broadcasts
):
108 print(" [%2d] %s %s" % (i
, date
.strftime("%H:%M:%S"), title
))
109 broadcast
= input_index("Broadcast: ", broadcasts
)
112 url
= a
.get_broadcast_url(day
, broadcast
)
114 print("No stream found.")
118 def screen_search(key
):
121 for d
, b
in a
.get_broadcasts_by_regex(key
):
122 date
, title
= a
.get_broadcast(d
, b
)
123 print("%s %s" % (date
.strftime("%a %d.%m.%Y %H:%M:%S"), title
))
124 print(" %s" % a
.get_broadcast_url(d
, b
))
127 if __name__
== "__main__":
130 opts
, args
= getopt
.getopt(sys
.argv
[1:], "hcs:",
131 ["help", "choose", "search="])
132 except getopt
.GetoptError
as err
:
138 if o
in ["-h", "--help"]:
140 if o
in ["-c", "--choose"]:
142 if o
in ["-s", "--search"]: