diff --git a/pytchat/config/__init__.py b/pytchat/config/__init__.py index 4f26a1e..f9909b5 100644 --- a/pytchat/config/__init__.py +++ b/pytchat/config/__init__.py @@ -4,7 +4,9 @@ from base64 import a85decode as dc headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36 Edg/86.0.622.63,gzip(gfe)', } - +m_headers = { + 'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Mobile Safari/537.36 Edg/91.0.864.59', +} _sml = dc(b"BQS?8F#ks-GB\\6`H#IhIF^eo7@rH3;H#IhIF^eor06T''Ch\\'(?XmbXF>%9O7mh!,G@+K5?SO9T@okV").decode() _smr = dc(b"BQS?8F#ks-GB\\6`H#IhIF^eo7@rH3;H#IhIF^eor06T''Ch\\'(?XmbXF>%9 str: def get_channelid(client, video_id): resp = client.get("https://www.youtube.com/embed/{}".format(quote(video_id)), headers=config.headers) match = re.search(PATTERN_CHANNEL, resp.text) + try: + if match is None: + raise IndexError + ret = match.group(1) + except IndexError: + ret = get_channelid_2nd(client, video_id) + return ret + + +def get_channelid_2nd(client, video_id): + resp = client.get("https://m.youtube.com/watch?v={}".format(quote(video_id)), headers=config.m_headers) + + match = re.search(PATTERN_M_CHANNEL, resp.text) if match is None: raise InvalidVideoIdException(f"Cannot find channel id for video id:{video_id}. This video id seems to be invalid.") try: @@ -108,9 +123,21 @@ def get_channelid(client, video_id): raise InvalidVideoIdException(f"Invalid video id: {video_id}") return ret + async def get_channelid_async(client, video_id): resp = await client.get("https://www.youtube.com/embed/{}".format(quote(video_id)), headers=config.headers) match = re.search(PATTERN_CHANNEL, resp.text) + try: + if match is None: + raise IndexError + ret = match.group(1) + except IndexError: + ret = await get_channelid_async_2nd(client, video_id) + return ret + +async def get_channelid_async_2nd(client, video_id): + resp = await client.get("https://m.youtube.com/watch?v={}".format(quote(video_id)), headers=config.m_headers) + match = re.search(PATTERN_M_CHANNEL, resp.text) if match is None: raise InvalidVideoIdException(f"Cannot find channel id for video id:{video_id}. This video id seems to be invalid.") try: