[chirbit] Simplify and extract profile from RSS (#5032)
[youtube-dl] / youtube_dl / extractor / chirbit.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import (
6     parse_duration,
7     int_or_none,
8 )
9
10
11 class ChirbitIE(InfoExtractor):
12     _VALID_URL = r'https?://(?:www\.)?chirb\.it/(?:(?:wp|pl)/|fb_chirbit_player\.swf\?key=)?(?P<id>[\da-zA-Z]+)'
13     _TESTS = [{
14         'url': 'http://chirb.it/PrIPv5',
15         'md5': '9847b0dad6ac3e074568bf2cfb197de8',
16         'info_dict': {
17             'id': 'PrIPv5',
18             'ext': 'mp3',
19             'title': 'Фасадстрой',
20             'duration': 52,
21             'view_count': int,
22             'comment_count': int,
23         }
24     }, {
25         'url': 'https://chirb.it/fb_chirbit_player.swf?key=PrIPv5',
26         'only_matching': True,
27     }]
28
29     def _real_extract(self, url):
30         audio_id = self._match_id(url)
31
32         webpage = self._download_webpage(
33             'http://chirb.it/%s' % audio_id, audio_id)
34
35         audio_url = self._search_regex(
36             r'"setFile"\s*,\s*"([^"]+)"', webpage, 'audio url')
37
38         title = self._search_regex(
39             r'itemprop="name">([^<]+)', webpage, 'title')
40         duration = parse_duration(self._html_search_meta(
41             'duration', webpage, 'duration', fatal=False))
42         view_count = int_or_none(self._search_regex(
43             r'itemprop="playCount"\s*>(\d+)', webpage,
44             'listen count', fatal=False))
45         comment_count = int_or_none(self._search_regex(
46             r'>(\d+) Comments?:', webpage,
47             'comment count', fatal=False))
48
49         return {
50             'id': audio_id,
51             'url': audio_url,
52             'title': title,
53             'duration': duration,
54             'view_count': view_count,
55             'comment_count': comment_count,
56         }
57
58
59 class ChirbitProfileIE(InfoExtractor):
60     _VALID_URL = r'https?://(?:www\.)?chirbit.com/(?:rss/)?(?P<id>[^/]+)'
61     _TEST = {
62         'url': 'http://chirbit.com/ScarletBeauty',
63         'info_dict': {
64             'id': 'ScarletBeauty',
65             'title': 'Chirbits by ScarletBeauty',
66         },
67         'playlist_mincount': 3,
68     }
69
70     def _real_extract(self, url):
71         profile_id = self._match_id(url)
72
73         rss = self._download_xml(
74             'http://chirbit.com/rss/%s' % profile_id, profile_id)
75
76         entries = [
77             self.url_result(audio_url.text, 'Chirbit')
78             for audio_url in rss.findall('./channel/item/link')]
79
80         title = rss.find('./channel/title').text
81
82         return self.playlist_result(entries, profile_id, title)