forked from vincelwt/RaspberryCast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYoutubeFullUrl.py
executable file
·69 lines (60 loc) · 2.21 KB
/
YoutubeFullUrl.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
#!/usr/bin/python
import urllib2
import sys
from urlparse import parse_qs, urlparse
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='
video_id = extract_video_id(video_url)
if video_id != None:
request_url += video_id
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 urllib2.URLError :
print "urllib error"
sys.exit('Error : Invalid Youtube URL Passing %s' % video_url)
def extract_video_id(value):
query = urlparse(value)
if query.hostname == 'youtu.be':
return query.path[1:]
if query.hostname in ('www.youtube.com', 'youtube.com'):
if query.path == '/watch':
p = parse_qs(query.query)
return p['v'][0]
if query.path[:7] == '/embed/':
return query.path.split('/')[2]
if query.path[:3] == '/v/':
return query.path.split('/')[2]
# fail?
return None
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 get_flux_url(url_str):
type = 'video/mp4'
video_info = VideoInfo(url_str)
video_url_map = video_file_urls(video_info)
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')
return url