This repository has been archived by the owner on Jan 18, 2021. It is now read-only.
forked from backingUpYouTube/backUpYouTubePyCollection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVideoIDHelper.py
203 lines (182 loc) · 5.16 KB
/
VideoIDHelper.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
from bs4 import BeautifulSoup
import re,requests
from datetime import datetime
def idExtractor(s):
#URL?
if isURL(s):
return getIDfromURL(s)
#Short URL?
elif isShortURL(s):
return getIDfromShortURL(s)
#Assume ID
else:
return s
def playlistIdExtractor(s):
#Direct Link
if isPlaylistUrl(s):
return getList(s)
#Video from playlist
elif isURL(s):
return getList(s)
#Video from playlist with short link
elif isShortURL(s):
return getList(s)
#Assume ID
else:
return s
def channelExtractor(s):
#Link to user
if isChannelURL(s):
#print('a')
#print(getUserFromUrl(s))
return getUserFromChannel(s)
#Assume ID
else:
#print('c')
return s
def userExtractor(s):
#Link to user
if isUserURL(s):
#print('a')
#print(getUserFromUrl(s))
return getUserFromUrl(s)
#Assume ID
else:
#print('c')
return s
def isURL(s):
return (s.find("www.youtube.com") != -1)
def isShortURL(s):
return (s.find("youtu.be") != -1)
def isPlaylistUrl(s):
return (s.find("www.youtube.com/playlist") != -1)
def isChannelId(s):
return ((s.find("UC") != -1 ) and (len(re.findall(r'UC[^&#\/]+',s)[0]) > 12 ))
def getChannelId(s):
#return s[s.find("UC"):]
try:
return re.findall(r'UC[^&#\/]+',s)[0]
except:
return ""
def getUserFromUrl(s):
splitUp=s.split('/')
#print(splitUp[splitUp.index('user')+1])
#print("GETTING USER:")
#print(s)
return splitUp[splitUp.index('user')+1]
def getUserFromChannel(s):
splitUp=s.split('/')
#print(splitUp[splitUp.index('user')+1])
#print("GETTING USER:")
#print(s)
return splitUp[splitUp.index('channel')+1]
def isUserURL(s):
return (s.find("www.youtube.com/user") != -1)
def isChannelURL(s):
return (s.find("www.youtube.com/channel") != -1)
def getIDfromURL(s):
try:
return re.findall(r'v=[^&#]+',s)[0][2:]
except:
return None
def getIDfromShortURL(s):
if(s.find("?")!=-1):
return s[ s.find("/", s.find("/")+2)+1 : s.find("?")]
return s[ s.find("/", s.find("/")+2)+1 : ]
def getList(s):
try:
return re.findall(r'list=[^&#]+',s)[0][5:]
except:
return None
"""
def getPLList(s):
try:
s=s[s.find('/playlist')+10:]
print(s)
return re.findall(r'list=[^&#]+',s)[0][2:]
except:
return None
"""
def channelIDInvalid(id):
if id.find('youtube.com') != -1 or id.find('youtu.be') != -1:
return True
r=requests.get("https://www.youtube.com/channel/{}".format(id))
if r.status_code != 200 and r.status_code != 404:
return False
if r.text.find("empty-channel-banner") != -1:
return True
return False
def channelUnavailable(id):
if id.find('youtube.com') != -1 or id.find('youtu.be') != -1:
return True
r=requests.get("https://www.youtube.com/user/{}".format(id))
if r.status_code != 200 and r.status_code != 404:
return False
if r.text.find("empty-channel-banner") != -1:
return True
return False
"""
def videoReallyUnavailable(id):
r=requests.get("http://youtu.be/{}".format(id))
#print (r.status_code)
if r.status_code == 404:
return True
if r.status_code == 200:
if r.text.find("unavailable-message") != -1:
return True
return False
return False
"""
def videoUnavailable(id):
r=requests.get("http://youtu.be/{}".format(id))
if r.status_code != 200:
return False
if r.text.find("watch-title") == -1:
return True
return False
def getVideoUser(id):
r=requests.get("http://youtu.be/{}".format(id))
if r.status_code != 200:
return ""
pageText = r.text
soup=BeautifulSoup(pageText,"html.parser")
for link in soup.select(".yt-user-info > a"):
#print("HERE YOU GO:\n"+link.decode_contents())
return ("https://www.youtube.com/user/{}".format(link.decode_contents()))
return ""
def getVideoDate(id):
r=requests.get("http://youtu.be/{}".format(id))
if r.status_code != 200:
return ""
pageText = r.text
soup=BeautifulSoup(pageText,"html.parser")
for link in soup.find_all("meta", itemprop="datePublished"):
return(link.get('content'))
return ""
#https://www.youtube.com/watch?v=HW-Jr4M4w90
def dateSearch(date,ids,start,end,getFirst):
#print(date,start,end)
if start==end:
return start
if start==end-1:
if getFirst:
return end
return start
#Query date if it fails return -1
midIndex=start + int((end-start)/2)
midQuery=getVideoDate(ids[midIndex])
if midQuery=="":
return -1
midDate=dateConvert(midQuery)
if getFirst:
if date > midDate:
return dateSearch(date,ids,start,midIndex,getFirst)
else:
return dateSearch(date,ids,midIndex,end,getFirst)
if date >= midDate:
return dateSearch(date,ids,start,midIndex,getFirst)
else:
return dateSearch(date,ids,midIndex,end,getFirst)
def dateConvert(s):
y,m,d = map(int, s.split('-'))
return datetime( y,m,d )