-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathYouTubeCore.py
209 lines (171 loc) · 6.39 KB
/
YouTubeCore.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
import sys, urllib, urllib2, re, os, string
from xml.dom.minidom import parseString
# ERRORCODES:
# 0 = Ignore
# 200 = Ok
# 303 = Returned and error
# 500 = Uncaught error
class YouTubeCore(object):
__plugin__ = 'Reddit.YouTubeCore'
__dbg__ = True
PREFERRED = True
USERAGENT = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8"
def __init__(self):
return None;
def _fetchPage(self, link=''):
if self.__dbg__:
print self.__plugin__ + " fetching page : " + link
request = urllib2.Request(link)
request.add_header('User-Agent', self.USERAGENT)
try:
con = urllib2.urlopen(request)
result = con.read()
new_url = con.geturl()
con.close()
# Return result if it isn't age restricted
if ( result.find("verify-actions") == -1 and result.find("verify-age-actions") == -1):
return ( result, 200 )
else:
# TODO: handle login
if self.__dbg__:
print self.__plugin__ + " _fetchPage. Need to login"
return ( "", 500 )
except urllib2.HTTPError, e:
err = str(e)
if self.__dbg__:
print self.__plugin__ + " _fetchPage HTTPError : " + err
# 400 (Bad request)
if ( err.find("400") > -1 ):
return ( err, 303 )
# 401 (Not authorized)
elif ( err.find("401") > -1 ):
if self.__dbg__:
print self.__plugin__ + " _fetchPage 401 Not Authorized"
return ( None, 303)
# 403 (Forbidden)
elif ( err.find("403") > -1 ):
if self.__dbg__:
print self.__plugin__ + " _fetchPage got empty results back"
return ( None, 303)
# 501 (Not implemented) - A 501 response code indicates that you have tried to execute an unsupported operation.
elif ( err.find("501") > -1):
return ( err, 303 )
except:
if self.__dbg__:
print self.__plugin__ + ' _fetchPage ERROR: %s::%s (%d) - %s' % (self.__class__.__name__, sys.exc_info()[2].tb_frame.f_code.co_name, sys.exc_info()[2].tb_lineno, sys.exc_info()[1])
return ( "", 500 )
def _convertFlashVars(self, html):
obj = { "PLAYER_CONFIG": { "args": {} } }
temp = html.split("&")
for item in temp:
it = item.split("=")
obj["PLAYER_CONFIG"]["args"][it[0]] = urllib.unquote_plus(it[1])
return obj
def getVideoUrlMap(self, pl_obj, video = {}):
if self.__dbg__:
print self.__plugin__ + " getVideoUrlMap: "
links = {}
video["url_map"] = "true"
html = ""
if pl_obj["args"].has_key("fmt_stream_map"):
html = pl_obj["args"]["fmt_stream_map"]
if len(html) == 0 and pl_obj["args"].has_key("url_encoded_fmt_stream_map"):
html = urllib.unquote(pl_obj["args"]["url_encoded_fmt_stream_map"])
if len(html) == 0 and pl_obj["args"].has_key("fmt_url_map"):
html = pl_obj["args"]["fmt_url_map"]
html = urllib.unquote_plus(html)
if pl_obj["args"].has_key("liveplayback_module"):
video["live_play"] = "true"
fmt_url_map = [html]
if html.find("|") > -1:
fmt_url_map = html.split('|')
elif html.find(",url=") > -1:
fmt_url_map = html.split(',url=')
elif html.find("&conn=") > -1:
video["stream_map"] = "true"
fmt_url_map = html.split('&conn=')
print self.__plugin__ + " getVideoUrlMap Searching for fmt_url_map 2: " + repr(fmt_url_map)
if len(fmt_url_map) > 0:
for index, fmt_url in enumerate(fmt_url_map):
if fmt_url.find("&url") > -1:
fmt_url = fmt_url.split("&url")
fmt_url_map += [fmt_url[1]]
fmt_url = fmt_url[0]
if (len(fmt_url) > 7 and fmt_url.find("&") > 7):
quality = "5"
final_url = fmt_url.replace(" ", "%20").replace("url=", "")
if (final_url.rfind(';') > 0):
final_url = final_url[:final_url.rfind(';')]
if (final_url.rfind(',') > final_url.rfind('&id=')):
final_url = final_url[:final_url.rfind(',')]
elif (final_url.rfind(',') > final_url.rfind('/id/') and final_url.rfind('/id/') > 0):
final_url = final_url[:final_url.rfind('/')]
if (final_url.rfind('itag=') > 0):
quality = final_url[final_url.rfind('itag=') + 5:]
if quality.find('&') > -1:
quality = quality[:quality.find('&')]
if quality.find(',') > -1:
quality = quality[:quality.find(',')]
elif (final_url.rfind('/itag/') > 0):
quality = final_url[final_url.rfind('/itag/') + 6:]
if final_url.find("&type") > 0:
final_url = final_url[:final_url.find("&type")]
if self.PREFERRED == "true":
pos = final_url.find("://")
fpos = final_url.find("fallback_host")
if pos > -1 and fpos > -1:
host = final_url[pos + 3:]
if host.find("/") > -1:
host = host[:host.find("/")]
fmt_fallback = final_url[fpos + 14:]
if fmt_fallback.find("&") > -1:
fmt_fallback = fmt_fallback[:fmt_fallback.find("&")]
final_url = final_url.replace(host, fmt_fallback)
final_url = final_url.replace("fallback_host=" + fmt_fallback, "fallback_host=" + host)
if final_url.find("rtmp") > -1 and index > 0:
if pl_obj.has_key("url") or True:
final_url += " swfurl=" + pl_obj["url"] + " swfvfy=1"
playpath = False
if final_url.find("stream=") > -1:
playpath = final_url[final_url.find("stream=")+7:]
if playpath.find("&") > -1:
playpath = playpath[:playpath.find("&")]
else:
playpath = fmt_url_map[index - 1]
if playpath:
if pl_obj["args"].has_key("ptk") and pl_obj["args"].has_key("ptchn"):
final_url += " playpath=" + playpath + "?ptchn=" + pl_obj["args"]["ptchn"] + "&ptk=" + pl_obj["args"]["ptk"]
links[int(quality)] = final_url.replace('\/','/')
#if self.__dbg__:
# print self.__plugin__ + " getVideoUrlMap done " + repr(links)
return links
def selectVideoQuality(self, links):
link = links.get
video_url = ""
if self.__dbg__:
print self.__plugin__ + "selectedVideoQuality : "
# SD videos are default, but go for the highest res
if (link(35)):
video_url = link(35)
elif (link(34)):
video_url = link(34)
elif (link(59)): #<-- 480 for rtmpe
video_url = link(59)
elif (link(78)): #<-- seems to be around 400 for rtmpe
video_url = link(78)
elif (link(18)):
video_url = link(18)
elif (link(5)):
video_url = link(5)
elif (link(22)): # 720p
video_url = link(22)
elif (link(37)): # 1080p
video_url = link(37)
return video_url
def video_id(self, value):
# TODO: make this more fool proof
value = value.replace('&', '&')
value = value.split('?')[1]
value = value.split('=')[1]
value = value.split('&')[0]
return value