refactor: replace string concatenations with f-strings
All checks were successful
CI / test (push) Successful in 50s
All checks were successful
CI / test (push) Successful in 50s
This commit is contained in:
@@ -72,7 +72,7 @@ class TorManager:
|
||||
def __init__(self):
|
||||
self.old_tor_connection_pool = None
|
||||
self.tor_connection_pool = urllib3.contrib.socks.SOCKSProxyManager(
|
||||
'socks5h://127.0.0.1:' + str(settings.tor_port) + '/',
|
||||
f'socks5h://127.0.0.1:{settings.tor_port}/',
|
||||
cert_reqs='CERT_REQUIRED')
|
||||
self.tor_pool_refresh_time = time.monotonic()
|
||||
settings.add_setting_changed_hook(
|
||||
@@ -92,7 +92,7 @@ class TorManager:
|
||||
self.old_tor_connection_pool = self.tor_connection_pool
|
||||
|
||||
self.tor_connection_pool = urllib3.contrib.socks.SOCKSProxyManager(
|
||||
'socks5h://127.0.0.1:' + str(settings.tor_port) + '/',
|
||||
f'socks5h://127.0.0.1:{settings.tor_port}/',
|
||||
cert_reqs='CERT_REQUIRED')
|
||||
self.tor_pool_refresh_time = time.monotonic()
|
||||
|
||||
@@ -198,9 +198,9 @@ class HTTPAsymmetricCookieProcessor(urllib.request.BaseHandler):
|
||||
class FetchError(Exception):
|
||||
def __init__(self, code, reason='', ip=None, error_message=None):
|
||||
if error_message:
|
||||
string = code + ' ' + reason + ': ' + error_message
|
||||
string = f"{code} {reason}: {error_message}"
|
||||
else:
|
||||
string = 'HTTP error during request: ' + code + ' ' + reason
|
||||
string = f"HTTP error during request: {code} {reason}"
|
||||
Exception.__init__(self, string)
|
||||
self.code = code
|
||||
self.reason = reason
|
||||
@@ -294,14 +294,12 @@ def fetch_url_response(url, headers=(), timeout=15, data=None,
|
||||
exception_cause = e.__context__.__context__
|
||||
if (isinstance(exception_cause, socks.ProxyConnectionError)
|
||||
and settings.route_tor):
|
||||
msg = ('Failed to connect to Tor. Check that Tor is open and '
|
||||
'that your internet connection is working.\n\n'
|
||||
+ str(e))
|
||||
msg = f'Failed to connect to Tor. Check that Tor is open and that your internet connection is working.\n\n{e}'
|
||||
raise FetchError('502', reason='Bad Gateway',
|
||||
error_message=msg)
|
||||
elif isinstance(e.__context__,
|
||||
urllib3.exceptions.NewConnectionError):
|
||||
msg = 'Failed to establish a connection.\n\n' + str(e)
|
||||
msg = f'Failed to establish a connection.\n\n{e}'
|
||||
raise FetchError(
|
||||
'502', reason='Bad Gateway',
|
||||
error_message=msg)
|
||||
@@ -391,7 +389,7 @@ def fetch_url(url, headers=(), timeout=15, report_text=None, data=None,
|
||||
if error:
|
||||
raise FetchError(
|
||||
'429', reason=response.reason, ip=ip,
|
||||
error_message='Automatic circuit change: ' + error)
|
||||
error_message=f'Automatic circuit change: {error}')
|
||||
continue # retry with new identity
|
||||
|
||||
# Check for client errors (400, 404) - don't retry these
|
||||
@@ -467,10 +465,7 @@ def head(url, use_tor=False, report_text=None, max_redirects=10):
|
||||
headers = {'User-Agent': 'Python-urllib'}
|
||||
response = pool.request('HEAD', url, headers=headers, retries=retries)
|
||||
if report_text:
|
||||
print(
|
||||
report_text,
|
||||
' Latency:',
|
||||
round(time.monotonic() - start_time, 3))
|
||||
print(f'{report_text} Latency: {round(time.monotonic() - start_time, 3)}')
|
||||
return response
|
||||
|
||||
mobile_user_agent = 'Mozilla/5.0 (Linux; Android 7.0; Redmi Note 4 Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Mobile Safari/537.36'
|
||||
@@ -544,16 +539,16 @@ def download_thumbnail(save_directory, video_id):
|
||||
for quality in ('hq720.jpg', 'sddefault.jpg', 'hqdefault.jpg'):
|
||||
url = f'https://i.ytimg.com/vi/{video_id}/{quality}'
|
||||
try:
|
||||
thumbnail = fetch_url(url, report_text='Saved thumbnail: ' + video_id)
|
||||
thumbnail = fetch_url(url, report_text=f'Saved thumbnail: {video_id}')
|
||||
except FetchError as e:
|
||||
if '404' in str(e):
|
||||
continue
|
||||
print('Failed to download thumbnail for ' + video_id + ': ' + str(e))
|
||||
print(f'Failed to download thumbnail for {video_id}: {e}')
|
||||
return False
|
||||
except urllib.error.HTTPError as e:
|
||||
if e.code == 404:
|
||||
continue
|
||||
print('Failed to download thumbnail for ' + video_id + ': ' + str(e))
|
||||
print(f'Failed to download thumbnail for {video_id}: {e}')
|
||||
return False
|
||||
try:
|
||||
with open(save_location, 'wb') as f:
|
||||
@@ -563,7 +558,7 @@ def download_thumbnail(save_directory, video_id):
|
||||
with open(save_location, 'wb') as f:
|
||||
f.write(thumbnail)
|
||||
return True
|
||||
print('No thumbnail available for ' + video_id)
|
||||
print(f'No thumbnail available for {video_id}')
|
||||
return False
|
||||
|
||||
|
||||
@@ -698,7 +693,7 @@ def prefix_urls(item):
|
||||
|
||||
def add_extra_html_info(item):
|
||||
if item['type'] == 'video':
|
||||
item['url'] = (URL_ORIGIN + '/watch?v=' + item['id']) if item.get('id') else None
|
||||
item['url'] = f'{URL_ORIGIN}/watch?v={item["id"]}' if item.get('id') else None
|
||||
|
||||
video_info = {}
|
||||
for key in ('id', 'title', 'author', 'duration', 'author_id'):
|
||||
@@ -721,7 +716,7 @@ def add_extra_html_info(item):
|
||||
item['url'] = concat_or_none(URL_ORIGIN, "/channel/", item['id'])
|
||||
|
||||
if item.get('author_id') and 'author_url' not in item:
|
||||
item['author_url'] = URL_ORIGIN + '/channel/' + item['author_id']
|
||||
item['author_url'] = f'{URL_ORIGIN}/channel/{item["author_id"]}'
|
||||
|
||||
|
||||
def check_gevent_exceptions(*tasks):
|
||||
@@ -967,7 +962,7 @@ def call_youtube_api(client, api, data):
|
||||
user_agent = context['client'].get('userAgent') or mobile_user_agent
|
||||
visitor_data = get_visitor_data()
|
||||
|
||||
url = 'https://' + host + '/youtubei/v1/' + api + '?key=' + key
|
||||
url = f'https://{host}/youtubei/v1/{api}?key={key}'
|
||||
if visitor_data:
|
||||
context['client'].update({'visitorData': visitor_data})
|
||||
data['context'] = context
|
||||
@@ -978,8 +973,8 @@ def call_youtube_api(client, api, data):
|
||||
headers = ( *headers, ('X-Goog-Visitor-Id', visitor_data ))
|
||||
response = fetch_url(
|
||||
url, data=data, headers=headers,
|
||||
debug_name='youtubei_' + api + '_' + client,
|
||||
report_text='Fetched ' + client + ' youtubei ' + api
|
||||
debug_name=f'youtubei_{api}_{client}',
|
||||
report_text=f'Fetched {client} youtubei {api}'
|
||||
).decode('utf-8')
|
||||
return response
|
||||
|
||||
|
||||
Reference in New Issue
Block a user