[2999ca]: plugin.video.fox.news / default.py  Maximize  Restore  History

Download this file

292 lines (248 with data), 10.2 kB

  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
281
282
283
284
285
286
287
288
289
290
291
292
import urllib
import urllib2
import os
import re
import xbmcplugin
import xbmcgui
import xbmcaddon
import xbmcvfs
import StorageServer
from BeautifulSoup import BeautifulSoup
try:
import json
except:
import simplejson as json
__settings__ = xbmcaddon.Addon(id='plugin.video.fox.news')
__language__ = __settings__.getLocalizedString
home = __settings__.getAddonInfo('path')
icon = xbmc.translatePath( os.path.join( home, 'icon.png' ) )
cache = StorageServer.StorageServer("foxnews", 24)
quality = __settings__.getSetting('quality')
def make_request(url, headers=None):
if headers is None:
headers = {'User-agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0.1',
'Referer' : 'http://video.foxnews.com'}
try:
req = urllib2.Request(url,None,headers)
response = urllib2.urlopen(req)
data = response.read()
response.close()
return data
except urllib2.URLError, e:
print 'We failed to open "%s".' % url
if hasattr(e, 'reason'):
print 'We failed to reach a server.'
print 'Reason: ', e.reason
if hasattr(e, 'code'):
print 'We failed with error code - %s.' % e.code
def get_categories():
# add_dir('Watch Live', '', 5, icon)
url = 'http://video.foxnews.com/playlist/latest-latest-news/'
data = make_request(url)
soup = BeautifulSoup(data, convertEntities=BeautifulSoup.HTML_ENTITIES)
add_dir(soup.body.h1.contents[0].strip(), '', 2, icon)
for i in soup.find('nav')('a'):
add_dir(i.string, i['href'], 1, icon)
cache.set("videos_dict", repr({ "videos": get_video_list(data) }))
def get_sub_categories(url):
if url.startswith('//video.foxnews'):
url = 'http:'+url
elif url.startswith('/'):
url = 'http://video.foxnews.com'+url
data = make_request(url)
soup = BeautifulSoup(data, convertEntities=BeautifulSoup.HTML_ENTITIES)
featured_name = soup.body.h1.contents[0].strip()
add_dir(featured_name, '', 2, icon)
items = soup('div', attrs={'id' : 'shows'})[0]('a')
for i in items:
name = i.string
if name != featured_name:
href = 'http:'+i['href']
add_dir(name, href, 3, icon)
cache.set("videos_dict", repr({ "videos": get_video_list(data) }))
def get_video_list(html, url=None):
if url is None:
soup = BeautifulSoup(html, convertEntities=BeautifulSoup.HTML_ENTITIES)
else:
soup = BeautifulSoup(make_request(url), convertEntities=BeautifulSoup.HTML_ENTITIES)
items = soup.find('div', attrs={'class' : 'pl'})('li')
video_list = []
for i in items:
href = i.a['href']
name = i.h2.a.string
thumb = i.img['src']
date = i.time.string
duration = i.strong.string
desc = i.span.string
if url is not None:
add_link(date, name, duration, href, thumb, desc)
else:
video_list.append((date, name, duration, href, thumb, desc))
if url is None:
return video_list
def get_featured_videos():
for i in eval(cache.get("videos_dict"))['videos']:
add_link(i[0], i[1], i[2], i[3], i[4], i[5])
def get_json_data(video_id):
url = 'http://video.foxnews.com/v/feed/video/%s.js?' %video_id
data = json.loads(make_request(url))
try:
video_url = data['channel']['item']['media-content']['@attributes']['url']
if video_url is None: raise
else: return video_url
except:
try:
video_url = data['channel']['item']['media-content']['mvn-fnc_mp4']
if video_url is None: raise
else: return video_url
except:
try:
video_url = data['channel']['item']['media-content']['mvn-flv1200']
if video_url is None: raise
else: return video_url
except:
print '-- No video_url --'
return None
def get_smil(video_id):
smil_url = 'http://video.foxnews.com/v/feed/video/%s.smil' %video_id
headers = {'Referer' : 'http://video.foxnews.com/assets/akamai/FoxNewsPlayer.swf',
'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0.1'}
soup = BeautifulSoup(make_request(smil_url, headers))
try:
return soup.find('meta', attrs={'name' : "rtmpAuthBase"})['content'] + soup.find('video')['src']
except: return None
def resolve_url(url):
quality_types = {'0' : '_FNC_HIGH.',
'1' : '_FNC_MED.',
'2' : '_FNC_MED_LOW.',
'3' : '_FNC_LOW.'}
quality_type = quality_types[quality]
video_id = url.split('/v/')[1].split('/')[0]
vidoe_url = None
try:
video_url = get_smil(video_id)
if vidoe_url is None: raise
except:
try:
video_url = get_json_data(video_id)
if vidoe_url is None: raise
except: pass
if video_url is None:
succeeded = False
playback_url = ''
else:
succeeded = True
if succeeded:
if not quality_type in video_url:
for i in quality_types.values():
if i in video_url:
playback_url = video_url.replace(i, quality_type)
break
else: playback_url = video_url
item = xbmcgui.ListItem(path=playback_url)
xbmcplugin.setResolvedUrl(int(sys.argv[1]), succeeded, item)
def get_live_streams():
playlist_id = None
items = None
data = make_request('http://video.foxnews.com/playlist/live-landing-page/')
pattern = 'pageVars.playlistId = "(.+?)";'
match = re.findall(pattern, data)
if len(match) > 0:
playlist_id = match[0]
if playlist_id:
json_url = 'http://video.foxnews.com/v/feed/playlist/%s.js?template=fox&callback=FOX_Header_Watch_Feed' %playlist_id
j_data = json.loads(make_request(json_url).strip('FOX_Header_Watch_Feed(')[:-1])
items = j_data['channel']['item']
if items:
for i in items:
title = i['title']
smil = i['enclosure']['@attributes']['url']
thumb = i['media-group']['media-thumbnail']['@attributes']['url']
print (title, smil, thumb)
u=sys.argv[0]+"?url="+urllib.quote_plus(smil)+"&mode=6"
liz=xbmcgui.ListItem(title, iconImage="DefaultVideo.png", thumbnailImage=thumb)
liz.setInfo(type="Video", infoLabels={"Title": title})
liz.setProperty('IsPlayable', 'true')
ok=xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=liz)
def resolve_live_url(url):
resolved_url = ''
succeeded = False
soup = BeautifulSoup(make_request(url))
base = soup.find('meta', attrs={'name': "httpBase"})['content']
renditions = []
for i in soup('video'):
renditions.append({'bitrate': i['system-bitrate'], 'href': i['src']})
sorted_renditions = sorted(renditions, key=lambda k: int(k['bitrate']), reverse=True)
if len(sorted_renditions) == 4:
resolved_url = base + sorted_renditions[int(quality)]['href']
else:
resolved_url = base + sorted_renditions[0]['href']
if resolved_url != '':
succeeded = True
item = xbmcgui.ListItem(path=resolved_url)
xbmcplugin.setResolvedUrl(int(sys.argv[1]), succeeded, item)
def add_link(date, name, duration, href, thumb, desc):
description = date+'\n\n'+desc
u=sys.argv[0]+"?url="+urllib.quote_plus(href)+"&mode=4"
liz=xbmcgui.ListItem(name, iconImage="DefaultVideo.png", thumbnailImage=thumb)
liz.setInfo(type="Video", infoLabels={ "Title": name, "Plot": description, "Duration": duration})
liz.setProperty('IsPlayable', 'true')
ok=xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=liz)
def add_dir(name,url,mode,iconimage):
u=sys.argv[0]+"?url="+urllib.quote_plus(url)+"&mode="+str(mode)+"&name="+urllib.quote_plus(name)
ok=True
liz=xbmcgui.ListItem(name, iconImage="DefaultFolder.png", thumbnailImage=iconimage)
liz.setInfo( type="Video", infoLabels={ "Title": name } )
ok=xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=liz,isFolder=True)
return ok
def get_params():
param=[]
paramstring=sys.argv[2]
if len(paramstring)>=2:
params=sys.argv[2]
cleanedparams=params.replace('?','')
if (params[len(params)-1]=='/'):
params=params[0:len(params)-2]
pairsofparams=cleanedparams.split('&')
param={}
for i in range(len(pairsofparams)):
splitparams={}
splitparams=pairsofparams[i].split('=')
if (len(splitparams))==2:
param[splitparams[0]]=splitparams[1]
return param
xbmcplugin.setContent(int(sys.argv[1]), 'movies')
params=get_params()
url=None
name=None
mode=None
try:
url=urllib.unquote_plus(params["url"])
except:
pass
try:
name=urllib.unquote_plus(params["name"])
except:
pass
try:
mode=int(params["mode"])
except:
pass
print "Mode: "+str(mode)
print "URL: "+str(url)
print "Name: "+str(name)
if mode==None:
get_categories()
elif mode==1:
get_sub_categories(url)
elif mode==2:
get_featured_videos()
elif mode==3:
get_video_list(None, url)
elif mode==4:
resolve_url(url)
elif mode==5:
get_live_streams()
elif mode==6:
resolve_live_url(url)
xbmcplugin.endOfDirectory(int(sys.argv[1]))

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks