-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl.py
64 lines (49 loc) · 1.89 KB
/
url.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from bs4 import BeautifulSoup
import requests
import json
WATCH_URL="https://www.youtube.com/watch?v="
USER_AGENT="Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.3"
CACHE_FILE="cache.json"
def getCachedIds()->dict:
try:
with open(CACHE_FILE,'r') as cachefile:
return json.loads(cachefile.read())
except:
return {}
def writeCachedId(channelName:str,channelId:str,cache:dict)->dict:
cache[channelName]=channelId
with open(CACHE_FILE,'w') as cachefile:
cachefile.write(json.dumps(cache,indent=4))
def getChannelId(channelName:str)->str:
cache=getCachedIds()
channel_url=f"https://www.youtube.com/@{channelName}"
if channelName in cache:
return cache[channelName]
response=requests.get(channel_url,headers={
"User-Agent":USER_AGENT
})
if response.status_code!=200:
print(f"Something went wrong with the fetching the channel id,{response.status_code}")
soup=BeautifulSoup(response.content,'html.parser')
try:
channelId=soup.select('meta[itemprop*=identifier]')[0]['content']
writeCachedId(channelName,channelId,cache)
return channelId
except Exception as e:
print(e)
return None
def getLiveUrl(channelName:str)->str:
channelId=getChannelId(channelName)
if not channelId:
raise ValueError("channelId is not useable")
live_url=f"https://www.youtube.com/channel/{channelId}/live"
response=requests.get(live_url,headers={
"User-Agent":USER_AGENT
})
if response.status_code!=200:
print(f"Something went wrong with the getting the live url,{response.status_code}")
soup=BeautifulSoup(response.content,'html.parser')
for element in soup.select('link[rel*=canonical]'):
if WATCH_URL in element['href']:
return element['href']
return None