[allocine] Fix extraction (closes #10860)
[youtube-dl] / youtube_dl / extractor / allocine.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import (
6     qualities,
7     url_basename,
8 )
9
10
11 class AllocineIE(InfoExtractor):
12     _VALID_URL = r'https?://(?:www\.)?allocine\.fr/(?:article|video|film)/(?:fichearticle_gen_carticle=|player_gen_cmedia=|fichefilm_gen_cfilm=|video-)(?P<id>[0-9]+)(?:\.html)?'
13
14     _TESTS = [{
15         'url': 'http://www.allocine.fr/article/fichearticle_gen_carticle=18635087.html',
16         'md5': '0c9fcf59a841f65635fa300ac43d8269',
17         'info_dict': {
18             'id': '19546517',
19             'display_id': '18635087',
20             'ext': 'mp4',
21             'title': 'Astérix - Le Domaine des Dieux Teaser VF',
22             'description': 'md5:4a754271d9c6f16c72629a8a993ee884',
23             'thumbnail': 're:http://.*\.jpg',
24         },
25     }, {
26         'url': 'http://www.allocine.fr/video/player_gen_cmedia=19540403&cfilm=222257.html',
27         'md5': 'd0cdce5d2b9522ce279fdfec07ff16e0',
28         'info_dict': {
29             'id': '19540403',
30             'display_id': '19540403',
31             'ext': 'mp4',
32             'title': 'Planes 2 Bande-annonce VF',
33             'description': 'Regardez la bande annonce du film Planes 2 (Planes 2 Bande-annonce VF). Planes 2, un film de Roberts Gannaway',
34             'thumbnail': 're:http://.*\.jpg',
35         },
36     }, {
37         'url': 'http://www.allocine.fr/video/player_gen_cmedia=19544709&cfilm=181290.html',
38         'md5': '101250fb127ef9ca3d73186ff22a47ce',
39         'info_dict': {
40             'id': '19544709',
41             'display_id': '19544709',
42             'ext': 'mp4',
43             'title': 'Dragons 2 - Bande annonce finale VF',
44             'description': 'md5:6cdd2d7c2687d4c6aafe80a35e17267a',
45             'thumbnail': 're:http://.*\.jpg',
46         },
47     }, {
48         'url': 'http://www.allocine.fr/video/video-19550147/',
49         'only_matching': True,
50     }]
51
52     def _real_extract(self, url):
53         display_id = self._match_id(url)
54
55         webpage = self._download_webpage(url, display_id)
56
57         model = self._html_search_regex(
58             r'data-model="([^"]+)"', webpage, 'data model')
59         model_data = self._parse_json(model, display_id)
60
61         quality = qualities(['ld', 'md', 'hd'])
62
63         formats = []
64         for video_url in model_data['sources'].values():
65             video_id, format_id = url_basename(video_url).split('_')[:2]
66             formats.append({
67                 'format_id': format_id,
68                 'quality': quality(format_id),
69                 'url': video_url,
70             })
71         self._sort_formats(formats)
72
73         return {
74             'id': video_id,
75             'display_id': display_id,
76             'title': model_data['title'],
77             'thumbnail': self._og_search_thumbnail(webpage),
78             'formats': formats,
79             'description': self._og_search_description(webpage),
80         }