X-Git-Url: https://git.sthu.org/?a=blobdiff_plain;f=oe1archive.py;h=de4d412a88cdd1b97a09126d1b61d0166b6cf217;hb=209fa89db7684bc721bd9b30a8d00eeb1f6e651a;hp=5a8e397a3d558dab9104d1db17028fdd4d841c64;hpb=5924be30bd64b943cb33a927f26316477a43da1c;p=shutils.git
diff --git a/oe1archive.py b/oe1archive.py
index 5a8e397..de4d412 100755
--- a/oe1archive.py
+++ b/oe1archive.py
@@ -1,7 +1,8 @@
#!/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"
@@ -11,6 +12,7 @@ import dateutil.parser
import sys
import getopt
import re
+import os
class Archive:
@@ -28,9 +30,18 @@ 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']
@@ -46,6 +57,21 @@ class Archive:
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 + "
" + akm;
+
def get_broadcasts_by_regex(self, key):
rex = re.compile(key, re.IGNORECASE)
@@ -99,6 +125,7 @@ def screen_choose():
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:")
@@ -109,21 +136,101 @@ def screen_choose():
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("\n")
+ file.write("\n")
+ file.write("