| @@ -14,6 +14,17 @@ import concurrent.futures | |||
| import ntpath | |||
| import yaml | |||
| from pathlib import Path | |||
| import logging | |||
| from systemd.journal import JournalHandler | |||
| log = logging.getLogger('demotape') | |||
| log.addHandler(JournalHandler()) | |||
| log.setLevel(logging.INFO) | |||
| config_path = Path(__file__).parent / './config.yaml' | |||
| with config_path.open() as file: | |||
| @@ -22,13 +33,13 @@ with config_path.open() as file: | |||
| try: | |||
| if sys.argv[1] and os.path.exists(sys.argv[1]): | |||
| ROOT_PATH = sys.argv[1] | |||
| print('Root path for downloaded streams: ' + ROOT_PATH) | |||
| log('Root path for downloaded streams: ' + ROOT_PATH) | |||
| else: | |||
| print('destination path does not exist') | |||
| log('destination path does not exist') | |||
| sys.exit() | |||
| except IndexError: | |||
| print('Script needs a valid destination path for recorded videos as argument') | |||
| print('For example: \ndemotape.py /path/to/videos') | |||
| log('Script needs a valid destination path for recorded videos as argument') | |||
| log('For example: \ndemotape.py /path/to/videos') | |||
| sys.exit() | |||
| @@ -50,9 +61,9 @@ def generate_channellist(): | |||
| 'url': 'https://stream.wien.gv.at/live/ngrp:bv' + district_str_lz + '.stream_all/playlist.m3u8' | |||
| } | |||
| channels.append(channel) | |||
| print('channels:') | |||
| log('channels:') | |||
| for channel in channels: | |||
| print(channel['name'] + ' ' + channel['url']) | |||
| log(channel['name'] + ' ' + channel['url']) | |||
| return channels | |||
| @@ -66,33 +77,33 @@ def check_stream(url): | |||
| # no livestream | |||
| return False | |||
| except (ValueError, KeyError): | |||
| print('some connection error or so') | |||
| log('some connection error or so') | |||
| class MyLogger(object): | |||
| def debug(self, msg): | |||
| #pass | |||
| print(msg) | |||
| log(msg) | |||
| def warning(self, msg): | |||
| #pass | |||
| print(msg) | |||
| log(msg) | |||
| def error(self, msg): | |||
| print(msg) | |||
| log(msg) | |||
| def my_ytdl_hook(d): | |||
| if d['status'] == 'finished': | |||
| print(timestamp() + 'Done downloading!') | |||
| log(timestamp() + 'Done downloading!') | |||
| else: | |||
| print(timestamp() + 'sth went wrong' + d['status']) | |||
| print(d) | |||
| log(timestamp() + 'sth went wrong' + d['status']) | |||
| log(d) | |||
| def download_stream(channel, dest_path): | |||
| print('download_stream') | |||
| log('download_stream') | |||
| ytdl_opts = { | |||
| 'logger': MyLogger(), | |||
| 'outtmpl': dest_path, | |||
| @@ -111,42 +122,42 @@ def download_stream(channel, dest_path): | |||
| ytdl = youtube_dl.YoutubeDL(ytdl_opts) | |||
| try: | |||
| print(timestamp() + " Downloading: " + channel['url']) | |||
| log(timestamp() + " Downloading: " + channel['url']) | |||
| ytdl.download([channel['url']]) | |||
| except (youtube_dl.utils.DownloadError) as e: | |||
| print(timestamp() + " Download error: " + str(e)) | |||
| log(timestamp() + " Download error: " + str(e)) | |||
| except (youtube_dl.utils.SameFileError) as e: | |||
| print("Download error: " + str(e)) | |||
| log("Download error: " + str(e)) | |||
| except (UnicodeDecodeError) as e: | |||
| print("UnicodeDecodeError: " + str(e)) | |||
| log("UnicodeDecodeError: " + str(e)) | |||
| def process_channel(channel): | |||
| #print('entered function process_channel with ' + channel['name']) | |||
| #log('entered function process_channel with ' + channel['name']) | |||
| while True: | |||
| print(timestamp() + ' checking ' + channel['name']) | |||
| log(timestamp() + ' checking ' + channel['name']) | |||
| if check_stream(channel['url']): | |||
| print(channel['name'] + ': stream online! Downloading ...') | |||
| log(channel['name'] + ': stream online! Downloading ...') | |||
| dest_dir = ROOT_PATH + '/' + channel['name'] +'/' | |||
| # create directory if it doesn't exist | |||
| if not os.path.exists(dest_dir): | |||
| print('creating directory ' + dest_dir) | |||
| log('creating directory ' + dest_dir) | |||
| os.makedirs(dest_dir) | |||
| dest_path = get_destpath(channel) # dirctory + filename | |||
| download_stream(channel, dest_path) # also converts video | |||
| print(timestamp() + " Uploading video " + dest_path) | |||
| log(timestamp() + " Uploading video " + dest_path) | |||
| upload_video(dest_path) | |||
| else: | |||
| waitingtime = random.randint(50,60) | |||
| time.sleep(waitingtime) | |||
| print('end processing ' + channel['name'] + ' ... (shouldn\'t happen!)') | |||
| log('end processing ' + channel['name'] + ' ... (shouldn\'t happen!)') | |||
| def upload_video(videofile_path): | |||
| print('uploading %s' % (videofile_path)) | |||
| log('uploading %s' % (videofile_path)) | |||
| credentials = config['webdav']['username'] + ':' + config['webdav']['password'] | |||
| webdav_baseurl = config['webdav']['base_url'] | |||
| filename = ntpath.basename(videofile_path) | |||
| @@ -158,7 +169,7 @@ def upload_video(videofile_path): | |||
| delete_video(videofile_path) | |||
| return true | |||
| except: | |||
| print('Error while uploading %s to %s' % (file, webdav_url)) | |||
| log('Error while uploading %s to %s' % (file, webdav_url)) | |||
| def delete_video(file): | |||
| @@ -166,7 +177,7 @@ def delete_video(file): | |||
| os.system('rm -rf "%s"' % (file)) | |||
| return true | |||
| except: | |||
| print('Error while deleting %s' % (file)) | |||
| log('Error while deleting %s' % (file)) | |||
| def get_destpath(channel): | |||
| @@ -187,12 +198,12 @@ def main(): | |||
| try: | |||
| data = future.result() | |||
| except Exception as exc: | |||
| print('%r generated an exception: %s' % (channel, exc)) | |||
| log('%r generated an exception: %s' % (channel, exc)) | |||
| else: | |||
| print('%r page is %d bytes' % (channel, len(data))) | |||
| log('%r page is %d bytes' % (channel, len(data))) | |||
| print('end main (this shouldn\'t happen!)') | |||
| log('end main (this shouldn\'t happen!)') | |||
| main() | |||