]>
Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/ximalaya.py
   3 from __future__ 
import unicode_literals
 
   8 from .common 
import InfoExtractor
 
  11 class XimalayaBaseIE(InfoExtractor
): 
  12     _GEO_COUNTRIES 
= ['CN'] 
  15 class XimalayaIE(XimalayaBaseIE
): 
  18     _VALID_URL 
= r
'https?://(?:www\.|m\.)?ximalaya\.com/(?P<uid>[0-9]+)/sound/(?P<id>[0-9]+)' 
  19     _USER_URL_FORMAT 
= '%s://www.ximalaya.com/zhubo/%i/' 
  22             'url': 'http://www.ximalaya.com/61425525/sound/47740352/', 
  27                 'uploader_id': 61425525, 
  28                 'uploader_url': 'http://www.ximalaya.com/zhubo/61425525/', 
  29                 'title': '261.唐诗三百首.卷八.送孟浩然之广陵.李白', 
  30                 'description': "contains:《送孟浩然之广陵》\n作者:李白\n故人西辞黄鹤楼,烟花三月下扬州。\n孤帆远影碧空尽,惟见长江天际流。", 
  34                         'url': r
're:^https?://.*\.jpg$', 
  37                         'name': 'cover_url_142', 
  38                         'url': r
're:^https?://.*\.jpg$', 
  43                 'categories': ['renwen', '人文'], 
  50             'url': 'http://m.ximalaya.com/61425525/sound/47740352/', 
  55                 'uploader_id': 61425525, 
  56                 'uploader_url': 'http://www.ximalaya.com/zhubo/61425525/', 
  57                 'title': '261.唐诗三百首.卷八.送孟浩然之广陵.李白', 
  58                 'description': "contains:《送孟浩然之广陵》\n作者:李白\n故人西辞黄鹤楼,烟花三月下扬州。\n孤帆远影碧空尽,惟见长江天际流。", 
  62                         'url': r
're:^https?://.*\.jpg$', 
  65                         'name': 'cover_url_142', 
  66                         'url': r
're:^https?://.*\.jpg$', 
  71                 'categories': ['renwen', '人文'], 
  78             'url': 'https://www.ximalaya.com/11045267/sound/15705996/', 
  83                 'uploader_id': 11045267, 
  84                 'uploader_url': 'https://www.ximalaya.com/zhubo/11045267/', 
  85                 'title': 'Lesson 1 Excuse me!', 
  86                 'description': "contains:Listen to the tape then answer\xa0this question. Whose handbag is it?\n" 
  91                         'url': r
're:^https?://.*\.jpg$', 
  94                         'name': 'cover_url_142', 
  95                         'url': r
're:^https?://.*\.jpg$', 
 100                 'categories': ['train', '外语'], 
 108     def _real_extract(self
, url
): 
 110         is_m 
= 'm.ximalaya' in url
 
 111         scheme 
= 'https' if url
.startswith('https') else 'http' 
 113         audio_id 
= self
._match
_id
(url
) 
 114         webpage 
= self
._download
_webpage
(url
, audio_id
, 
 115                                          note
='Download sound page for %s' % audio_id
, 
 116                                          errnote
='Unable to get sound page') 
 118         audio_info_file 
= '%s://m.ximalaya.com/tracks/%s.json' % (scheme
, audio_id
) 
 119         audio_info 
= self
._download
_json
(audio_info_file
, audio_id
, 
 120                                          'Downloading info json %s' % audio_info_file
, 
 121                                          'Unable to download info file') 
 124         for bps
, k 
in (('24k', 'play_path_32'), ('64k', 'play_path_64')): 
 125             if audio_info
.get(k
): 
 128                     'url': audio_info
[k
], 
 132         for k 
in audio_info
.keys(): 
 133             # cover pics kyes like: cover_url', 'cover_url_142' 
 134             if k
.startswith('cover_url'): 
 135                 thumbnail 
= {'name': k
, 'url': audio_info
[k
]} 
 136                 if k 
== 'cover_url_142': 
 137                     thumbnail
['width'] = 180 
 138                     thumbnail
['height'] = 180 
 139                 thumbnails
.append(thumbnail
) 
 141         audio_uploader_id 
= audio_info
.get('uid') 
 144             audio_description 
= self
._html
_search
_regex
(r
'(?s)<section\s+class=["\']content
[^
>]+>(.+?
)</section
>', 
 145                                                         webpage, 'audio_description
', fatal=False) 
 147             audio_description = self._html_search_regex(r'(?s
)<div\s
+class=["\']rich_intro[^>]*>(.+?</article>)', 
 148                                                         webpage, 'audio_description', fatal=False) 
 150         if not audio_description: 
 151             audio_description_file = '%s://www.ximalaya.com/sounds/%s/rich_intro' % (scheme, audio_id) 
 152             audio_description = self._download_webpage(audio_description_file, audio_id, 
 153                                                        note='Downloading description file %s' % audio_description_file, 
 154                                                        errnote='Unable to download descrip file', 
 156             audio_description = audio_description.strip() if audio_description else None 
 160             'uploader': audio_info.get('nickname'), 
 161             'uploader_id': audio_uploader_id, 
 162             'uploader_url': self._USER_URL_FORMAT % (scheme, audio_uploader_id) if audio_uploader_id else None, 
 163             'title': audio_info['title'], 
 164             'thumbnails': thumbnails, 
 165             'description': audio_description, 
 166             'categories': list(filter(None, (audio_info.get('category_name'), audio_info.get('category_title')))), 
 167             'duration': audio_info.get('duration'), 
 168             'view_count': audio_info.get('play_count'), 
 169             'like_count': audio_info.get('favorites_count'), 
 174 class XimalayaAlbumIE(XimalayaBaseIE): 
 175     IE_NAME = 'ximalaya:album' 
 176     IE_DESC = '喜马拉雅FM 专辑' 
 177     _VALID_URL = r'https?://(?:www\.|m\.)?ximalaya\.com/(?P<uid>[0-9]+)/album/(?P<id>[0-9]+)' 
 178     _TEMPLATE_URL = '%s://www.ximalaya.com/%s/album/%s/' 
 179     _BASE_URL_TEMPL = '%s://www.ximalaya.com%s' 
 180     _LIST_VIDEO_RE = r'<a[^>]+?href="(?P
<url
>/%s/sound
/(?P
<id>\d
+)/?
)"[^>]+?title="(?P
<title
>[^
>]+)">' 
 182         'url': 'http://www.ximalaya.com/61425525/album/5534601/', 
 184             'title': '唐诗三百首(含赏析)', 
 187         'playlist_count': 312, 
 189         'url': 'http://m.ximalaya.com/61425525/album/5534601', 
 191             'title': '唐诗三百首(含赏析)', 
 194         'playlist_count': 312, 
 198     def _real_extract(self, url): 
 199         self.scheme = scheme = 'https' if url.startswith('https') else 'http' 
 201         mobj = re.match(self._VALID_URL, url) 
 202         uid, playlist_id = mobj.group('uid'), mobj.group('id') 
 204         webpage = self._download_webpage(self._TEMPLATE_URL % (scheme, uid, playlist_id), playlist_id, 
 205                                          note='Download album page for %s' % playlist_id, 
 206                                          errnote='Unable to get album info') 
 208         title = self._html_search_regex(r'detailContent_title[^>]*><h1(?:[^>]+)?>([^<]+)</h1>', 
 209                                         webpage, 'title', fatal=False) 
 211         return self.playlist_result(self._entries(webpage, playlist_id, uid), playlist_id, title) 
 213     def _entries(self, page, playlist_id, uid): 
 215         for page_num in itertools.count(1): 
 216             for entry in self._process_page(html, uid): 
 219             next_url = self._search_regex(r'<a\s+href=(["\'])(?P
<more
>[\S
]+)\
1[^
>]+rel
=(["\'])next\3', 
 220                                           html, 'list_next_url', default=None, group='more') 
 224             next_full_url = self._BASE_URL_TEMPL % (self.scheme, next_url) 
 225             html = self._download_webpage(next_full_url, playlist_id) 
 227     def _process_page(self, html, uid): 
 228         find_from = html.index('album_soundlist') 
 229         for mobj in re.finditer(self._LIST_VIDEO_RE % uid, html[find_from:]): 
 230             yield self.url_result(self._BASE_URL_TEMPL % (self.scheme, mobj.group('url')),