-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYoutubeVideouSrcCrawler.py
280 lines (225 loc) · 7.91 KB
/
YoutubeVideouSrcCrawler.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import urllib
import urllib2
import sys
import time
import argparse
from os import path
from urlparse import parse_qs
from urlparse import urlparse
from urllib2 import URLError
import dns.resolver
from bs4 import BeautifulSoup
import threading
from Queue import Queue
__author__ = (
'YU \'Johnny\' ZHOU'
)
class VideoInfo(object):
"""
VideoInfo Class hold all information retrieved from www.youtube.com/get_video_info?video_id=
[VIDEO_ID]
"""
def __init__(self, video_url):
request_url = 'http://www.youtube.com/get_video_info?video_id='
if 'http://www.youtube.com/watch?v' in parse_qs(video_url).keys():
request_url += parse_qs(video_url)['http://www.youtube.com/watch?v'][0]
elif 'https://www.youtube.com/watch?v' in parse_qs(video_url).keys():
request_url = 'https://www.youtube.com/get_video_info?video_id='+parse_qs(video_url)['https://www.youtube.com/watch?v'][0]
elif 'v' in parse_qs(video_url).keys():
request_url += parse_qs(video_url)['v'][0]
else :
sys.exit('Error : Invalid Youtube URL Passing %s' % video_url)
request = urllib2.Request(request_url)
try:
self.video_info = parse_qs(urllib2.urlopen(request).read())
except URLError :
sys.exit('Error : Invalid Youtube URL Passing %s' % video_url)
def thumbnail_url(videoinfo):
"""
extract thumbnail's url from VideoInfo object and return its url
"""
if not isinstance(videoinfo, VideoInfo):
sys.exit('Error : method (thumbnail_url) invalid argument passing')
return urllib.unquote_plus(videoinfo.video_info['thumbnail_url'][0])
def title(videoinfo):
"""
extract title from VideoInfo object
"""
if not isinstance(videoinfo, VideoInfo):
sys.exit('Error : method (title) invalid argument passing')
title = videoinfo.video_info['title'][0].decode('utf-8')
return title
def video_file_urls(videoinfo):
"""
extract video file's url from VideoInfo object and return them
"""
if not isinstance(videoinfo, VideoInfo):
sys.exit('Error : method(video_file_urls) invalid argument passing')
url_encoded_fmt_stream_map = videoinfo.video_info['url_encoded_fmt_stream_map'][0].split(',')
entrys = [parse_qs(entry) for entry in url_encoded_fmt_stream_map]
url_maps = [dict(url=entry['url'][0], type=entry['type']) for entry in entrys]
return url_maps
def downloader(url, filename, prefix_message=''):
"""
download file from url
"""
if path.exists(filename):
sys.exit('Error : file already exists')
request=urllib2.Request(url)
file=open(filename, 'wb')
link=urllib2.urlopen(request)
meta=link.info()
filesize=int(meta.getheader('Content-Length'))
size_message = 'downloading file size is %d byte\n' %(filesize)
buff_size=16384
downloaded_size=0
sys.stdout.write(prefix_message+'\n'+size_message+'\n')
sys.stdout.flush()
while True:
buffer = link.read(buff_size)
if not buffer:
break
downloaded_size += len(buffer)
file.write(buffer)
display = '%s .............. %d / %d' %(filename, downloaded_size, filesize)
sys.stdout.write("\r"+display)
sys.stdout.flush()
time.sleep(1)
sys.stdout.write('\n')
sys.stdout.flush()
file.close()
def __getFileExtension(type):
if type.lower() == 'video/webm':
return 'webm'
if type.lower() == 'video/mp4':
return 'mp4'
if type.lower() == 'video/3gpp':
return '3gp'
if type.lower() == 'video/x-flv':
return 'flv'
return None
def __getFileType(extension):
if extension.lower() == 'webm':
return 'video/webm'
if extension.lower() == 'mp4':
return 'video/mp4'
if extension.lower() == '3gp':
return 'video/3gpp'
if extension.lower() == 'flv':
return 'video/x-flv'
return None
def __getFileName(videoinfo, type):
if not isinstance(videoinfo, VideoInfo):
sys.exit('Error : method(__getFileName) invalid argument passing')
filename = title(videoinfo)+'.'+type
return filename
completed_url = set([])
added_url = set([])
ip_count = {}
lineNum = 0
def getSoup(url) :
f= urllib.urlopen(url)
s = f.read()
f.close()
return BeautifulSoup(s)
def resolveVideoURL(url_str, type="mp4"):
global lineNum
type = __getFileType(type)
if not type:
sys.exit('Error : Unsupported file type %s' % type)
video_info = VideoInfo(url_str)
#print video_info
video_url_map = video_file_urls(video_info)
#print "video_url_map : " , video_url_map;
video_title = title(video_info)
#print "video_title : ", video_title
url = ''
for entry in video_url_map:
entry_type = entry['type'][0]
entry_type = entry_type.split(';')[0]
if entry_type.lower() == type.lower():
url = entry['url']
break
if url == '' :
sys.exit('Error : Can not find video file\'s url')
#print url
#downloader(url, video_title+'.'+argvs.type)
parsed_uri = urlparse( url )
domain = '{uri.netloc}'.format(uri=parsed_uri)
#domain="www.naver.com"
#print "Adresses for %s:" % domain
for address_type in ['A']:
#for address_type in ['A', 'CNAME']:
try:
answers = dns.resolver.query(domain, address_type)
ip = ""
for rdata in answers:
ip = rdata
print str(lineNum),
lineNum +=1
sys.stdout.write(url_str+","),
sys.stdout.write(domain+","),
print ip
return ip
except dns.resolver.NoAnswer:
pass
#print ""
MAX_NUM =1000
q = Queue()
def worker():
while(True):
url_str = q.get()
added_url.pop()
try :
ip = resolveVideoURL(url_str)
except KeyError :
print "%s, error occured" % (url_str)
completed_url.add(url_str)
continue
if(lineNum > MAX_NUM):
threading.thread.exit()
str_ip = str(ip)
if( str_ip in ip_count):
ip_count[str_ip] += 1
else :
ip_count[str_ip] = 1
completed_url.add(url_str)
if(len(completed_url) + len(added_url) > MAX_NUM):
continue
soup = getSoup(url_str)
#print soup.find("ul", {"id" : "watch-related"})
relatedList = soup.find("ul", {"id" : "watch-related"}).findAll("li", {"class":"video-list-item"} )
for related in relatedList :
#print "################################"
#print related
if(related):
full_url = "https://www.youtube.com"+ related.find("a")["href"][0:20];
if(full_url not in completed_url):
added_url.add(full_url)
q.put(full_url)
def main():
lineNum = 0
parser = argparse.ArgumentParser(description='YoutubeVideoDownload -- a small and simple program for downloading Youtube Video File')
parser.add_argument('url', metavar='url', type=str, help='Youtube video URL string with "http://" prefixed')
parser.add_argument('type', metavar='type', type=str, help="Downloaded file's type ( webm || mp4 || 3gp || flv)")
argvs = parser.parse_args()
added_url.add(argvs.url)
q.put(argvs.url)
threadList = []
for i in range(10):
t = threading.Thread(target=worker)
threadList.append(t)
t.daemon = True
t.start()
for t in threadList:
t.join()
resolvedNum =0
#thread.exit()
#print ip_count
f = open("youtube_ip_count.txt", 'w')
for x in ip_count:
print (x) + ", " +str(ip_count[x])
f.write((x) + ", " +str(ip_count[x])+ "\n")
sys.exit(0)
if __name__ == '__main__':
main()