Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

settings.py: add 'proxy_images' #24

Merged
merged 6 commits into from
Oct 12, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@
'comment': '',
}),

('proxy_images', {
'label': 'proxy images',
'type': bool,
'default': True,
'comment': '',
}),

('theme', {
'type': int,
'default': 0,
Expand Down Expand Up @@ -257,7 +264,7 @@ def log_ignored_line(line_number, message):
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
Expand All @@ -266,11 +273,11 @@ def log_ignored_line(line_number, message):
if type(target) != ast.Name:
log_ignored_line(node.lineno, "only simple single-variable assignments allowed")
continue

if target.id not in acceptable_targets:
log_ignored_line(node.lineno, target.id + " is not a valid setting")
continue

if type(node.value) not in attributes:
log_ignored_line(node.lineno, "only literals allowed for values")
continue
Expand Down Expand Up @@ -298,6 +305,11 @@ def log_ignored_line(line_number, message):
globals().update(current_settings_dict)


if proxy_images:
img_prefix = "/"
else:
img_prefix = ""



if route_tor:
Expand Down
4 changes: 2 additions & 2 deletions youtube/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def post_process_comments_info(comments_info):
comment['author_url'] = concat_or_none(
util.URL_ORIGIN, comment['author_url'])
comment['author_avatar'] = concat_or_none(
'/', comment['author_avatar'])
settings.img_prefix, comment['author_avatar'])

comment['permalink'] = concat_or_none(util.URL_ORIGIN, '/watch?v=',
comments_info['video_id'], '&lc=', comment['id'])
Expand Down Expand Up @@ -139,7 +139,7 @@ def post_process_comments_info(comments_info):

comments_info['video_url'] = concat_or_none(util.URL_ORIGIN,
'/watch?v=', comments_info['video_id'])
comments_info['video_thumbnail'] = concat_or_none('/i.ytimg.com/vi/',
comments_info['video_thumbnail'] = concat_or_none(settings.img_prefix, 'https://i.ytimg.com/vi/',
comments_info['video_id'], '/mqdefault.jpg')


Expand Down
4 changes: 2 additions & 2 deletions youtube/local_playlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def add_to_playlist(name, video_info_list):
file.write(info + "\n")
missing_thumbnails.append(id)
gevent.spawn(util.download_thumbnails, os.path.join(thumbnails_directory, name), missing_thumbnails)


def get_local_playlist_videos(name, offset=0, amount=50):
try:
Expand All @@ -52,7 +52,7 @@ def get_local_playlist_videos(name, offset=0, amount=50):
try:
info = json.loads(video_json)
if info['id'] + ".jpg" in thumbnails:
info['thumbnail'] = "/youtube.com/data/playlist_thumbnails/" + name + "/" + info['id'] + ".jpg"
info['thumbnail'] = settings.img_prefix + "https://youtube.com/data/playlist_thumbnails/" + name + "/" + info['id'] + ".jpg"
user234683 marked this conversation as resolved.
Show resolved Hide resolved
else:
info['thumbnail'] = util.get_thumbnail_url(info['id'])
missing_thumbnails.append(info['id'])
Expand Down
13 changes: 7 additions & 6 deletions youtube/playlist.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from youtube import util, yt_data_extract, proto
from youtube import yt_app
import settings

import base64
import urllib
Expand All @@ -14,15 +15,15 @@



def playlist_ctoken(playlist_id, offset):
def playlist_ctoken(playlist_id, offset):

offset = proto.uint(1, offset)
# this is just obfuscation as far as I can tell. It doesn't even follow protobuf
offset = b'PT:' + proto.unpadded_b64encode(offset)
offset = proto.string(15, offset)

continuation_info = proto.string( 3, proto.percent_b64encode(offset) )

playlist_id = proto.string(2, 'VL' + playlist_id )
pointless_nest = proto.string(80226972, playlist_id + continuation_info)

Expand Down Expand Up @@ -51,7 +52,7 @@ def playlist_first_page(playlist_id, report_text = "Retrieved playlist"):
content = json.loads(util.uppercase_escape(content.decode('utf-8')))

return content


#https://m.youtube.com/playlist?itct=CBMQybcCIhMIptj9xJaJ2wIV2JKcCh3Idwu-&ctoken=4qmFsgI2EiRWTFBMT3kwajlBdmxWWlB0bzZJa2pLZnB1MFNjeC0tN1BHVEMaDmVnWlFWRHBEUWxFJTNE&pbj=1
def get_videos(playlist_id, page):
Expand Down Expand Up @@ -84,7 +85,7 @@ def get_playlist_page():
this_page_json = first_page_json
else:
tasks = (
gevent.spawn(playlist_first_page, playlist_id, report_text="Retrieved playlist info" ),
gevent.spawn(playlist_first_page, playlist_id, report_text="Retrieved playlist info" ),
gevent.spawn(get_videos, playlist_id, page)
)
gevent.joinall(tasks)
Expand All @@ -103,7 +104,7 @@ def get_playlist_page():
util.prefix_urls(item)
util.add_extra_html_info(item)
if 'id' in item:
item['thumbnail'] = '/https://i.ytimg.com/vi/' + item['id'] + '/default.jpg'
item['thumbnail'] = f'{settings.img_prefix}https://i.ytimg.com/vi/' + item['id'] + '/default.jpg'
user234683 marked this conversation as resolved.
Show resolved Hide resolved

item['url'] += '&list=' + playlist_id
if item['index']:
Expand Down
1 change: 1 addition & 0 deletions youtube/subscriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,7 @@ def get_subscriptions_page():
videos, number_of_videos_in_db = _get_videos(cursor, 60, (page - 1)*60, tag)
for video in videos:
video['thumbnail'] = util.URL_ORIGIN + '/data/subscription_thumbnails/' + video['id'] + '.jpg'
if not settings.proxy_images: video['thumbnail'] = video['thumbnail'][1:]
user234683 marked this conversation as resolved.
Show resolved Hide resolved
video['type'] = 'video'
video['item_size'] = 'small'
util.add_extra_html_info(video)
Expand Down
4 changes: 3 additions & 1 deletion youtube/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
<head>
<meta charset="utf-8">
<title>{{ page_title }}</title>
<meta http-equiv="Content-Security-Policy" content="default-src 'self' 'unsafe-inline'; media-src 'self' https://*.googlevideo.com">
{% if settings.proxy_images %}
<meta http-equiv="Content-Security-Policy" content="default-src 'self' 'unsafe-inline'; media-src 'self' https://*.googlevideo.com">
user234683 marked this conversation as resolved.
Show resolved Hide resolved
{% endif %}
<link href="{{ theme_path }}" type="text/css" rel="stylesheet">
<link href="/youtube.com/static/shared.css" type="text/css" rel="stylesheet">
<link href="/youtube.com/static/comments.css" type="text/css" rel="stylesheet">
Expand Down
13 changes: 7 additions & 6 deletions youtube/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,8 @@ def video_id(url):

# default, sddefault, mqdefault, hqdefault, hq720
def get_thumbnail_url(video_id):
return "/i.ytimg.com/vi/" + video_id + "/mqdefault.jpg"
return f"{settings.img_prefix}https://i.ytimg.com/vi/" + video_id + "/mqdefault.jpg"
user234683 marked this conversation as resolved.
Show resolved Hide resolved

def seconds_to_timestamp(seconds):
seconds = int(seconds)
hours, seconds = divmod(seconds,3600)
Expand Down Expand Up @@ -394,10 +394,11 @@ def concat_or_none(*strings):


def prefix_urls(item):
try:
item['thumbnail'] = prefix_url(item['thumbnail'])
except KeyError:
pass
if settings.proxy_images:
try:
item['thumbnail'] = prefix_url(item['thumbnail'])
except KeyError:
pass

try:
item['author_url'] = prefix_url(item['author_url'])
Expand Down