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

Generated by cgit