You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
3.3 KiB

10 years ago
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. int_or_none,
  6. merge_dicts,
  7. mimetype2ext,
  8. url_or_none,
  9. )
  10. class AparatIE(InfoExtractor):
  11. _VALID_URL = r'https?://(?:www\.)?aparat\.com/(?:v/|video/video/embed/videohash/)(?P<id>[a-zA-Z0-9]+)'
  12. _TESTS = [{
  13. 'url': 'http://www.aparat.com/v/wP8On',
  14. 'md5': '131aca2e14fe7c4dcb3c4877ba300c89',
  15. 'info_dict': {
  16. 'id': 'wP8On',
  17. 'ext': 'mp4',
  18. 'title': 'تیم گلکسی 11 - زومیت',
  19. 'description': 'md5:096bdabcdcc4569f2b8a5e903a3b3028',
  20. 'duration': 231,
  21. 'timestamp': 1387394859,
  22. 'upload_date': '20131218',
  23. 'view_count': int,
  24. },
  25. }, {
  26. # multiple formats
  27. 'url': 'https://www.aparat.com/v/8dflw/',
  28. 'only_matching': True,
  29. }]
  30. def _real_extract(self, url):
  31. video_id = self._match_id(url)
  32. # Provides more metadata
  33. webpage = self._download_webpage(url, video_id, fatal=False)
  34. if not webpage:
  35. # Note: There is an easier-to-parse configuration at
  36. # http://www.aparat.com/video/video/config/videohash/%video_id
  37. # but the URL in there does not work
  38. webpage = self._download_webpage(
  39. 'http://www.aparat.com/video/video/embed/vt/frame/showvideo/yes/videohash/' + video_id,
  40. video_id)
  41. options = self._parse_json(
  42. self._search_regex(
  43. r'options\s*=\s*JSON\.parse\(\s*(["\'])(?P<value>(?:(?!\1).)+)\1\s*\)',
  44. webpage, 'options', group='value'),
  45. video_id)
  46. player = options['plugins']['sabaPlayerPlugin']
  47. formats = []
  48. for sources in player['multiSRC']:
  49. for item in sources:
  50. if not isinstance(item, dict):
  51. continue
  52. file_url = url_or_none(item.get('src'))
  53. if not file_url:
  54. continue
  55. item_type = item.get('type')
  56. if item_type == 'application/vnd.apple.mpegurl':
  57. formats.extend(self._extract_m3u8_formats(
  58. file_url, video_id, 'mp4',
  59. entry_protocol='m3u8_native', m3u8_id='hls',
  60. fatal=False))
  61. else:
  62. ext = mimetype2ext(item.get('type'))
  63. label = item.get('label')
  64. formats.append({
  65. 'url': file_url,
  66. 'ext': ext,
  67. 'format_id': 'http-%s' % (label or ext),
  68. 'height': int_or_none(self._search_regex(
  69. r'(\d+)[pP]', label or '', 'height',
  70. default=None)),
  71. })
  72. self._sort_formats(
  73. formats, field_preference=('height', 'width', 'tbr', 'format_id'))
  74. info = self._search_json_ld(webpage, video_id, default={})
  75. if not info.get('title'):
  76. info['title'] = player['title']
  77. return merge_dicts(info, {
  78. 'id': video_id,
  79. 'thumbnail': url_or_none(options.get('poster')),
  80. 'duration': int_or_none(player.get('duration')),
  81. 'formats': formats,
  82. })