logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

aparat.py (3206B)


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