Enhance channelID extraction
This commit is contained in:
@@ -13,6 +13,8 @@ PATTERN_YTURL = re.compile(r"((?<=(v|V)/)|(?<=be/)|(?<=(\?|\&)v=)|(?<=embed/))([
|
||||
|
||||
PATTERN_CHANNEL = re.compile(r"\\\"channelId\\\":\\\"(.{24})\\\"")
|
||||
|
||||
PATTERN_M_CHANNEL = re.compile(r"\"channelId\":\"(.{24})\"")
|
||||
|
||||
YT_VIDEO_ID_LENGTH = 11
|
||||
|
||||
CLIENT_VERSION = ''.join(("2.", (datetime.datetime.today() - datetime.timedelta(days=1)).strftime("%Y%m%d"), ".01.00"))
|
||||
@@ -100,6 +102,19 @@ def extract_video_id(url_or_id: str) -> 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:
|
||||
|
||||
Reference in New Issue
Block a user