1313
1414PATTERN_CHANNEL = re .compile (r"\\\"channelId\\\":\\\"(.{24})\\\"" )
1515
16+ PATTERN_M_CHANNEL = re .compile (r"\"channelId\":\"(.{24})\"" )
17+
1618YT_VIDEO_ID_LENGTH = 11
1719
1820CLIENT_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:
100102def get_channelid (client , video_id ):
101103 resp = client .get ("https://www.youtube.com/embed/{}" .format (quote (video_id )), headers = config .headers )
102104 match = re .search (PATTERN_CHANNEL , resp .text )
105+ try :
106+ if match is None :
107+ raise IndexError
108+ ret = match .group (1 )
109+ except IndexError :
110+ ret = get_channelid_2nd (client , video_id )
111+ return ret
112+
113+
114+ def get_channelid_2nd (client , video_id ):
115+ resp = client .get ("https://m.youtube.com/watch?v={}" .format (quote (video_id )), headers = config .m_headers )
116+
117+ match = re .search (PATTERN_M_CHANNEL , resp .text )
103118 if match is None :
104119 raise InvalidVideoIdException (f"Cannot find channel id for video id:{ video_id } . This video id seems to be invalid." )
105120 try :
@@ -108,9 +123,21 @@ def get_channelid(client, video_id):
108123 raise InvalidVideoIdException (f"Invalid video id: { video_id } " )
109124 return ret
110125
126+
111127async def get_channelid_async (client , video_id ):
112128 resp = await client .get ("https://www.youtube.com/embed/{}" .format (quote (video_id )), headers = config .headers )
113129 match = re .search (PATTERN_CHANNEL , resp .text )
130+ try :
131+ if match is None :
132+ raise IndexError
133+ ret = match .group (1 )
134+ except IndexError :
135+ ret = await get_channelid_async_2nd (client , video_id )
136+ return ret
137+
138+ async def get_channelid_async_2nd (client , video_id ):
139+ resp = await client .get ("https://m.youtube.com/watch?v={}" .format (quote (video_id )), headers = config .m_headers )
140+ match = re .search (PATTERN_M_CHANNEL , resp .text )
114141 if match is None :
115142 raise InvalidVideoIdException (f"Cannot find channel id for video id:{ video_id } . This video id seems to be invalid." )
116143 try :
0 commit comments