[canal13cl] fix info extraction
[youtube-dl] / youtube_dl / extractor / tele13.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 js_to_json
8
9
10 class Tele13IE(InfoExtractor):
11     _VALID_URL = r'^http://(?:www\.)?t13\.cl/videos(?:/[^/]+)+/(?P<id>[\w-]+)'
12     _TESTS = [
13         {
14             'url': 'http://www.t13.cl/videos/actualidad/el-circulo-de-hierro-de-michelle-bachelet-en-su-regreso-a-la-moneda',
15             'md5': '4cb1fa38adcad8fea88487a078831755',
16             'info_dict': {
17                 'id': 'el-circulo-de-hierro-de-michelle-bachelet-en-su-regreso-a-la-moneda',
18                 'ext': 'mp4',
19                 'title': 'El c\u00edrculo de hierro de Michelle Bachelet en su regreso a La Moneda',
20             }
21         },
22         {
23             'url': 'http://www.t13.cl/videos/mundo/tendencias/video-captan-misteriosa-bola-fuego-cielos-bangkok',
24             'md5': '65d1ae54812c96f4b345dd21d3bb1adc',
25             'info_dict': {
26                 'id': 'rOoKv2OMpOw',
27                 'ext': 'mp4',
28                 'title': 'Shooting star seen on 7-Sep-2015',
29                 'description': 'md5:a1cd2e74f6ee6851552c9cf5851d6b06',
30                 'uploader': 'Porjai Jaturongkhakun',
31                 'upload_date': '20150906',
32                 'uploader_id': 'UCnLY_3ezwNcDSC_Wc6suZxw',
33             },
34             'add_ie': ['Youtube'],
35         }
36     ]
37
38     def _real_extract(self, url):
39         display_id = self._match_id(url)
40
41         webpage = self._download_webpage(url, display_id)
42
43         setup_js = self._parse_json(
44             js_to_json(
45                 self._search_regex(
46                     r"jwplayer\('player-vivo'\).setup\((\{.*?\})\)",
47                     webpage,
48                     'setup code',
49                     flags=re.DOTALL
50                 ).replace('\n//', '')
51             ),
52             display_id
53         )
54         title = setup_js['title']
55         thumbnail = setup_js.get('image') or setup_js['playlist'][0].get('image')
56         description = self._html_search_meta(
57             'description', webpage, 'description')
58
59         formats = []
60         for f in setup_js['playlist'][0]['sources']:
61             format_url = f['file']
62             if format_url != '':
63                 if '.m3u8' in format_url:
64                     formats.extend(self._extract_m3u8_formats(format_url, display_id))
65                 else:
66                     if 'youtube.com' in format_url:
67                         return self.url_result(format_url, 'Youtube')
68                     else:
69                         formats.append({'url': format_url, 'format_id': f.get('label')})
70
71         return {
72             'id': display_id,
73             'title': title,
74             'description': description,
75             'thumbnail': thumbnail,
76             'formats': formats,
77         }