feat: Allow users to specify a cookie when querying youtube.

See #263.
This commit is contained in:
Terry Geng 2021-04-23 23:01:20 +08:00
parent b7232b8bae
commit 61748c199a
No known key found for this signature in database
GPG Key ID: F982F8EA1DF720E7
3 changed files with 11 additions and 1 deletions

View File

@ -13,6 +13,7 @@ import util
import variables as var
from pyradios import RadioBrowser
from database import SettingsDatabase, MusicDatabase, Condition
import media.playlist
from media.item import item_id_generators, dict_to_item, dicts_to_items, ValidationFailedError
from media.cache import get_cached_wrapper_from_scrap, get_cached_wrapper_by_id, get_cached_wrappers_by_tags, \
get_cached_wrapper, get_cached_wrappers, get_cached_wrapper_from_dict, get_cached_wrappers_from_dicts

View File

@ -116,6 +116,13 @@ port = 64738
# - leave empty (do nothing)
#when_nobody_in_channel =
# 'youtube_query_cookie': Sometimes youtube will block the request of our bot and
# request the bot to complete a captcha to verify the request is not made by a
# bot.
# This problem can be resolved if the bot bears a valid cookie.
# If the bot complains "unable to query youtube", you may try to add your cookie here.
#youtube_query_cookie = {'CONSENT': 'paste your CONSENT cookie value here'}
# [webinterface] stores settings related to the web interface.
[webinterface]
# 'enable': Set 'enabled' to True if you'd like to use the web interface to manage

View File

@ -321,7 +321,9 @@ def youtube_search(query):
import json
try:
r = requests.get("https://www.youtube.com/results", params={'search_query': query}, timeout=5)
cookie = json.loads(var.config.get('bot', 'youtube_query_cookie', default='{}'))
r = requests.get("https://www.youtube.com/results", cookie=cookie,
params={'search_query': query}, timeout=5)
result_json_match = re.findall(r">var ytInitialData = (.*?);</script>", r.text)
if not len(result_json_match):