]>
Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/twentytwotracks.py
   1 from __future__ 
import unicode_literals
 
   5 from .common 
import InfoExtractor
 
   6 from ..utils 
import int_or_none
 
   8 # 22Tracks regularly replace the audio tracks that can be streamed on their 
   9 # site. The tracks usually expire after 1 months, so we can't add tests. 
  12 class TwentyTwoTracksIE(InfoExtractor
): 
  13     _VALID_URL 
= r
'https?://22tracks\.com/(?P<city>[a-z]+)/(?P<genre>[\da-z]+)/(?P<id>\d+)' 
  14     IE_NAME 
= '22tracks:track' 
  16     _API_BASE 
= 'http://22tracks.com/api' 
  18     def _extract_info(self
, city
, genre_name
, track_id
=None): 
  19         item_id 
= track_id 
if track_id 
else genre_name
 
  21         cities 
= self
._download
_json
( 
  22             '%s/cities' % self
._API
_BASE
, item_id
, 
  23             'Downloading cities info', 
  24             'Unable to download cities info') 
  25         city_id 
= [x
['id'] for x 
in cities 
if x
['slug'] == city
][0] 
  27         genres 
= self
._download
_json
( 
  28             '%s/genres/%s' % (self
._API
_BASE
, city_id
), item_id
, 
  29             'Downloading %s genres info' % city
, 
  30             'Unable to download %s genres info' % city
) 
  31         genre 
= [x 
for x 
in genres 
if x
['slug'] == genre_name
][0] 
  32         genre_id 
= genre
['id'] 
  34         tracks 
= self
._download
_json
( 
  35             '%s/tracks/%s' % (self
._API
_BASE
, genre_id
), item_id
, 
  36             'Downloading %s genre tracks info' % genre_name
, 
  37             'Unable to download track info') 
  39         return [x 
for x 
in tracks 
if x
['id'] == item_id
][0] if track_id 
else [genre
['title'], tracks
] 
  41     def _get_track_url(self
, filename
, track_id
): 
  42         token 
= self
._download
_json
( 
  43             'http://22tracks.com/token.php?desktop=true&u=/128/%s' % filename
, 
  44             track_id
, 'Downloading token', 'Unable to download token') 
  45         return 'http://audio.22tracks.com%s?st=%s&e=%d' % (token
['filename'], token
['st'], token
['e']) 
  47     def _extract_track_info(self
, track_info
, track_id
): 
  48         download_url 
= self
._get
_track
_url
(track_info
['filename'], track_id
) 
  49         title 
= '%s - %s' % (track_info
['artist'].strip(), track_info
['title'].strip()) 
  55             'duration': int_or_none(track_info
.get('duration')), 
  56             'timestamp': int_or_none(track_info
.get('published_at') or track_info
.get('created')) 
  59     def _real_extract(self
, url
): 
  60         mobj 
= re
.match(self
._VALID
_URL
, url
) 
  62         city 
= mobj
.group('city') 
  63         genre 
= mobj
.group('genre') 
  64         track_id 
= mobj
.group('id') 
  66         track_info 
= self
._extract
_info
(city
, genre
, track_id
) 
  67         return self
._extract
_track
_info
(track_info
, track_id
) 
  70 class TwentyTwoTracksGenreIE(TwentyTwoTracksIE
): 
  71     _VALID_URL 
= r
'https?://22tracks\.com/(?P<city>[a-z]+)/(?P<genre>[\da-z]+)/?$' 
  72     IE_NAME 
= '22tracks:genre' 
  74     def _real_extract(self
, url
): 
  75         mobj 
= re
.match(self
._VALID
_URL
, url
) 
  77         city 
= mobj
.group('city') 
  78         genre 
= mobj
.group('genre') 
  80         genre_title
, tracks 
= self
._extract
_info
(city
, genre
) 
  83             self
._extract
_track
_info
(track_info
, track_info
['id']) 
  84             for track_info 
in tracks
] 
  86         return self
.playlist_result(entries
, genre
, genre_title
)