orf.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import compat_str
  6. from ..utils import (
  7. clean_html,
  8. determine_ext,
  9. float_or_none,
  10. HEADRequest,
  11. int_or_none,
  12. orderedSet,
  13. remove_end,
  14. str_or_none,
  15. strip_jsonp,
  16. unescapeHTML,
  17. unified_strdate,
  18. url_or_none,
  19. )
  20. class ORFTVthekIE(InfoExtractor):
  21. IE_NAME = 'orf:tvthek'
  22. IE_DESC = 'ORF TVthek'
  23. _VALID_URL = r'https?://tvthek\.orf\.at/(?:[^/]+/)+(?P<id>\d+)'
  24. _TESTS = [{
  25. 'url': 'http://tvthek.orf.at/program/Aufgetischt/2745173/Aufgetischt-Mit-der-Steirischen-Tafelrunde/8891389',
  26. 'playlist': [{
  27. 'md5': '2942210346ed779588f428a92db88712',
  28. 'info_dict': {
  29. 'id': '8896777',
  30. 'ext': 'mp4',
  31. 'title': 'Aufgetischt: Mit der Steirischen Tafelrunde',
  32. 'description': 'md5:c1272f0245537812d4e36419c207b67d',
  33. 'duration': 2668,
  34. 'upload_date': '20141208',
  35. },
  36. }],
  37. 'skip': 'Blocked outside of Austria / Germany',
  38. }, {
  39. 'url': 'http://tvthek.orf.at/topic/Im-Wandel-der-Zeit/8002126/Best-of-Ingrid-Thurnher/7982256',
  40. 'info_dict': {
  41. 'id': '7982259',
  42. 'ext': 'mp4',
  43. 'title': 'Best of Ingrid Thurnher',
  44. 'upload_date': '20140527',
  45. 'description': 'Viele Jahre war Ingrid Thurnher das "Gesicht" der ZIB 2. Vor ihrem Wechsel zur ZIB 2 im Jahr 1995 moderierte sie unter anderem "Land und Leute", "Österreich-Bild" und "Niederösterreich heute".',
  46. },
  47. 'params': {
  48. 'skip_download': True, # rtsp downloads
  49. },
  50. 'skip': 'Blocked outside of Austria / Germany',
  51. }, {
  52. 'url': 'http://tvthek.orf.at/topic/Fluechtlingskrise/10463081/Heimat-Fremde-Heimat/13879132/Senioren-betreuen-Migrantenkinder/13879141',
  53. 'only_matching': True,
  54. }, {
  55. 'url': 'http://tvthek.orf.at/profile/Universum/35429',
  56. 'only_matching': True,
  57. }]
  58. def _real_extract(self, url):
  59. playlist_id = self._match_id(url)
  60. webpage = self._download_webpage(url, playlist_id)
  61. data_jsb = self._parse_json(
  62. self._search_regex(
  63. r'<div[^>]+class=(["\']).*?VideoPlaylist.*?\1[^>]+data-jsb=(["\'])(?P<json>.+?)\2',
  64. webpage, 'playlist', group='json'),
  65. playlist_id, transform_source=unescapeHTML)['playlist']['videos']
  66. entries = []
  67. for sd in data_jsb:
  68. video_id, title = sd.get('id'), sd.get('title')
  69. if not video_id or not title:
  70. continue
  71. video_id = compat_str(video_id)
  72. formats = []
  73. for fd in sd['sources']:
  74. src = url_or_none(fd.get('src'))
  75. if not src:
  76. continue
  77. format_id_list = []
  78. for key in ('delivery', 'quality', 'quality_string'):
  79. value = fd.get(key)
  80. if value:
  81. format_id_list.append(value)
  82. format_id = '-'.join(format_id_list)
  83. ext = determine_ext(src)
  84. if ext == 'm3u8':
  85. m3u8_formats = self._extract_m3u8_formats(
  86. src, video_id, 'mp4', m3u8_id=format_id, fatal=False)
  87. if any('/geoprotection' in f['url'] for f in m3u8_formats):
  88. self.raise_geo_restricted()
  89. formats.extend(m3u8_formats)
  90. elif ext == 'f4m':
  91. formats.extend(self._extract_f4m_formats(
  92. src, video_id, f4m_id=format_id, fatal=False))
  93. else:
  94. formats.append({
  95. 'format_id': format_id,
  96. 'url': src,
  97. 'protocol': fd.get('protocol'),
  98. })
  99. # Check for geoblocking.
  100. # There is a property is_geoprotection, but that's always false
  101. geo_str = sd.get('geoprotection_string')
  102. if geo_str:
  103. try:
  104. http_url = next(
  105. f['url']
  106. for f in formats
  107. if re.match(r'^https?://.*\.mp4$', f['url']))
  108. except StopIteration:
  109. pass
  110. else:
  111. req = HEADRequest(http_url)
  112. self._request_webpage(
  113. req, video_id,
  114. note='Testing for geoblocking',
  115. errnote=((
  116. 'This video seems to be blocked outside of %s. '
  117. 'You may want to try the streaming-* formats.')
  118. % geo_str),
  119. fatal=False)
  120. self._check_formats(formats, video_id)
  121. self._sort_formats(formats)
  122. subtitles = {}
  123. for sub in sd.get('subtitles', []):
  124. sub_src = sub.get('src')
  125. if not sub_src:
  126. continue
  127. subtitles.setdefault(sub.get('lang', 'de-AT'), []).append({
  128. 'url': sub_src,
  129. })
  130. upload_date = unified_strdate(sd.get('created_date'))
  131. entries.append({
  132. '_type': 'video',
  133. 'id': video_id,
  134. 'title': title,
  135. 'formats': formats,
  136. 'subtitles': subtitles,
  137. 'description': sd.get('description'),
  138. 'duration': int_or_none(sd.get('duration_in_seconds')),
  139. 'upload_date': upload_date,
  140. 'thumbnail': sd.get('image_full_url'),
  141. })
  142. return {
  143. '_type': 'playlist',
  144. 'entries': entries,
  145. 'id': playlist_id,
  146. }
  147. class ORFRadioIE(InfoExtractor):
  148. def _real_extract(self, url):
  149. mobj = re.match(self._VALID_URL, url)
  150. station = mobj.group('station')
  151. show_date = mobj.group('date')
  152. show_id = mobj.group('show')
  153. data = self._download_json(
  154. 'http://audioapi.orf.at/%s/api/json/current/broadcast/%s/%s'
  155. % (station, show_id, show_date), show_id)
  156. entries = []
  157. for info in data['streams']:
  158. loop_stream_id = str_or_none(info.get('loopStreamId'))
  159. if not loop_stream_id:
  160. continue
  161. title = str_or_none(data.get('title'))
  162. if not title:
  163. continue
  164. start = int_or_none(info.get('start'), scale=1000)
  165. end = int_or_none(info.get('end'), scale=1000)
  166. duration = end - start if end and start else None
  167. entries.append({
  168. 'id': loop_stream_id.replace('.mp3', ''),
  169. 'url': 'http://loopstream01.apa.at/?channel=%s&id=%s' % (station, loop_stream_id),
  170. 'title': title,
  171. 'description': clean_html(data.get('subtitle')),
  172. 'duration': duration,
  173. 'timestamp': start,
  174. 'ext': 'mp3',
  175. 'series': data.get('programTitle'),
  176. })
  177. return {
  178. '_type': 'playlist',
  179. 'id': show_id,
  180. 'title': data.get('title'),
  181. 'description': clean_html(data.get('subtitle')),
  182. 'entries': entries,
  183. }
  184. class ORFFM4IE(ORFRadioIE):
  185. IE_NAME = 'orf:fm4'
  186. IE_DESC = 'radio FM4'
  187. _VALID_URL = r'https?://(?P<station>fm4)\.orf\.at/player/(?P<date>[0-9]+)/(?P<show>4\w+)'
  188. _TEST = {
  189. 'url': 'http://fm4.orf.at/player/20170107/4CC',
  190. 'md5': '2b0be47375432a7ef104453432a19212',
  191. 'info_dict': {
  192. 'id': '2017-01-07_2100_tl_54_7DaysSat18_31295',
  193. 'ext': 'mp3',
  194. 'title': 'Solid Steel Radioshow',
  195. 'description': 'Die Mixshow von Coldcut und Ninja Tune.',
  196. 'duration': 3599,
  197. 'timestamp': 1483819257,
  198. 'upload_date': '20170107',
  199. },
  200. 'skip': 'Shows from ORF radios are only available for 7 days.',
  201. 'only_matching': True,
  202. }
  203. class ORFOE1IE(ORFRadioIE):
  204. IE_NAME = 'orf:oe1'
  205. IE_DESC = 'Radio Österreich 1'
  206. _VALID_URL = r'https?://(?P<station>oe1)\.orf\.at/player/(?P<date>[0-9]+)/(?P<show>\w+)'
  207. _TEST = {
  208. 'url': 'http://oe1.orf.at/player/20170108/456544',
  209. 'md5': '34d8a6e67ea888293741c86a099b745b',
  210. 'info_dict': {
  211. 'id': '2017-01-08_0759_tl_51_7DaysSun6_256141',
  212. 'ext': 'mp3',
  213. 'title': 'Morgenjournal',
  214. 'duration': 609,
  215. 'timestamp': 1483858796,
  216. 'upload_date': '20170108',
  217. },
  218. 'skip': 'Shows from ORF radios are only available for 7 days.'
  219. }
  220. class ORFIPTVIE(InfoExtractor):
  221. IE_NAME = 'orf:iptv'
  222. IE_DESC = 'iptv.ORF.at'
  223. _VALID_URL = r'https?://iptv\.orf\.at/(?:#/)?stories/(?P<id>\d+)'
  224. _TEST = {
  225. 'url': 'http://iptv.orf.at/stories/2275236/',
  226. 'md5': 'c8b22af4718a4b4af58342529453e3e5',
  227. 'info_dict': {
  228. 'id': '350612',
  229. 'ext': 'flv',
  230. 'title': 'Weitere Evakuierungen um Vulkan Calbuco',
  231. 'description': 'md5:d689c959bdbcf04efeddedbf2299d633',
  232. 'duration': 68.197,
  233. 'thumbnail': r're:^https?://.*\.jpg$',
  234. 'upload_date': '20150425',
  235. },
  236. }
  237. def _real_extract(self, url):
  238. story_id = self._match_id(url)
  239. webpage = self._download_webpage(
  240. 'http://iptv.orf.at/stories/%s' % story_id, story_id)
  241. video_id = self._search_regex(
  242. r'data-video(?:id)?="(\d+)"', webpage, 'video id')
  243. data = self._download_json(
  244. 'http://bits.orf.at/filehandler/static-api/json/current/data.json?file=%s' % video_id,
  245. video_id)[0]
  246. duration = float_or_none(data['duration'], 1000)
  247. video = data['sources']['default']
  248. load_balancer_url = video['loadBalancerUrl']
  249. abr = int_or_none(video.get('audioBitrate'))
  250. vbr = int_or_none(video.get('bitrate'))
  251. fps = int_or_none(video.get('videoFps'))
  252. width = int_or_none(video.get('videoWidth'))
  253. height = int_or_none(video.get('videoHeight'))
  254. thumbnail = video.get('preview')
  255. rendition = self._download_json(
  256. load_balancer_url, video_id, transform_source=strip_jsonp)
  257. f = {
  258. 'abr': abr,
  259. 'vbr': vbr,
  260. 'fps': fps,
  261. 'width': width,
  262. 'height': height,
  263. }
  264. formats = []
  265. for format_id, format_url in rendition['redirect'].items():
  266. if format_id == 'rtmp':
  267. ff = f.copy()
  268. ff.update({
  269. 'url': format_url,
  270. 'format_id': format_id,
  271. })
  272. formats.append(ff)
  273. elif determine_ext(format_url) == 'f4m':
  274. formats.extend(self._extract_f4m_formats(
  275. format_url, video_id, f4m_id=format_id))
  276. elif determine_ext(format_url) == 'm3u8':
  277. formats.extend(self._extract_m3u8_formats(
  278. format_url, video_id, 'mp4', m3u8_id=format_id))
  279. else:
  280. continue
  281. self._sort_formats(formats)
  282. title = remove_end(self._og_search_title(webpage), ' - iptv.ORF.at')
  283. description = self._og_search_description(webpage)
  284. upload_date = unified_strdate(self._html_search_meta(
  285. 'dc.date', webpage, 'upload date'))
  286. return {
  287. 'id': video_id,
  288. 'title': title,
  289. 'description': description,
  290. 'duration': duration,
  291. 'thumbnail': thumbnail,
  292. 'upload_date': upload_date,
  293. 'formats': formats,
  294. }
  295. class ORFFM4StoryIE(InfoExtractor):
  296. IE_NAME = 'orf:fm4:story'
  297. IE_DESC = 'fm4.orf.at stories'
  298. _VALID_URL = r'https?://fm4\.orf\.at/stories/(?P<id>\d+)'
  299. _TEST = {
  300. 'url': 'http://fm4.orf.at/stories/2865738/',
  301. 'playlist': [{
  302. 'md5': 'e1c2c706c45c7b34cf478bbf409907ca',
  303. 'info_dict': {
  304. 'id': '547792',
  305. 'ext': 'flv',
  306. 'title': 'Manu Delago und Inner Tongue live',
  307. 'description': 'Manu Delago und Inner Tongue haben bei der FM4 Soundpark Session live alles gegeben. Hier gibt es Fotos und die gesamte Session als Video.',
  308. 'duration': 1748.52,
  309. 'thumbnail': r're:^https?://.*\.jpg$',
  310. 'upload_date': '20170913',
  311. },
  312. }, {
  313. 'md5': 'c6dd2179731f86f4f55a7b49899d515f',
  314. 'info_dict': {
  315. 'id': '547798',
  316. 'ext': 'flv',
  317. 'title': 'Manu Delago und Inner Tongue live (2)',
  318. 'duration': 1504.08,
  319. 'thumbnail': r're:^https?://.*\.jpg$',
  320. 'upload_date': '20170913',
  321. 'description': 'Manu Delago und Inner Tongue haben bei der FM4 Soundpark Session live alles gegeben. Hier gibt es Fotos und die gesamte Session als Video.',
  322. },
  323. }],
  324. }
  325. def _real_extract(self, url):
  326. story_id = self._match_id(url)
  327. webpage = self._download_webpage(url, story_id)
  328. entries = []
  329. all_ids = orderedSet(re.findall(r'data-video(?:id)?="(\d+)"', webpage))
  330. for idx, video_id in enumerate(all_ids):
  331. data = self._download_json(
  332. 'http://bits.orf.at/filehandler/static-api/json/current/data.json?file=%s' % video_id,
  333. video_id)[0]
  334. duration = float_or_none(data['duration'], 1000)
  335. video = data['sources']['q8c']
  336. load_balancer_url = video['loadBalancerUrl']
  337. abr = int_or_none(video.get('audioBitrate'))
  338. vbr = int_or_none(video.get('bitrate'))
  339. fps = int_or_none(video.get('videoFps'))
  340. width = int_or_none(video.get('videoWidth'))
  341. height = int_or_none(video.get('videoHeight'))
  342. thumbnail = video.get('preview')
  343. rendition = self._download_json(
  344. load_balancer_url, video_id, transform_source=strip_jsonp)
  345. f = {
  346. 'abr': abr,
  347. 'vbr': vbr,
  348. 'fps': fps,
  349. 'width': width,
  350. 'height': height,
  351. }
  352. formats = []
  353. for format_id, format_url in rendition['redirect'].items():
  354. if format_id == 'rtmp':
  355. ff = f.copy()
  356. ff.update({
  357. 'url': format_url,
  358. 'format_id': format_id,
  359. })
  360. formats.append(ff)
  361. elif determine_ext(format_url) == 'f4m':
  362. formats.extend(self._extract_f4m_formats(
  363. format_url, video_id, f4m_id=format_id))
  364. elif determine_ext(format_url) == 'm3u8':
  365. formats.extend(self._extract_m3u8_formats(
  366. format_url, video_id, 'mp4', m3u8_id=format_id))
  367. else:
  368. continue
  369. self._sort_formats(formats)
  370. title = remove_end(self._og_search_title(webpage), ' - fm4.ORF.at')
  371. if idx >= 1:
  372. # Titles are duplicates, make them unique
  373. title += ' (' + str(idx + 1) + ')'
  374. description = self._og_search_description(webpage)
  375. upload_date = unified_strdate(self._html_search_meta(
  376. 'dc.date', webpage, 'upload date'))
  377. entries.append({
  378. 'id': video_id,
  379. 'title': title,
  380. 'description': description,
  381. 'duration': duration,
  382. 'thumbnail': thumbnail,
  383. 'upload_date': upload_date,
  384. 'formats': formats,
  385. })
  386. return self.playlist_result(entries)