#!/usr/bin/env python3
-"""A simple tool to query the Ö1 7 Tage archive."""
-__version__ = "1.0"
+"""A simple tool to query the Oe1 7 Tage archive."""
+
+__version__ = "2.0"
__author__ = "Stefan Huber"
import sys
import getopt
import re
+import os
class Archive:
def get_broadcast(self, day, broadcast):
return _json_to_broadcast(self.json[day]['broadcasts'][broadcast])
+ def get_player_url(self, day, broadcast):
+ date = self.json[day]['day']
+ pk = self.json[day]['broadcasts'][broadcast]['programKey']
+ url = "http://oe1.orf.at/player/%d/%s"
+ return url % (date, pk)
+
def get_broadcast_subtitle(self, day, broadcast):
return self.json[day]['broadcasts'][broadcast]['subtitle']
+ def get_broadcast_pk(self, day, broadcast):
+ return self.json[day]['broadcasts'][broadcast]['programKey']
+
def get_broadcast_url(self, day, broadcast):
date = self.json[day]['day']
pk = self.json[day]['broadcasts'][broadcast]['programKey']
surl = 'http://loopstream01.apa.at/?channel=oe1&shoutcast=0&id=%s'
return surl % sid
+ def get_broadcast_description(self, day, broadcast):
+ date = self.json[day]['day']
+ pk = self.json[day]['broadcasts'][broadcast]['programKey']
+
+ burl = 'https://audioapi.orf.at/oe1/api/json/current/broadcast/%s/%d'
+ bjson = read_json(burl % (pk, date))
+
+ description = bjson['description']
+ akm = bjson['akm']
+ if description is None:
+ description = ""
+ if akm is None:
+ akm = ""
+ return description + "<br>" + akm;
+
def get_broadcasts_by_regex(self, key):
rex = re.compile(key, re.IGNORECASE)
for i, date in enumerate(days):
print(" [%d] %s" % (i, date.strftime("%a %d. %b %Y")))
day = input_index("Date: ", days)
+ chosen_datetime = days[day]
print()
print("Choose a broadcast:")
broadcast = input_index("Broadcast: ", broadcasts)
print()
+ print_broadcast_info(a, day, broadcast)
+ print()
+
url = a.get_broadcast_url(day, broadcast)
- if url is None:
- print("No stream found.")
- sys.exit(1)
- print(url)
+ if url is not None:
+ answer = input("Do you want to download the chosen broadcast? (y/N) ")
+ if answer in ["y", "Y", "j", "J"]:
+ name = input("Download directory (prefix): ")
+
+ try:
+ dirname = get_directory_name(name, chosen_datetime)
+ print("Downloading to %s..." % dirname)
+
+ make_directory(name, chosen_datetime)
+
+ description = a.get_broadcast_description(day, broadcast)
+ write_html_file(name, chosen_datetime, description)
+
+ write_mp3_file(name, chosen_datetime, url)
+
+ except OSError as e:
+ print("Error creating directory.")
+ print(e)
+
+ except requests.exceptions.RequestException as e:
+ print("Request getting mp3 failed.")
+
+ except Exception as e:
+ print("Error downloading mp3.")
+ print(e)
+
+def get_directory_name(name, datetime):
+ prefix = ""
+ if len(name) > 0:
+ prefix = name + "_"
+
+ return prefix + datetime.strftime("%Y-%m-%d")
+
+def make_directory(name, datetime):
+ """Creates the download subdirectory for the given name and datetime."""
+ dirname = get_directory_name(name, datetime)
+ if not os.path.exists(dirname):
+ os.makedirs(dirname)
+
+def write_html_file(name, datetime, description):
+ """Stores broadcast description into a html file."""
+
+ longname = get_directory_name(name, datetime)
+ filepath = os.path.join(longname, "description.html")
+ file = open(filepath, 'w+')
+ file.write("<!DOCTYPE html>\n")
+ file.write("<html>\n")
+ file.write("<head>\n")
+ file.write("<title>\n")
+ file.write("%s %s\n" % (name, datetime.strftime("%d.%m.%Y")))
+ file.write("</title>\n")
+ file.write("<meta charset = \"utf-8\">\n")
+ file.write("</head>\n")
+ file.write("<body>\n")
+ file.write("%s %s" % (name, datetime.strftime("%d.%m.%Y")))
+ file.write(description)
+ file.write("</body>\n")
+ file.write("</html>")
+ file.close()
+
+def write_mp3_file(name, datetime, url):
+ import requests
+
+ longname = get_directory_name(name, datetime)
+ filepath = os.path.join(longname, "stream.mp3")
+
+ print("Fetching mp3...")
+ r = requests.get(url, stream=True)
+ if r.status_code == 200:
+ with open(filepath, 'wb') as f:
+ f.write(r.content)
+ else:
+ print("Error downloading mp3. Status code: %d" % r.status_code)
def screen_search(key):
a = Archive()
-
for d, b in a.get_broadcasts_by_regex(key):
- date, title = a.get_broadcast(d, b)
- print("%s %s" % (date.strftime("%a %d.%m.%Y %H:%M:%S"), title))
- print(" %s" % a.get_broadcast_url(d, b))
+ print_broadcast_info(a, d, b)
print()
+def print_broadcast_info(archive, day, broadcast):
+ a, d, b = archive, day, broadcast
+ date, title = a.get_broadcast(d, b)
+
+ print("%s %s" % (date.strftime("%a %d.%m.%Y %H:%M:%S"), title))
+ print(" %s" % a.get_broadcast_subtitle(d, b))
+ print(" Broadcast: %s" % a.get_broadcast_url(d, b))
+ print(" Player: %s" % a.get_player_url(d, b))
+ print(" Program key: %s" % a.get_broadcast_pk(d, b))
+
if __name__ == "__main__":
try: