]>
Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/rai.py
   1 from __future__ 
import unicode_literals
 
   3 from .common 
import InfoExtractor
 
   4 from ..compat 
import compat_urlparse
 
  18 class RaiBaseIE(InfoExtractor
): 
  19     def _extract_relinker_formats(self
, relinker_url
, video_id
): 
  22         for platform 
in ('mon', 'flash', 'native'): 
  23             relinker 
= self
._download
_xml
( 
  24                 relinker_url
, video_id
, 
  25                 note
='Downloading XML metadata for platform %s' % platform
, 
  26                 transform_source
=fix_xml_ampersands
, 
  27                 query
={'output': 45, 'pl': platform
}, 
  28                 headers
=self
.geo_verification_headers()) 
  30             media_url 
= find_xpath_attr(relinker
, './url', 'type', 'content').text
 
  31             if media_url 
== 'http://download.rai.it/video_no_available.mp4': 
  32                 self
.raise_geo_restricted() 
  34             ext 
= determine_ext(media_url
) 
  35             if (ext 
== 'm3u8' and platform 
!= 'mon') or (ext 
== 'f4m' and platform 
!= 'flash'): 
  39                 formats
.extend(self
._extract
_m
3u8_formats
( 
  40                     media_url
, video_id
, 'mp4', 'm3u8_native', 
  41                     m3u8_id
='hls', fatal
=False)) 
  43                 manifest_url 
= update_url_query( 
  44                     media_url
.replace('manifest#live_hds.f4m', 'manifest.f4m'), 
  45                     {'hdcore': '3.7.0', 'plugin': 'aasp-3.7.0.39.44'}) 
  46                 formats
.extend(self
._extract
_f
4m
_formats
( 
  47                     manifest_url
, video_id
, f4m_id
='hds', fatal
=False)) 
  49                 bitrate 
= int_or_none(xpath_text(relinker
, 'bitrate')) 
  52                     'tbr': bitrate 
if bitrate 
> 0 else None, 
  53                     'format_id': 'http-%d' % bitrate 
if bitrate 
> 0 else 'http', 
  58     def _extract_from_content_id(self
, content_id
, base_url
): 
  59         media 
= self
._download
_json
( 
  60             'http://www.rai.tv/dl/RaiTV/programmi/media/ContentItem-%s.html?json' % content_id
, 
  61             content_id
, 'Downloading video JSON') 
  64         for image_type 
in ('image', 'image_medium', 'image_300'): 
  65             thumbnail_url 
= media
.get(image_type
) 
  68                     'url': compat_urlparse
.urljoin(base_url
, thumbnail_url
), 
  72         media_type 
= media
['type'] 
  73         if 'Audio' in media_type
: 
  75                 'format_id': media
.get('formatoAudio'), 
  76                 'url': media
['audioUrl'], 
  77                 'ext': media
.get('formatoAudio'), 
  79         elif 'Video' in media_type
: 
  80             formats
.extend(self
._extract
_relinker
_formats
(media
['mediaUri'], content_id
)) 
  81             self
._sort
_formats
(formats
) 
  83             raise ExtractorError('not a media file') 
  86         captions 
= media
.get('subtitlesUrl') 
  90             if captions
.endswith(STL_EXT
): 
  91                 captions 
= captions
[:-len(STL_EXT
)] + SRT_EXT
 
  99             'title': media
['name'], 
 100             'description': media
.get('desc'), 
 101             'thumbnails': thumbnails
, 
 102             'uploader': media
.get('author'), 
 103             'upload_date': unified_strdate(media
.get('date')), 
 104             'duration': parse_duration(media
.get('length')), 
 106             'subtitles': subtitles
, 
 110 class RaiTVIE(RaiBaseIE
): 
 111     _VALID_URL 
= r
'https?://(?:.+?\.)?(?:rai\.it|rai\.tv|rainews\.it)/dl/(?:[^/]+/)+(?:media|ondemand)/.+?-(?P<id>[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})(?:-.+?)?\.html' 
 114             'url': 'http://www.rai.tv/dl/RaiTV/programmi/media/ContentItem-cb27157f-9dd0-4aee-b788-b1f67643a391.html', 
 115             'md5': '8970abf8caf8aef4696e7b1f2adfc696', 
 117                 'id': 'cb27157f-9dd0-4aee-b788-b1f67643a391', 
 119                 'title': 'Report del 07/04/2014', 
 120                 'description': 'md5:f27c544694cacb46a078db84ec35d2d9', 
 121                 'upload_date': '20140407', 
 123                 'thumbnail': 're:^https?://.*\.jpg$', 
 128             'url': 'http://www.raisport.rai.it/dl/raiSport/media/rassegna-stampa-04a9f4bd-b563-40cf-82a6-aad3529cb4a9.html', 
 129             # HDS download, MD5 is unstable 
 131                 'id': '04a9f4bd-b563-40cf-82a6-aad3529cb4a9', 
 133                 'title': 'TG PRIMO TEMPO', 
 134                 'upload_date': '20140612', 
 136                 'thumbnail': 're:^https?://.*\.jpg$', 
 138             'skip': 'Geo-restricted to Italy', 
 141             'url': 'http://www.rainews.it/dl/rainews/media/state-of-the-net-Antonella-La-Carpia-regole-virali-7aafdea9-0e5d-49d5-88a6-7e65da67ae13.html', 
 142             'md5': '35cf7c229f22eeef43e48b5cf923bef0', 
 144                 'id': '7aafdea9-0e5d-49d5-88a6-7e65da67ae13', 
 146                 'title': 'State of the Net, Antonella La Carpia: regole virali', 
 147                 'description': 'md5:b0ba04a324126903e3da7763272ae63c', 
 148                 'upload_date': '20140613', 
 153             'url': 'http://www.rai.tv/dl/RaiTV/programmi/media/ContentItem-b4a49761-e0cc-4b14-8736-2729f6f73132-tg2.html', 
 155                 'id': 'b4a49761-e0cc-4b14-8736-2729f6f73132', 
 157                 'title': 'Alluvione in Sardegna e dissesto idrogeologico', 
 158                 'description': 'Edizione delle ore 20:30 ', 
 160             'skip': 'invalid urls', 
 163             'url': 'http://www.ilcandidato.rai.it/dl/ray/media/Il-Candidato---Primo-episodio-Le-Primarie-28e5525a-b495-45e8-a7c3-bc48ba45d2b6.html', 
 164             'md5': 'e57493e1cb8bc7c564663f363b171847', 
 166                 'id': '28e5525a-b495-45e8-a7c3-bc48ba45d2b6', 
 168                 'title': 'Il Candidato - Primo episodio: "Le Primarie"', 
 169                 'description': 'md5:364b604f7db50594678f483353164fb8', 
 170                 'upload_date': '20140923', 
 172                 'thumbnail': 're:^https?://.*\.jpg$', 
 177     def _real_extract(self
, url
): 
 178         video_id 
= self
._match
_id
(url
) 
 180         return self
._extract
_from
_content
_id
(video_id
, url
) 
 183 class RaiIE(RaiBaseIE
): 
 184     _VALID_URL 
= r
'https?://(?:.+?\.)?(?:rai\.it|rai\.tv|rainews\.it)/dl/.+?-(?P<id>[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})(?:-.+?)?\.html' 
 187             'url': 'http://www.report.rai.it/dl/Report/puntata/ContentItem-0c7a664b-d0f4-4b2c-8835-3f82e46f433e.html', 
 188             'md5': '2dd727e61114e1ee9c47f0da6914e178', 
 190                 'id': '59d69d28-6bb6-409d-a4b5-ed44096560af', 
 193                 'description': 'md5:4b1afae1364115ce5d78ed83cd2e5b3a', 
 194                 'upload_date': '20141221', 
 198             # Direct relinker URL 
 199             'url': 'http://www.rai.tv/dl/RaiTV/dirette/PublishingBlock-1912dbbf-3f96-44c3-b4cf-523681fbacbc.html?channel=EuroNews', 
 200             # HDS live stream, MD5 is unstable 
 202                 'id': '1912dbbf-3f96-44c3-b4cf-523681fbacbc', 
 206             'skip': 'Geo-restricted to Italy', 
 209             # Embedded content item ID 
 210             'url': 'http://www.tg1.rai.it/dl/tg1/2010/edizioni/ContentSet-9b6e0cba-4bef-4aef-8cf0-9f7f665b7dfb-tg1.html?item=undefined', 
 211             'md5': '84c1135ce960e8822ae63cec34441d63', 
 213                 'id': '0960e765-62c8-474a-ac4b-7eb3e2be39c8', 
 215                 'title': 'TG1 ore 20:00 del 02/07/2016', 
 216                 'upload_date': '20160702', 
 220             'url': 'http://www.rainews.it/dl/rainews/live/ContentItem-3156f2f2-dc70-4953-8e2f-70d7489d4ce9.html', 
 221             # HDS live stream, MD5 is unstable 
 223                 'id': '3156f2f2-dc70-4953-8e2f-70d7489d4ce9', 
 225                 'title': 'La diretta di Rainews24', 
 231     def suitable(cls
, url
): 
 232         return False if RaiTVIE
.suitable(url
) else super(RaiIE
, cls
).suitable(url
) 
 234     def _real_extract(self
, url
): 
 235         video_id 
= self
._match
_id
(url
) 
 236         webpage 
= self
._download
_webpage
(url
, video_id
) 
 238         iframe_url 
= self
._search
_regex
( 
 239             [r
'<iframe[^>]+src="([^"]*/dl/[^"]+\?iframe\b[^"]*)"', 
 240              r
'drawMediaRaiTV\(["\'](.+?
)["\']'], 
 241             webpage, 'iframe', default=None) 
 243             if not iframe_url.startswith('http'): 
 244                 iframe_url = compat_urlparse.urljoin(url, iframe_url) 
 245             return self.url_result(iframe_url) 
 247         content_item_id = self._search_regex( 
 248             r'initEdizione\((?P<q1>[\'"])ContentItem
-(?P
<content_id
>[^
\'"]+)(?P=q1)', 
 249             webpage, 'content item ID', group='content_id', default=None) 
 251             return self._extract_from_content_id(content_item_id, url) 
 253         relinker_url = compat_urlparse.urljoin(url, self._search_regex( 
 254             r'(?:var\s+videoURL|mediaInfo\.mediaUri)\s*=\s*(?P<q1>[\'"])(?P
<url
>(https?
:)?
//mediapolis\
.rai\
.it
/relinker
/relinkerServlet\
.htm
\?cont
=\d
+)(?P
=q1
)', 
 255             webpage, 'relinker URL
', group='url
')) 
 256         formats = self._extract_relinker_formats(relinker_url, video_id) 
 257         self._sort_formats(formats) 
 259         title = self._search_regex( 
 260             r'var\s
+videoTitolo\s
*=\s
*([\'"])(?P<title>[^\'"]+)\
1', 
 261             webpage, 'title
', group='title
', default=None) or self._og_search_title(webpage)