Can now store settings&data in ~/.youtube-local, keeping program files separate

This commit is contained in:
James Taylor 2018-11-10 00:27:43 -08:00
parent 9dd8308cf2
commit 717bf21093
6 changed files with 62 additions and 35 deletions

View File

@ -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

View File

@ -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 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
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
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 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
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)])

View File

@ -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)

View File

@ -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())

View File

@ -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")

View File

@ -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()