[asiancrush] Fix extraction (closes #15630)
[ytdl] / youtube_dl / extractor / asiancrush.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from .kaltura import KalturaIE
8 from ..utils import (
9     extract_attributes,
10     remove_end,
11 )
12
13
14 class AsianCrushIE(InfoExtractor):
15     _VALID_URL = r'https?://(?:www\.)?asiancrush\.com/video/(?:[^/]+/)?0+(?P<id>\d+)v\b'
16     _TESTS = [{
17         'url': 'https://www.asiancrush.com/video/012869v/women-who-flirt/',
18         'md5': 'c3b740e48d0ba002a42c0b72857beae6',
19         'info_dict': {
20             'id': '1_y4tmjm5r',
21             'ext': 'mp4',
22             'title': 'Women Who Flirt',
23             'description': 'md5:3db14e9186197857e7063522cb89a805',
24             'timestamp': 1496936429,
25             'upload_date': '20170608',
26             'uploader_id': 'craig@crifkin.com',
27         },
28     }, {
29         'url': 'https://www.asiancrush.com/video/she-was-pretty/011886v-pretty-episode-3/',
30         'only_matching': True,
31     }]
32
33     def _real_extract(self, url):
34         video_id = self._match_id(url)
35
36         webpage = self._download_webpage(url, video_id)
37
38         entry_id, partner_id, title = [None] * 3
39
40         vars = self._parse_json(
41             self._search_regex(
42                 r'iEmbedVars\s*=\s*({.+?})', webpage, 'embed vars',
43                 default='{}'), video_id, fatal=False)
44         if vars:
45             entry_id = vars.get('entry_id')
46             partner_id = vars.get('partner_id')
47             title = vars.get('vid_label')
48
49         if not entry_id:
50             entry_id = self._search_regex(
51                 r'\bentry_id["\']\s*:\s*["\'](\d+)', webpage, 'entry id')
52
53         player = self._download_webpage(
54             'https://api.asiancrush.com/embeddedVideoPlayer', video_id,
55             query={'id': entry_id})
56
57         kaltura_id = self._search_regex(
58             r'entry_id["\']\s*:\s*(["\'])(?P<id>(?:(?!\1).)+)\1', player,
59             'kaltura id', group='id')
60
61         if not partner_id:
62             partner_id = self._search_regex(
63                 r'/p(?:artner_id)?/(\d+)', player, 'partner id',
64                 default='513551')
65
66         return self.url_result(
67             'kaltura:%s:%s' % (partner_id, kaltura_id),
68             ie=KalturaIE.ie_key(), video_id=kaltura_id,
69             video_title=title)
70
71
72 class AsianCrushPlaylistIE(InfoExtractor):
73     _VALID_URL = r'https?://(?:www\.)?asiancrush\.com/series/0+(?P<id>\d+)s\b'
74     _TEST = {
75         'url': 'https://www.asiancrush.com/series/012481s/scholar-walks-night/',
76         'info_dict': {
77             'id': '12481',
78             'title': 'Scholar Who Walks the Night',
79             'description': 'md5:7addd7c5132a09fd4741152d96cce886',
80         },
81         'playlist_count': 20,
82     }
83
84     def _real_extract(self, url):
85         playlist_id = self._match_id(url)
86
87         webpage = self._download_webpage(url, playlist_id)
88
89         entries = []
90
91         for mobj in re.finditer(
92                 r'<a[^>]+href=(["\'])(?P<url>%s.*?)\1[^>]*>' % AsianCrushIE._VALID_URL,
93                 webpage):
94             attrs = extract_attributes(mobj.group(0))
95             if attrs.get('class') == 'clearfix':
96                 entries.append(self.url_result(
97                     mobj.group('url'), ie=AsianCrushIE.ie_key()))
98
99         title = remove_end(
100             self._html_search_regex(
101                 r'(?s)<h1\b[^>]\bid=["\']movieTitle[^>]+>(.+?)</h1>', webpage,
102                 'title', default=None) or self._og_search_title(
103                 webpage, default=None) or self._html_search_meta(
104                 'twitter:title', webpage, 'title',
105                 default=None) or self._search_regex(
106                 r'<title>([^<]+)</title>', webpage, 'title', fatal=False),
107             ' | AsianCrush')
108
109         description = self._og_search_description(
110             webpage, default=None) or self._html_search_meta(
111             'twitter:description', webpage, 'description', fatal=False)
112
113         return self.playlist_result(entries, playlist_id, title, description)