vgtv.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. ExtractorError
  7. )
  8. class VGTVIE(InfoExtractor):
  9. # Because of the #! in the URL structure we need to add ' before and after given URL.
  10. # Or else it will cry: -bash: !/video/100495/lars-og-lars-sesong-6-episode-6-lakselus: event not found
  11. _VALID_URL = r'http://(?:www\.)?vgtv\.no/#!/(?:.*)/(?P<id>[0-9]+)/(?P<title>[^?#]*)'
  12. _TEST = {
  13. 'url': 'http://www.vgtv.no/#!/video/84196/hevnen-er-soet-episode-10-abu',
  14. 'md5': 'b8be7a234cebb840c0d512c78013e02f',
  15. 'info_dict': {
  16. 'id': '84196',
  17. 'ext': 'mp4',
  18. 'title': 'Hevnen er søt episode 10: Abu',
  19. 'description': 'md5:e25e4badb5f544b04341e14abdc72234',
  20. 'timestamp': 1404626400,
  21. 'upload_date': '20140706'
  22. }
  23. }
  24. def _real_extract(self, url):
  25. mobj = re.match(self._VALID_URL, url)
  26. video_id = mobj.group('id')
  27. # Download JSON file containing video info.
  28. data = self._download_json('http://svp.vg.no/svp/api/v1/vgtv/assets/%s?appName=vgtv-website' % video_id, video_id, 'Downloading media JSON')
  29. # Known streamType: vod, live, wasLive
  30. # Will it even be possible to add support for live streams?
  31. if data['streamType'] != 'vod':
  32. raise ExtractorError('Stream type \'%s\' is not yet supported.' % data['streamType'], expected=True)
  33. # Add access token to image or it will fail.
  34. thumbnail = data['images']['main'] + '?t[]=900x506q80'
  35. formats = []
  36. # Most videos are in MP4, but some are either HLS or HDS.
  37. # Don't want to support HDS.
  38. if data['streamUrls']['mp4'] is not None:
  39. formats.append({
  40. 'url': data['streamUrls']['mp4'],
  41. 'format_id': 'mp4',
  42. 'ext': 'mp4'
  43. })
  44. elif data['streamUrls']['hls'] is not None:
  45. self.to_screen(u'No MP4 URL found, using m3u8. This may take some extra time.')
  46. formats.append({
  47. 'url': data['streamUrls']['hls'],
  48. 'format_id': 'm3u8',
  49. 'ext': 'mp4'
  50. })
  51. else:
  52. raise ExtractorError('No download URL found for video: %s.' % video_id, expected=True)
  53. return {
  54. 'id': video_id,
  55. 'title': data['title'],
  56. 'description': data['description'],
  57. 'thumbnail': thumbnail,
  58. 'timestamp': data['published'],
  59. 'duration': data['duration'],
  60. 'view_count': data['displays'],
  61. 'formats': formats,
  62. }