orf.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. HEADRequest,
  8. unified_strdate,
  9. strip_jsonp,
  10. int_or_none,
  11. float_or_none,
  12. determine_ext,
  13. remove_end,
  14. unescapeHTML,
  15. )
  16. class ORFTVthekIE(InfoExtractor):
  17. IE_NAME = 'orf:tvthek'
  18. IE_DESC = 'ORF TVthek'
  19. _VALID_URL = r'https?://tvthek\.orf\.at/(?:[^/]+/)+(?P<id>\d+)'
  20. _TESTS = [{
  21. 'url': 'http://tvthek.orf.at/program/Aufgetischt/2745173/Aufgetischt-Mit-der-Steirischen-Tafelrunde/8891389',
  22. 'playlist': [{
  23. 'md5': '2942210346ed779588f428a92db88712',
  24. 'info_dict': {
  25. 'id': '8896777',
  26. 'ext': 'mp4',
  27. 'title': 'Aufgetischt: Mit der Steirischen Tafelrunde',
  28. 'description': 'md5:c1272f0245537812d4e36419c207b67d',
  29. 'duration': 2668,
  30. 'upload_date': '20141208',
  31. },
  32. }],
  33. 'skip': 'Blocked outside of Austria / Germany',
  34. }, {
  35. 'url': 'http://tvthek.orf.at/topic/Im-Wandel-der-Zeit/8002126/Best-of-Ingrid-Thurnher/7982256',
  36. 'info_dict': {
  37. 'id': '7982259',
  38. 'ext': 'mp4',
  39. 'title': 'Best of Ingrid Thurnher',
  40. 'upload_date': '20140527',
  41. '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".',
  42. },
  43. 'params': {
  44. 'skip_download': True, # rtsp downloads
  45. },
  46. '_skip': 'Blocked outside of Austria / Germany',
  47. }, {
  48. 'url': 'http://tvthek.orf.at/topic/Fluechtlingskrise/10463081/Heimat-Fremde-Heimat/13879132/Senioren-betreuen-Migrantenkinder/13879141',
  49. 'skip_download': True,
  50. }, {
  51. 'url': 'http://tvthek.orf.at/profile/Universum/35429',
  52. 'skip_download': True,
  53. }]
  54. def _real_extract(self, url):
  55. playlist_id = self._match_id(url)
  56. webpage = self._download_webpage(url, playlist_id)
  57. data_jsb = self._parse_json(
  58. self._search_regex(
  59. r'<div[^>]+class=(["\']).*?VideoPlaylist.*?\1[^>]+data-jsb=(["\'])(?P<json>.+?)\2',
  60. webpage, 'playlist', group='json'),
  61. playlist_id, transform_source=unescapeHTML)['playlist']['videos']
  62. def quality_to_int(s):
  63. m = re.search('([0-9]+)', s)
  64. if m is None:
  65. return -1
  66. return int(m.group(1))
  67. entries = []
  68. for sd in data_jsb:
  69. video_id, title = sd.get('id'), sd.get('title')
  70. if not video_id or not title:
  71. continue
  72. video_id = compat_str(video_id)
  73. formats = [{
  74. 'preference': -10 if fd['delivery'] == 'hls' else None,
  75. 'format_id': '%s-%s-%s' % (
  76. fd['delivery'], fd['quality'], fd['quality_string']),
  77. 'url': fd['src'],
  78. 'protocol': fd['protocol'],
  79. 'quality': quality_to_int(fd['quality']),
  80. } for fd in sd['sources']]
  81. # Check for geoblocking.
  82. # There is a property is_geoprotection, but that's always false
  83. geo_str = sd.get('geoprotection_string')
  84. if geo_str:
  85. try:
  86. http_url = next(
  87. f['url']
  88. for f in formats
  89. if re.match(r'^https?://.*\.mp4$', f['url']))
  90. except StopIteration:
  91. pass
  92. else:
  93. req = HEADRequest(http_url)
  94. self._request_webpage(
  95. req, video_id,
  96. note='Testing for geoblocking',
  97. errnote=((
  98. 'This video seems to be blocked outside of %s. '
  99. 'You may want to try the streaming-* formats.')
  100. % geo_str),
  101. fatal=False)
  102. self._check_formats(formats, video_id)
  103. self._sort_formats(formats)
  104. subtitles = {}
  105. for sub in sd.get('subtitles', []):
  106. sub_src = sub.get('src')
  107. if not sub_src:
  108. continue
  109. subtitles.setdefault(sub.get('lang', 'de-AT'), []).append({
  110. 'url': sub_src,
  111. })
  112. upload_date = unified_strdate(sd.get('created_date'))
  113. entries.append({
  114. '_type': 'video',
  115. 'id': video_id,
  116. 'title': title,
  117. 'formats': formats,
  118. 'subtitles': subtitles,
  119. 'description': sd.get('description'),
  120. 'duration': int_or_none(sd.get('duration_in_seconds')),
  121. 'upload_date': upload_date,
  122. 'thumbnail': sd.get('image_full_url'),
  123. })
  124. return {
  125. '_type': 'playlist',
  126. 'entries': entries,
  127. 'id': playlist_id,
  128. }
  129. class ORFRadioIE(InfoExtractor):
  130. def _real_extract(self, url):
  131. mobj = re.match(self._VALID_URL, url)
  132. station = mobj.group('station')
  133. show_date = mobj.group('date')
  134. show_id = mobj.group('show')
  135. if station == 'fm4':
  136. show_id = '4%s' % show_id
  137. data = self._download_json(
  138. 'http://audioapi.orf.at/%s/api/json/current/broadcast/%s/%s' % (station, show_id, show_date),
  139. show_id
  140. )
  141. def extract_entry_dict(info, title, subtitle):
  142. return {
  143. 'id': info['loopStreamId'].replace('.mp3', ''),
  144. 'url': 'http://loopstream01.apa.at/?channel=%s&id=%s' % (station, info['loopStreamId']),
  145. 'title': title,
  146. 'description': subtitle,
  147. 'duration': (info['end'] - info['start']) / 1000,
  148. 'timestamp': info['start'] / 1000,
  149. 'ext': 'mp3'
  150. }
  151. entries = [extract_entry_dict(t, data['title'], data['subtitle']) for t in data['streams']]
  152. return {
  153. '_type': 'playlist',
  154. 'id': show_id,
  155. 'title': data['title'],
  156. 'description': data['subtitle'],
  157. 'entries': entries
  158. }
  159. class ORFFM4IE(ORFRadioIE):
  160. IE_NAME = 'orf:fm4'
  161. IE_DESC = 'radio FM4'
  162. _VALID_URL = r'https?://(?P<station>fm4)\.orf\.at/(?:7tage/?#|player/)(?P<date>[0-9]+)/(?P<show>\w+)'
  163. _TESTS = [
  164. {
  165. 'url': 'http://fm4.orf.at/player/20170107/CC',
  166. 'md5': '2b0be47375432a7ef104453432a19212',
  167. 'info_dict': {
  168. 'id': '2017-01-07_2100_tl_54_7DaysSat18_31295',
  169. 'ext': 'mp3',
  170. 'title': 'Solid Steel Radioshow',
  171. 'description': 'Die Mixshow von Coldcut und Ninja Tune.',
  172. 'duration': 3599,
  173. 'timestamp': 1483819257,
  174. 'upload_date': '20170107',
  175. },
  176. 'skip': 'Shows from ORF radios are only available for 7 days.'
  177. }
  178. ]
  179. class ORFOE1IE(ORFRadioIE):
  180. IE_NAME = 'orf:oe1'
  181. IE_DESC = 'Radio Österreich 1'
  182. _VALID_URL = r'https?://(?P<station>oe1)\.orf\.at/(?:7tage/?#|player/)(?P<date>[0-9]+)/(?P<show>\w+)'
  183. _TESTS = [
  184. {
  185. 'url': 'http://oe1.orf.at/player/20170108/456544',
  186. 'md5': '34d8a6e67ea888293741c86a099b745b',
  187. 'info_dict': {
  188. 'id': '2017-01-08_0759_tl_51_7DaysSun6_256141',
  189. 'ext': 'mp3',
  190. 'title': 'Morgenjournal',
  191. 'duration': 609,
  192. 'timestamp': 1483858796,
  193. 'upload_date': '20170108',
  194. },
  195. 'skip': 'Shows from ORF radios are only available for 7 days.'
  196. }
  197. ]
  198. class ORFIPTVIE(InfoExtractor):
  199. IE_NAME = 'orf:iptv'
  200. IE_DESC = 'iptv.ORF.at'
  201. _VALID_URL = r'https?://iptv\.orf\.at/(?:#/)?stories/(?P<id>\d+)'
  202. _TEST = {
  203. 'url': 'http://iptv.orf.at/stories/2275236/',
  204. 'md5': 'c8b22af4718a4b4af58342529453e3e5',
  205. 'info_dict': {
  206. 'id': '350612',
  207. 'ext': 'flv',
  208. 'title': 'Weitere Evakuierungen um Vulkan Calbuco',
  209. 'description': 'md5:d689c959bdbcf04efeddedbf2299d633',
  210. 'duration': 68.197,
  211. 'thumbnail': r're:^https?://.*\.jpg$',
  212. 'upload_date': '20150425',
  213. },
  214. }
  215. def _real_extract(self, url):
  216. story_id = self._match_id(url)
  217. webpage = self._download_webpage(
  218. 'http://iptv.orf.at/stories/%s' % story_id, story_id)
  219. video_id = self._search_regex(
  220. r'data-video(?:id)?="(\d+)"', webpage, 'video id')
  221. data = self._download_json(
  222. 'http://bits.orf.at/filehandler/static-api/json/current/data.json?file=%s' % video_id,
  223. video_id)[0]
  224. duration = float_or_none(data['duration'], 1000)
  225. video = data['sources']['default']
  226. load_balancer_url = video['loadBalancerUrl']
  227. abr = int_or_none(video.get('audioBitrate'))
  228. vbr = int_or_none(video.get('bitrate'))
  229. fps = int_or_none(video.get('videoFps'))
  230. width = int_or_none(video.get('videoWidth'))
  231. height = int_or_none(video.get('videoHeight'))
  232. thumbnail = video.get('preview')
  233. rendition = self._download_json(
  234. load_balancer_url, video_id, transform_source=strip_jsonp)
  235. f = {
  236. 'abr': abr,
  237. 'vbr': vbr,
  238. 'fps': fps,
  239. 'width': width,
  240. 'height': height,
  241. }
  242. formats = []
  243. for format_id, format_url in rendition['redirect'].items():
  244. if format_id == 'rtmp':
  245. ff = f.copy()
  246. ff.update({
  247. 'url': format_url,
  248. 'format_id': format_id,
  249. })
  250. formats.append(ff)
  251. elif determine_ext(format_url) == 'f4m':
  252. formats.extend(self._extract_f4m_formats(
  253. format_url, video_id, f4m_id=format_id))
  254. elif determine_ext(format_url) == 'm3u8':
  255. formats.extend(self._extract_m3u8_formats(
  256. format_url, video_id, 'mp4', m3u8_id=format_id))
  257. else:
  258. continue
  259. self._sort_formats(formats)
  260. title = remove_end(self._og_search_title(webpage), ' - iptv.ORF.at')
  261. description = self._og_search_description(webpage)
  262. upload_date = unified_strdate(self._html_search_meta(
  263. 'dc.date', webpage, 'upload date'))
  264. return {
  265. 'id': video_id,
  266. 'title': title,
  267. 'description': description,
  268. 'duration': duration,
  269. 'thumbnail': thumbnail,
  270. 'upload_date': upload_date,
  271. 'formats': formats,
  272. }