aparat.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. from .common import InfoExtractor
  2. from ..utils import (
  3. get_element_by_id,
  4. int_or_none,
  5. merge_dicts,
  6. mimetype2ext,
  7. url_or_none,
  8. )
  9. class AparatIE(InfoExtractor):
  10. _VALID_URL = r'https?://(?:www\.)?aparat\.com/(?:v/|video/video/embed/videohash/)(?P<id>[a-zA-Z0-9]+)'
  11. _EMBED_REGEX = [r'<iframe .*?src="(?P<url>http://www\.aparat\.com/video/[^"]+)"']
  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 _parse_options(self, webpage, video_id, fatal=True):
  31. return self._parse_json(self._search_regex(
  32. r'options\s*=\s*({.+?})\s*;', webpage, 'options', default='{}'), video_id)
  33. def _real_extract(self, url):
  34. video_id = self._match_id(url)
  35. # If available, provides more metadata
  36. webpage = self._download_webpage(url, video_id, fatal=False)
  37. options = self._parse_options(webpage, video_id, fatal=False)
  38. if not options:
  39. webpage = self._download_webpage(
  40. 'http://www.aparat.com/video/video/embed/vt/frame/showvideo/yes/videohash/' + video_id,
  41. video_id, 'Downloading embed webpage')
  42. options = self._parse_options(webpage, video_id)
  43. formats = []
  44. for sources in (options.get('multiSRC') or []):
  45. for item in sources:
  46. if not isinstance(item, dict):
  47. continue
  48. file_url = url_or_none(item.get('src'))
  49. if not file_url:
  50. continue
  51. item_type = item.get('type')
  52. if item_type == 'application/vnd.apple.mpegurl':
  53. formats.extend(self._extract_m3u8_formats(
  54. file_url, video_id, 'mp4',
  55. entry_protocol='m3u8_native', m3u8_id='hls',
  56. fatal=False))
  57. else:
  58. ext = mimetype2ext(item.get('type'))
  59. label = item.get('label')
  60. formats.append({
  61. 'url': file_url,
  62. 'ext': ext,
  63. 'format_id': 'http-%s' % (label or ext),
  64. 'height': int_or_none(self._search_regex(
  65. r'(\d+)[pP]', label or '', 'height',
  66. default=None)),
  67. })
  68. info = self._search_json_ld(webpage, video_id, default={})
  69. if not info.get('title'):
  70. info['title'] = get_element_by_id('videoTitle', webpage) or \
  71. self._html_search_meta(['og:title', 'twitter:title', 'DC.Title', 'title'], webpage, fatal=True)
  72. return merge_dicts(info, {
  73. 'id': video_id,
  74. 'thumbnail': url_or_none(options.get('poster')),
  75. 'duration': int_or_none(options.get('duration')),
  76. 'formats': formats,
  77. })