Skip to content
This repository was archived by the owner on Jan 25, 2022. It is now read-only.

Commit e865c25

Browse files
committed
Merge branch 'develop'
2 parents 0dce1b0 + 6d581c2 commit e865c25

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

pytchat/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
pytchat is a lightweight python library to browse youtube livechat without Selenium or BeautifulSoup.
33
"""
44
__copyright__ = 'Copyright (C) 2019, 2020 taizan-hokuto'
5-
__version__ = '0.5.3'
5+
__version__ = '0.5.4'
66
__license__ = 'MIT'
77
__author__ = 'taizan-hokuto'
88
__author_email__ = '55448286+taizan-hokuto@users.noreply.github.com'

pytchat/config/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
headers = {
55
'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)',
66
}
7-
7+
m_headers = {
8+
'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',
9+
}
810
_sml = dc(b"BQS?8F#ks-GB\\6`H#IhIF^eo7@rH3;H#IhIF^eor06T''Ch\\'(?XmbXF>%9<FC/iuG%G#jBOQ!ICLqcS5tQB2;gCZ)?UdXC;f$GR3)MM2<(0>O7mh!,G@+K5?SO9T@okV").decode()
911
_smr = dc(b"BQS?8F#ks-GB\\6`H#IhIF^eo7@rH3;H#IhIF^eor06T''Ch\\'(?XmbXF>%9<FC/iuG%G#jBOQ!iEb03+@<k(QAU-F)8U=fDGsP557S5F7CiNH7;)D3N77^*B6YU@\\?WfBr0emZX=#^").decode()
1012

pytchat/util/__init__.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
PATTERN_CHANNEL = re.compile(r"\\\"channelId\\\":\\\"(.{24})\\\"")
1515

16+
PATTERN_M_CHANNEL = re.compile(r"\"channelId\":\"(.{24})\"")
17+
1618
YT_VIDEO_ID_LENGTH = 11
1719

1820
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:
100102
def 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+
111127
async 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

Comments
 (0)