]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/playfm.py
[extractor] Common function `_match_valid_url`
[yt-dlp.git] / yt_dlp / extractor / playfm.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4
5 from .common import InfoExtractor
6 from ..compat import compat_str
7 from ..utils import (
8 ExtractorError,
9 int_or_none,
10 parse_iso8601,
11 )
12
13
14 class PlayFMIE(InfoExtractor):
15 IE_NAME = 'play.fm'
16 _VALID_URL = r'https?://(?:www\.)?play\.fm/(?P<slug>(?:[^/]+/)+(?P<id>[^/]+))/?(?:$|[?#])'
17
18 _TEST = {
19 'url': 'https://www.play.fm/dan-drastic/sven-tasnadi-leipzig-electronic-music-batofar-paris-fr-2014-07-12',
20 'md5': 'c505f8307825a245d0c7ad1850001f22',
21 'info_dict': {
22 'id': '71276',
23 'ext': 'mp3',
24 'title': 'Sven Tasnadi - LEIPZIG ELECTRONIC MUSIC @ Batofar (Paris,FR) - 2014-07-12',
25 'description': '',
26 'duration': 5627,
27 'timestamp': 1406033781,
28 'upload_date': '20140722',
29 'uploader': 'Dan Drastic',
30 'uploader_id': '71170',
31 'view_count': int,
32 'comment_count': int,
33 },
34 }
35
36 def _real_extract(self, url):
37 mobj = self._match_valid_url(url)
38 video_id = mobj.group('id')
39 slug = mobj.group('slug')
40
41 recordings = self._download_json(
42 'http://v2api.play.fm/recordings/slug/%s' % slug, video_id)
43
44 error = recordings.get('error')
45 if isinstance(error, dict):
46 raise ExtractorError(
47 '%s returned error: %s' % (self.IE_NAME, error.get('message')),
48 expected=True)
49
50 audio_url = recordings['audio']
51 video_id = compat_str(recordings.get('id') or video_id)
52 title = recordings['title']
53 description = recordings.get('description')
54 duration = int_or_none(recordings.get('recordingDuration'))
55 timestamp = parse_iso8601(recordings.get('created_at'))
56 uploader = recordings.get('page', {}).get('title')
57 uploader_id = compat_str(recordings.get('page', {}).get('id'))
58 view_count = int_or_none(recordings.get('playCount'))
59 comment_count = int_or_none(recordings.get('commentCount'))
60 categories = [tag['name'] for tag in recordings.get('tags', []) if tag.get('name')]
61
62 return {
63 'id': video_id,
64 'url': audio_url,
65 'title': title,
66 'description': description,
67 'duration': duration,
68 'timestamp': timestamp,
69 'uploader': uploader,
70 'uploader_id': uploader_id,
71 'view_count': view_count,
72 'comment_count': comment_count,
73 'categories': categories,
74 }