[wat] Make geolock a warning (Fixes #3579)
[youtube-dl] / youtube_dl / extractor / wat.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5 import time
6 import hashlib
7
8 from .common import InfoExtractor
9 from ..utils import (
10     ExtractorError,
11     unified_strdate,
12 )
13
14
15 class WatIE(InfoExtractor):
16     _VALID_URL = r'http://www\.wat\.tv/video/(?P<display_id>.*)-(?P<short_id>.*?)_.*?\.html'
17     IE_NAME = 'wat.tv'
18     _TEST = {
19         'url': 'http://www.wat.tv/video/soupe-figues-l-orange-aux-epices-6z1uz_2hvf7_.html',
20         'md5': 'ce70e9223945ed26a8056d413ca55dc9',
21         'info_dict': {
22             'id': '11713067',
23             'display_id': 'soupe-figues-l-orange-aux-epices',
24             'ext': 'mp4',
25             'title': 'Soupe de figues à l\'orange et aux épices',
26             'description': 'Retrouvez l\'émission "Petits plats en équilibre", diffusée le 18 août 2014.',
27             'upload_date': '20140819',
28             'duration': 120,
29         },
30     }
31
32     def download_video_info(self, real_id):
33         # 'contentv4' is used in the website, but it also returns the related
34         # videos, we don't need them
35         info = self._download_json('http://www.wat.tv/interface/contentv3/' + real_id, real_id)
36         return info['media']
37
38     def _real_extract(self, url):
39         def real_id_for_chapter(chapter):
40             return chapter['tc_start'].split('-')[0]
41         mobj = re.match(self._VALID_URL, url)
42         short_id = mobj.group('short_id')
43         display_id = mobj.group('display_id')
44         webpage = self._download_webpage(url, display_id or short_id)
45         real_id = self._search_regex(r'xtpage = ".*-(.*?)";', webpage, 'real id')
46
47         video_info = self.download_video_info(real_id)
48
49         if video_info.get('geolock'):
50             self.report_warning(
51                 'This content is marked as not available in your area. Trying anyway ..')
52
53         chapters = video_info['chapters']
54         first_chapter = chapters[0]
55         files = video_info['files']
56         first_file = files[0]
57
58         if real_id_for_chapter(first_chapter) != real_id:
59             self.to_screen('Multipart video detected')
60             chapter_urls = []
61             for chapter in chapters:
62                 chapter_id = real_id_for_chapter(chapter)
63                 # Yes, when we this chapter is processed by WatIE,
64                 # it will download the info again
65                 chapter_info = self.download_video_info(chapter_id)
66                 chapter_urls.append(chapter_info['url'])
67             entries = [self.url_result(chapter_url) for chapter_url in chapter_urls]
68             return self.playlist_result(entries, real_id, video_info['title'])
69
70         upload_date = None
71         if 'date_diffusion' in first_chapter:
72             upload_date = unified_strdate(first_chapter['date_diffusion'])
73         # Otherwise we can continue and extract just one part, we have to use
74         # the short id for getting the video url
75
76         formats = [{
77             'url': 'http://wat.tv/get/android5/%s.mp4' % real_id,
78             'format_id': 'Mobile',
79         }]
80
81         fmts = [('SD', 'web')]
82         if first_file.get('hasHD'):
83             fmts.append(('HD', 'webhd'))
84
85         def compute_token(param):
86             timestamp = '%08x' % int(time.time())
87             magic = '9b673b13fa4682ed14c3cfa5af5310274b514c4133e9b3a81e6e3aba009l2564'
88             return '%s/%s' % (hashlib.md5((magic + param + timestamp).encode('ascii')).hexdigest(), timestamp)
89
90         for fmt in fmts:
91             webid = '/%s/%s' % (fmt[1], real_id)
92             video_url = self._download_webpage(
93                 'http://www.wat.tv/get%s?token=%s&getURL=1' % (webid, compute_token(webid)),
94                 real_id,
95                 'Downloding %s video URL' % fmt[0],
96                 'Failed to download %s video URL' % fmt[0],
97                 False)
98             if not video_url:
99                 continue
100             formats.append({
101                 'url': video_url,
102                 'ext': 'mp4',
103                 'format_id': fmt[0],
104             })
105
106         return {
107             'id': real_id,
108             'display_id': display_id,
109             'title': first_chapter['title'],
110             'thumbnail': first_chapter['preview'],
111             'description': first_chapter['description'],
112             'view_count': video_info['views'],
113             'upload_date': upload_date,
114             'duration': first_file['duration'],
115             'formats': formats,
116         }