[npr] Add extractor
[youtube-dl] / youtube_dl / extractor / npr.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import os.path
5 import re
6
7 from ..compat import compat_urllib_parse_unquote
8 from ..utils import url_basename
9 from .common import InfoExtractor
10
11 class NprIE(InfoExtractor):
12     _VALID_URL = r'http://(?:www\.)?npr\.org/player/v2/mediaPlayer.html?.*id=(?P<id>[0-9]+)'
13     _TEST = {
14     'url': 'http://www.npr.org/player/v2/mediaPlayer.html?id=449974205',
15     'info_dict': {
16         'id': '449974205',
17         'ext': 'mp4',
18         'title': 'New Music From Beach House, Chairlift, CMJ Discoveries And More'
19     }
20 }
21
22
23     def _real_extract(self, url):
24         mobj = re.match(self._VALID_URL, url)
25         video_id = mobj.group('id')
26         webpage_url = 'http://www.npr.org/player/v2/mediaPlayer.html?id=' + video_id
27         webpage = self._download_webpage(webpage_url, video_id)
28         key = 'MDAzMzQ2MjAyMDEyMzk4MTU1MDg3ZmM3MQ010'
29         xml_url = 'http://api.npr.org/query?id=%s&apiKey=%s' % (video_id, key)
30         json_url = 'http://api.npr.org/query?id=%s&apiKey=%s&format=json' % (video_id, key)
31
32         formats = []
33         entries = []
34
35         config = self._download_json(json_url, video_id)
36
37         content = config["list"]["story"]
38
39         album_title = config["list"]["story"][0]['song'][0]['album']['albumTitle']
40         print album_title['$text']
41
42         for key in content:
43             if "audio" in key:
44                 for x in key['audio']:
45                     if x['type'] == 'standard':
46                         playlist = True
47                         song_duration = x["duration"]['$text']
48                         song_title = x["title"]["$text"]
49                         song_id = x["id"]
50
51                         for k in x["format"]:
52                             if type(x["format"][k]) is list:
53                                 for z in x["format"][k]:
54                                     formats.append({ 'format': z['type'], 
55                                                      'url'   : z['$text']
56                                               })
57                             else:
58                                 formats.append({ 'format': k, 
59                                                  'url'   : x["format"][k]['$text']
60                                       })
61
62                         entries.append({ "title":song_title,
63                                          "id":song_id,
64                                          "duration": song_duration ,
65                                          "formats":formats})
66                         formats = []
67
68         return {    '_type': 'playlist',
69                     'id' : video_id,
70                     'title' : album_title,
71                     'entries': entries  }