diff options
author | James Taylor <user234683@users.noreply.github.com> | 2018-11-10 00:27:43 -0800 |
---|---|---|
committer | James Taylor <user234683@users.noreply.github.com> | 2018-11-10 00:27:43 -0800 |
commit | 717bf210937c757595624348db9389969be9654f (patch) | |
tree | 7814a81f5c0de1084ab46f3a88b6186798932dfa | |
parent | 9dd8308cf23de69c741f0304ace102718b844e16 (diff) | |
download | yt-local-717bf210937c757595624348db9389969be9654f.tar.lz yt-local-717bf210937c757595624348db9389969be9654f.tar.xz yt-local-717bf210937c757595624348db9389969be9654f.zip |
Can now store settings&data in ~/.youtube-local, keeping program files separate
-rw-r--r-- | server.py | 2 | ||||
-rw-r--r-- | settings.py | 79 | ||||
-rw-r--r-- | youtube/account_functions.py | 4 | ||||
-rw-r--r-- | youtube/local_playlist.py | 6 | ||||
-rw-r--r-- | youtube/watch.py | 3 | ||||
-rw-r--r-- | youtube/youtube.py | 3 |
6 files changed, 62 insertions, 35 deletions
@@ -82,7 +82,7 @@ def site_dispatch(env, start_response): yield error_code('403 Forbidden', start_response) return if "phpmyadmin" in path or (path == "/" and method == "HEAD"): - ban_address(client_address) + #ban_address(client_address) start_response('403 Fuck Off', ()) yield b'403 Fuck Off' return diff --git a/settings.py b/settings.py index 32bb1f5..83dc939 100644 --- a/settings.py +++ b/settings.py @@ -1,4 +1,7 @@ import ast +import re +import os + default_settings = '''route_tor = False port_number = 80 allow_foreign_addresses = False @@ -28,40 +31,58 @@ allowed_targets = set(("route_tor", "port_number", "allow_foreign_addresses", "s def log_ignored_line(line_number, message): print("settings.txt: Ignoring line " + str(node.lineno) + " (" + message + ")") + +if os.path.isfile("settings.txt"): + print("Running in portable mode") + settings_dir = os.path.normpath('./') + data_dir = os.path.normpath('./data') +else: + print("Running in non-portable mode") + settings_dir = os.path.expanduser(os.path.normpath("~/.youtube-local")) + data_dir = os.path.expanduser(os.path.normpath("~/.youtube-local/data")) + if not os.path.exists(settings_dir): + os.makedirs(settings_dir) + + + try: - with open('settings.txt', 'r', encoding='utf-8') as file: + with open(os.path.join(settings_dir, 'settings.txt'), 'r', encoding='utf-8') as file: settings_text = file.read() except FileNotFoundError: - with open('settings.txt', 'a', encoding='utf-8') as file: + with open(os.path.join(settings_dir, 'settings.txt'), 'a', encoding='utf-8') as file: file.write(default_settings) else: - attributes = { - ast.NameConstant: 'value', - ast.Num: 'n', - ast.Str: 's', - } - module_node = ast.parse(settings_text) - for node in module_node.body: - if type(node) != ast.Assign: - log_ignored_line(node.lineno, "only assignments are allowed") - continue - - if len(node.targets) > 1: - log_ignored_line(node.lineno, "only simple single-variable assignments allowed") - continue + if re.fullmatch(r'\s*', settings_text): # blank file + with open(os.path.join(settings_dir, 'settings.txt'), 'a', encoding='utf-8') as file: + file.write(default_settings) + else: + attributes = { + ast.NameConstant: 'value', + ast.Num: 'n', + ast.Str: 's', + } + module_node = ast.parse(settings_text) + for node in module_node.body: + if type(node) != ast.Assign: + log_ignored_line(node.lineno, "only assignments are allowed") + continue + + if len(node.targets) > 1: + log_ignored_line(node.lineno, "only simple single-variable assignments allowed") + continue - target = node.targets[0] - if type(target) != ast.Name: - log_ignored_line(node.lineno, "only simple single-variable assignments allowed") - continue - - if target.id not in allowed_targets: - log_ignored_line(node.lineno, "target is not a valid setting") - continue - - if type(node.value) not in (ast.NameConstant, ast.Num, ast.Str): - log_ignored_line(node.lineno, "only literals allowed for values") - continue + target = node.targets[0] + if type(target) != ast.Name: + log_ignored_line(node.lineno, "only simple single-variable assignments allowed") + continue + + if target.id not in allowed_targets: + log_ignored_line(node.lineno, "target is not a valid setting") + continue + + if type(node.value) not in (ast.NameConstant, ast.Num, ast.Str): + log_ignored_line(node.lineno, "only literals allowed for values") + continue - locals()[target.id] = node.value.__getattribute__(attributes[type(node.value)]) + locals()[target.id] = node.value.__getattribute__(attributes[type(node.value)])
\ No newline at end of file diff --git a/youtube/account_functions.py b/youtube/account_functions.py index f34e5f2..0c7973b 100644 --- a/youtube/account_functions.py +++ b/youtube/account_functions.py @@ -5,6 +5,8 @@ import json from youtube import common, proto, comments import re import traceback +import settings +import os def _post_comment(text, video_id, session_token, cookie): headers = { @@ -103,7 +105,7 @@ def delete_comment(video_id, comment_id, author_id, session_token, cookie): xsrf_token_regex = re.compile(r'''XSRF_TOKEN"\s*:\s*"([\w-]*(?:=|%3D){0,2})"''') def post_comment(query_string, fields): - with open('data/cookie.txt', 'r', encoding='utf-8') as f: + with open(os.path.join(settings.data_dir, 'cookie.txt'), 'r', encoding='utf-8') as f: cookie_data = f.read() parameters = urllib.parse.parse_qs(query_string) diff --git a/youtube/local_playlist.py b/youtube/local_playlist.py index 76e7ee6..715015e 100644 --- a/youtube/local_playlist.py +++ b/youtube/local_playlist.py @@ -5,9 +5,11 @@ from youtube import common import html import gevent import urllib +import settings + +playlists_directory = os.path.join(settings.data_dir, "playlists") +thumbnails_directory = os.path.join(settings.data_dir, "playlist_thumbnails") -playlists_directory = os.path.normpath("data/playlists") -thumbnails_directory = os.path.normpath("data/playlist_thumbnails") with open('yt_local_playlist_template.html', 'r', encoding='utf-8') as file: local_playlist_template = Template(file.read()) diff --git a/youtube/watch.py b/youtube/watch.py index 6ed0878..0f70635 100644 --- a/youtube/watch.py +++ b/youtube/watch.py @@ -9,6 +9,7 @@ from youtube.common import default_multi_get, get_thumbnail_url, video_id, URL_O import youtube.comments as comments import gevent import settings +import os video_height_priority = (360, 480, 240, 720, 1080) @@ -305,7 +306,7 @@ def get_watch_page(query_string): music_list_html += '''</tr>\n''' music_list_html += '''</table>\n''' if settings.gather_googlevideo_domains: - with open('data/googlevideo-domains.txt', 'a+', encoding='utf-8') as f: + with open(os.path.join(settings.data_dir, 'googlevideo-domains.txt'), 'a+', encoding='utf-8') as f: url = info['formats'][0]['url'] subdomain = url[0:url.find(".googlevideo.com")] f.write(subdomain + "\n") diff --git a/youtube/youtube.py b/youtube/youtube.py index 1a97b6c..5451254 100644 --- a/youtube/youtube.py +++ b/youtube/youtube.py @@ -1,5 +1,6 @@ import mimetypes import urllib.parse +import os from youtube import local_playlist, watch, search, playlist, channel, comments, common, account_functions import settings YOUTUBE_FILES = ( @@ -49,7 +50,7 @@ def youtube(env, start_response): return local_playlist.get_playlist_page(path[10:], query_string=query_string).encode() elif path.startswith("/data/playlist_thumbnails/"): - with open(path[1:], 'rb') as f: + with open(os.path.join(settings.data_dir, os.path.normpath(path[6:])), 'rb') as f: start_response('200 OK', (('Content-type', "image/jpeg"),) ) return f.read() |