[Lecture2Go] Add new extractor
[youtube-dl] / youtube_dl / extractor / svtplay.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     determine_ext,
9 )
10
11
12 class SVTPlayIE(InfoExtractor):
13     IE_DESC = 'SVT Play and Öppet arkiv'
14     _VALID_URL = r'https?://(?:www\.)?(?P<host>svtplay|oppetarkiv)\.se/video/(?P<id>[0-9]+)'
15     _TESTS = [{
16         'url': 'http://www.svtplay.se/video/2609989/sm-veckan/sm-veckan-rally-final-sasong-1-sm-veckan-rally-final',
17         'md5': 'ade3def0643fa1c40587a422f98edfd9',
18         'info_dict': {
19             'id': '2609989',
20             'ext': 'flv',
21             'title': 'SM veckan vinter, Örebro - Rally, final',
22             'duration': 4500,
23             'thumbnail': 're:^https?://.*[\.-]jpg$',
24             'age_limit': 0,
25         },
26     }, {
27         'url': 'http://www.oppetarkiv.se/video/1058509/rederiet-sasong-1-avsnitt-1-av-318',
28         'md5': 'c3101a17ce9634f4c1f9800f0746c187',
29         'info_dict': {
30             'id': '1058509',
31             'ext': 'flv',
32             'title': 'Farlig kryssning',
33             'duration': 2566,
34             'thumbnail': 're:^https?://.*[\.-]jpg$',
35             'age_limit': 0,
36         },
37         'skip': 'Only works from Sweden',
38     }]
39
40     def _real_extract(self, url):
41         mobj = re.match(self._VALID_URL, url)
42         video_id = mobj.group('id')
43         host = mobj.group('host')
44
45         info = self._download_json(
46             'http://www.%s.se/video/%s?output=json' % (host, video_id), video_id)
47
48         title = info['context']['title']
49         thumbnail = info['context'].get('thumbnailImage')
50
51         video_info = info['video']
52         formats = []
53         for vr in video_info['videoReferences']:
54             vurl = vr['url']
55             ext = determine_ext(vurl)
56             if ext == 'm3u8':
57                 formats.extend(self._extract_m3u8_formats(
58                     vurl, video_id,
59                     ext='mp4', entry_protocol='m3u8_native',
60                     m3u8_id=vr.get('playerType')))
61             elif ext == 'f4m':
62                 formats.extend(self._extract_f4m_formats(
63                     vurl + '?hdcore=3.3.0', video_id,
64                     f4m_id=vr.get('playerType')))
65             else:
66                 formats.append({
67                     'format_id': vr.get('playerType'),
68                     'url': vurl,
69                 })
70         self._sort_formats(formats)
71
72         duration = video_info.get('materialLength')
73         age_limit = 18 if video_info.get('inappropriateForChildren') else 0
74
75         return {
76             'id': video_id,
77             'title': title,
78             'formats': formats,
79             'thumbnail': thumbnail,
80             'duration': duration,
81             'age_limit': age_limit,
82         }