pandoratv.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # encoding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..compat import (
  5. compat_str,
  6. compat_urlparse,
  7. )
  8. from ..utils import (
  9. ExtractorError,
  10. float_or_none,
  11. parse_duration,
  12. str_to_int,
  13. )
  14. class PandoraTVIE(InfoExtractor):
  15. _VALID_URL = r'https?://(?:.+?\.)?channel\.pandora\.tv/channel/video\.ptv\?'
  16. _TEST = {
  17. 'url': 'http://jp.channel.pandora.tv/channel/video.ptv?c1=&prgid=53294230&ch_userid=mikakim&ref=main&lot=cate_01_2',
  18. 'info_dict': {
  19. 'id': '53294230',
  20. 'ext': 'flv',
  21. 'title': '頭を撫でてくれる?',
  22. 'description': '頭を撫でてくれる?',
  23. 'thumbnail': 're:^https?://.*\.jpg$',
  24. 'duration': 39,
  25. 'upload_date': '20151218',
  26. 'uploader': 'カワイイ動物まとめ',
  27. 'uploader_id': 'mikakim',
  28. 'view_count': int,
  29. 'like_count': int,
  30. }
  31. }
  32. def _real_extract(self, url):
  33. qs = compat_urlparse.parse_qs(compat_urlparse.urlparse(url).query)
  34. video_id = qs.get('prgid', [None])[0]
  35. user_id = qs.get('ch_userid', [None])[0]
  36. if any(not f for f in (video_id, user_id,)):
  37. raise ExtractorError('Invalid URL', expected=True)
  38. data = self._download_json(
  39. 'http://m.pandora.tv/?c=view&m=viewJsonApi&ch_userid=%s&prgid=%s'
  40. % (user_id, video_id), video_id)
  41. info = data['data']['rows']['vod_play_info']['result']
  42. formats = []
  43. for format_id, format_url in info.items():
  44. if not format_url:
  45. continue
  46. height = self._search_regex(
  47. r'^v(\d+)[Uu]rl$', format_id, 'height', default=None)
  48. if not height:
  49. continue
  50. formats.append({
  51. 'format_id': '%sp' % height,
  52. 'url': format_url,
  53. 'height': int(height),
  54. })
  55. self._sort_formats(formats)
  56. return {
  57. 'id': video_id,
  58. 'title': info['subject'],
  59. 'description': info.get('body'),
  60. 'thumbnail': info.get('thumbnail') or info.get('poster'),
  61. 'duration': float_or_none(info.get('runtime'), 1000) or parse_duration(info.get('time')),
  62. 'upload_date': info['fid'][:8] if isinstance(info.get('fid'), compat_str) else None,
  63. 'uploader': info.get('nickname'),
  64. 'uploader_id': info.get('upload_userid'),
  65. 'view_count': str_to_int(info.get('hit')),
  66. 'like_count': str_to_int(info.get('likecnt')),
  67. 'formats': formats,
  68. }