dbtv.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 DBTVIE(InfoExtractor):
  9. _VALID_URL = r'http://dbtv.no/(?P<id>[0-9]+)/?(?P<slug>.*)$'
  10. _TEST = {
  11. 'url': 'http://dbtv.no/3649835190001#Skulle_teste_ut_fornøyelsespark,_men_kollegaen_var_bare_opptatt_av_bikinikroppen',
  12. 'md5': 'b89953ed25dacb6edb3ef6c6f430f8bc',
  13. 'info_dict': {
  14. 'id': '3649835190001',
  15. 'ext': 'mp4',
  16. 'title': 'Skulle teste ut fornøyelsespark, men kollegaen var bare opptatt av bikinikroppen',
  17. 'description': 'md5:d681bf2bb7dd3503892cedb9c2d0e6f2',
  18. 'thumbnail': 'http://gfx.dbtv.no/thumbs/still/33100.jpg',
  19. 'timestamp': 1404039863,
  20. 'upload_date': '20140629',
  21. 'duration': 69544,
  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://api.dbtv.no/discovery/%s' % video_id, video_id, 'Downloading media JSON')
  29. # We only want the first video in the JSON API file.
  30. video = data['playlist'][0]
  31. # Check for full HD video, else use the standard video URL
  32. for i in range(0, len(video['renditions'])):
  33. if int(video['renditions'][i]['width']) == 1280:
  34. video_url = video['renditions'][i]['URL']
  35. break
  36. else:
  37. video_url = video['URL']
  38. # Add access token to image or it will fail.
  39. thumbnail = video['splash']
  40. # Duration int.
  41. duration = int(video['length'])
  42. # Timestamp is given in milliseconds.
  43. timestamp = float(str(video['publishedAt'])[0:-3])
  44. formats = []
  45. # Video URL.
  46. if video['URL'] is not None:
  47. formats.append({
  48. 'url': video_url,
  49. 'format_id': 'mp4',
  50. 'ext': 'mp4'
  51. })
  52. else:
  53. raise ExtractorError('No download URL found for video: %s.' % video_id, expected=True)
  54. return {
  55. 'id': video_id,
  56. 'title': video['title'],
  57. 'description': video['desc'],
  58. 'thumbnail': thumbnail,
  59. 'timestamp': timestamp,
  60. 'duration': duration,
  61. 'view_count': video['views'],
  62. 'formats': formats,
  63. }