[9a84ba]: plugin.video.vgtv / resources / lib / api.py Maximize Restore History

Download this file

api.py    334 lines (279 with data), 11.9 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright 2012-2013 Espen Hovlandsdal
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# This module contains helper functions that interact with the VGTV.no JSON API
import simplejson
import urllib2
import re
from utils import unescape
from datetime import datetime
from urllib import urlencode
from random import random
class VgtvApi():
API_URL = 'http://api.vgtv.no/api/actions'
CMS_URL = 'http://cmsapi.vgtv.no'
PER_PAGE = 40
plugin = None
categories = None
def __init__(self, plugin):
self.plugin = plugin
def get_api_url(self, url, params={}):
defaults = {'formats': 'http', 'meta': 1}
params = dict(defaults.items() + params.items())
return self.API_URL + url + '?' + urlencode(params)
def get_default_video_list(self, url, page, raw=False, params={}):
if raw is False:
offset = self.calculate_offset(page)
params.update({'limit': self.PER_PAGE, 'offset': offset})
url = self.get_api_url(url=url, params=params)
response = urllib2.urlopen(url)
return self.parse_video_response(response)
def get_category_tree(self):
if self.categories is not None:
return self.categories
url = self.CMS_URL + '/categories/drvideo-list'
response = urllib2.urlopen(url)
data = simplejson.loads(response.read())
self.categories = data
return data
def get_categories(self, root_id=0):
categories = self.get_category_tree()
matches = []
for id in categories:
if int(id) < 0:
continue
category = categories.get(id)
if (int(category.get('parentId')) == int(root_id)):
matches.append({
'label': unescape(category.get('name')),
'path': self.plugin.url_for('show_category', id=str(id)),
'id': id
})
return matches
def resolve_video_url(self, video_id):
#url = self.get_api_url(url='/video/', params={'id': video_id})
url = 'http://www.vgtv.no/data/actions/videostatus/?id=%s' % video_id
response = urllib2.urlopen(url)
data = simplejson.loads(response.read())
return self.get_video_urls(data, allow_resolve=False)
def parse_video_response(self, response):
data = simplejson.loads(response.read())
items = list()
count = 0
for video in data['videos']:
vid_url, thumb_url, category_id, dur = self.get_video_urls(video)
count += 1
if vid_url is None:
continue
meta = video.get('meta')
items.append({
'label': unescape(meta.get('title')),
'thumbnail': thumb_url,
'info': {
'plot': unescape(meta.get('preamble') or ''),
'originaltitle': unescape(meta.get('title') or '???'),
'tagline': unescape(meta.get('preamble') or ''),
'aired': self.get_date(meta.get('timePublished')),
'duration': self.get_duration(meta.get('duration', 0))
},
'stream_info': {
'video': {
'duration': meta.get('duration', 0)
}
},
'path': vid_url,
'is_playable': True,
})
return items, (count < self.PER_PAGE)
def get_video_urls(self, video, allow_resolve=True):
highest_bitrate = 0
best_thumb = {'width': 10000}
best_format = None
category_id = video.get('meta').get('category')
video_title = video.get('meta').get('title', 'none')
duration = str(video.get('meta').get('duration'))
video_title = self.url_friendly(video_title.encode('utf-8'))
# Some videos do not have a formats array
if 'formats' not in video and allow_resolve:
video_url = self.plugin.url_for('play_id',
id=str(video['id']),
category=str(category_id),
title=video_title.encode('utf-8')
)
thumb_url = self.build_thumbnail_url({
'width': 354,
'height': 199
}, video['id'])
return video_url, thumb_url, category_id, duration
elif 'formats' not in video:
self.plugin.log.warning('Formats not in video-response')
return None, None, None, None
elif 'http' not in video['formats']:
# Can't find any http URL, probably protected
return None, None, None, None
# MP4 or m3u8?
if (allow_resolve):
# Use MP4 by default
format = 'mp4'
if ('mp4' not in video['formats']['http'] and
'm3u8' in video['formats']['http']):
format = 'm3u8'
elif ('mp4' not in video['formats']['http'] and
'flv' in video['formats']['http']):
format = 'flv'
else:
# Reverse order for stuff we have to resolve
# Had some trouble getting mp4 stream to work
format = 'm3u8'
if ('m3u8' not in video['formats']['http'] and
'mp4' in video['formats']['http']):
format = 'mp4'
# Loop through the formats to find the best one
for currentformat in video['formats']['http'][format]:
# Find the highest bitrate available
if currentformat['bitrate'] > highest_bitrate:
highest_bitrate = currentformat['bitrate']
best_format = currentformat
# Thumbs seem to be around ~300px in general
if currentformat['width'] > 310 and currentformat['width'] < best_thumb['width']:
best_thumb = currentformat
# Fall back if something failed
if best_format is None:
self.plugin.log.error('No format found for video %s' % video['id'])
return None, None, None, None
# If we didn't find a fitting thumb, use thumb from the highest bitrate
if 'height' not in best_thumb:
best_thumb = best_format
video_url = self.build_video_url(best_format['paths'][0])
video_url = self.plugin.url_for('play_url',
url=video_url,
category=str(category_id),
id=str(video['id']),
title=video_title.encode('utf-8'),
duration=duration
)
thumb_url = self.build_thumbnail_url(best_thumb, video['id'])
return video_url, thumb_url, category_id, duration
def build_video_url(self, p):
# p = parts
url = 'http://%s/%s/%s' % (p['address'], p['path'], p['filename'])
return url
def build_thumbnail_url(self, d, id):
# d = dimensions
url = 'http://api.vgtv.no/images/id/' + str(id / 1000) + 'xxx/'
url += '%s/w%dh%dc.jpg' % (id % 1000, d['width'], d['height'])
return url
def calculate_offset(self, page):
return self.PER_PAGE * (int(page) - 1)
def get_date(self, timestamp):
return datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d')
def get_duration(self, secs):
return str(secs / 60)
def track_play(self, id, category_id, title, resolution, duration):
category = self.get_category(category_id)
self.track_play_drclick(id, category)
self.track_play_tns(id, category, resolution, title, duration)
self.track_play_xiti(id, category, resolution, title, duration)
def track_play_drclick(self, id, category=None):
url = 'http://drclick.vg.no/drklikkvgtv/register/betalive.php?'
url += 'event=videoplay&identification=' + str(id) + '&text1=vgtv-xbmc'
if category is not None:
url += '&text2=' + str(category.get('drVideoId'))
try:
urllib2.urlopen(url)
except urllib2.URLError:
# Not being able to track is no fatal error
pass
def track_play_tns(self, id, category, resolution, title, duration):
res = resolution.split('x')
path = self.get_category_path(category)
name = 'vgtv/' + path + '/video_' + str(id) + '_' + title
uid = self.baseN(int(round(random() * 10000000000)), 32)
url = 'http://vgstream.tns-cs.net/j0=,,,pl=VGTVplayerXBMC+pv=version1'
url += '+sx=' + res[0] + '+sy=' + res[1] + ';+,name=' + name
url += '+ct=video+uid=' + uid + '+post=,,0+0,+memlv5;+,1+12+memlv7;;'
url += '+dur=' + str(duration) + '+vt=217;;;'
try:
urllib2.urlopen(url.encode('utf-8'))
except urllib2.URLError:
# Not being able to track is no fatal error
pass
def track_play_xiti(self, id, category, resolution, title, duration):
now = datetime.now()
category_name = category.get('title').encode('utf-8')
title = title.encode('utf-8')
params = {
's': 417204,
'p': category_name + '::' + str(id) + '_' + title,
's2': str(category.get('drVideoId')),
'type': 'video',
'plyr': 2,
'a': 'play',
'm5': 'int',
'm6': 'clip',
'm1': duration,
'rmp': 0,
'rmpf': 0,
'rmbufp': 0,
'm3': 2,
'm4': 16,
'm7': 0,
'm8': 9,
'prich': 'VGTV-XBMC',
's2rich': 1,
'rfsh': 10,
'hl': 'x'.join([str(now.hour), str(now.minute), str(now.second)]),
'r': resolution + 'x24x24'
}
url = 'http://logc189.xiti.com/hit.xiti?'
url += urlencode(params)
try:
urllib2.urlopen(url)
except urllib2.URLError:
# Not being able to track is no fatal error
pass
def url_friendly(self, unfriendly):
friendly = unfriendly.decode('utf-8')
friendly = re.sub(r'\s', r'_', friendly)
friendly = friendly.lower()
friendly = friendly.replace(u'\xe6', u'ae')
friendly = friendly.replace(u'\xf8', u'oe')
friendly = friendly.replace(u'\xe5', u'aa')
friendly = re.sub(r'[^A-Za-z\/_0-9\-:()]', r'', friendly)
return friendly
def get_category(self, category_id):
url = self.CMS_URL + '/categories/' + str(category_id)
try:
response = urllib2.urlopen(url)
data = simplejson.loads(response.read())
return data
except urllib2.HTTPError:
return {
'drVideoId': 0,
'categoryPath': None,
'title': ''
}
return data
def get_category_path(self, category):
categoryPath = category.get('categoryPath', None)
if categoryPath is None:
return 'front'
else:
return '/'.join([p.get('name') for p in categoryPath])
def baseN(self, num, b, numerals='0123456789abcdefghijklmnopqrstuvwxyz'):
return ((num == 0) and numerals[0]) or (
self.baseN(num // b, b, numerals)
.lstrip(numerals[0]) + numerals[num % b])