]> gitweb @ CieloNegro.org - youtube-dl.git/blob - youtube_dl/extractor/playfm.py
[ebaumsworld] Modernize
[youtube-dl.git] / youtube_dl / extractor / playfm.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     compat_urllib_parse,
9     compat_urllib_request,
10     ExtractorError,
11     float_or_none,
12     int_or_none,
13 )
14
15
16 class PlayFMIE(InfoExtractor):
17     IE_NAME = 'play.fm'
18     _VALID_URL = r'https?://(?:www\.)?play\.fm/[^?#]*(?P<upload_date>[0-9]{8})(?P<id>[0-9]{6})(?:$|[?#])'
19
20     _TEST = {
21         'url': 'http://www.play.fm/recording/leipzigelectronicmusicbatofarparis_fr20140712137220',
22         'md5': 'c505f8307825a245d0c7ad1850001f22',
23         'info_dict': {
24             'id': '137220',
25             'ext': 'mp3',
26             'title': 'LEIPZIG ELECTRONIC MUSIC @ Batofar (Paris,FR) - 2014-07-12',
27             'uploader': 'Sven Tasnadi',
28             'uploader_id': 'sventasnadi',
29             'duration': 5627.428,
30             'upload_date': '20140712',
31             'view_count': int,
32             'thumbnail': 're:^https?://.*\.jpg$',
33         },
34     }
35
36     def _real_extract(self, url):
37         mobj = re.match(self._VALID_URL, url)
38         video_id = mobj.group('id')
39         upload_date = mobj.group('upload_date')
40
41         rec_data = compat_urllib_parse.urlencode({'rec_id': video_id})
42         req = compat_urllib_request.Request(
43             'http://www.play.fm/flexRead/recording', data=rec_data)
44         req.add_header('Content-Type', 'application/x-www-form-urlencoded')
45         rec_doc = self._download_xml(req, video_id)
46
47         error_node = rec_doc.find('./error')
48         if error_node is not None:
49             raise ExtractorError('An error occured: %s (code %s)' % (
50                 error_node.text, rec_doc.find('./status').text))
51
52         recording = rec_doc.find('./recording')
53         title = recording.find('./title').text
54         view_count = int_or_none(recording.find('./stats/playcount').text)
55         duration = float_or_none(recording.find('./duration').text, scale=1000)
56         thumbnail = recording.find('./image').text
57
58         artist = recording.find('./artists/artist')
59         uploader = artist.find('./name').text
60         uploader_id = artist.find('./slug').text
61
62         video_url = '%s//%s/%s/%s/offset/0/sh/%s/rec/%s/jingle/%s/loc/%s' % (
63             'http:', recording.find('./url').text,
64             recording.find('./_class').text, recording.find('./file_id').text,
65             rec_doc.find('./uuid').text, video_id,
66             rec_doc.find('./jingle/file_id').text,
67             'http%3A%2F%2Fwww.play.fm%2Fplayer',
68         )
69
70         return {
71             'id': video_id,
72             'url': video_url,
73             'ext': 'mp3',
74             'filesize': int_or_none(recording.find('./size').text),
75             'title': title,
76             'upload_date': upload_date,
77             'view_count': view_count,
78             'duration': duration,
79             'thumbnail': thumbnail,
80             'uploader': uploader,
81             'uploader_id': uploader_id,
82         }