orf.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import json
  4. import re
  5. import calendar
  6. import datetime
  7. from .common import InfoExtractor
  8. from ..utils import (
  9. HEADRequest,
  10. unified_strdate,
  11. ExtractorError,
  12. strip_jsonp,
  13. int_or_none,
  14. float_or_none,
  15. determine_ext,
  16. remove_end,
  17. )
  18. class ORFTVthekIE(InfoExtractor):
  19. IE_NAME = 'orf:tvthek'
  20. IE_DESC = 'ORF TVthek'
  21. _VALID_URL = r'https?://tvthek\.orf\.at/(?:programs/.+?/episodes|topics?/.+?|program/[^/]+)/(?P<id>\d+)'
  22. _TESTS = [{
  23. 'url': 'http://tvthek.orf.at/program/Aufgetischt/2745173/Aufgetischt-Mit-der-Steirischen-Tafelrunde/8891389',
  24. 'playlist': [{
  25. 'md5': '2942210346ed779588f428a92db88712',
  26. 'info_dict': {
  27. 'id': '8896777',
  28. 'ext': 'mp4',
  29. 'title': 'Aufgetischt: Mit der Steirischen Tafelrunde',
  30. 'description': 'md5:c1272f0245537812d4e36419c207b67d',
  31. 'duration': 2668,
  32. 'upload_date': '20141208',
  33. },
  34. }],
  35. 'skip': 'Blocked outside of Austria / Germany',
  36. }, {
  37. 'url': 'http://tvthek.orf.at/topic/Im-Wandel-der-Zeit/8002126/Best-of-Ingrid-Thurnher/7982256',
  38. 'md5': '68f543909aea49d621dfc7703a11cfaf',
  39. 'info_dict': {
  40. 'id': '7982259',
  41. 'ext': 'mp4',
  42. 'title': 'Best of Ingrid Thurnher',
  43. 'upload_date': '20140527',
  44. '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".',
  45. },
  46. 'params': {
  47. 'skip_download': True, # rtsp downloads
  48. },
  49. '_skip': 'Blocked outside of Austria / Germany',
  50. }]
  51. def _real_extract(self, url):
  52. playlist_id = self._match_id(url)
  53. webpage = self._download_webpage(url, playlist_id)
  54. data_json = self._search_regex(
  55. r'initializeAdworx\((.+?)\);\n', webpage, 'video info')
  56. all_data = json.loads(data_json)
  57. def get_segments(all_data):
  58. for data in all_data:
  59. if data['name'] in (
  60. 'Tracker::EPISODE_DETAIL_PAGE_OVER_PROGRAM',
  61. 'Tracker::EPISODE_DETAIL_PAGE_OVER_TOPIC'):
  62. return data['values']['segments']
  63. sdata = get_segments(all_data)
  64. if not sdata:
  65. raise ExtractorError('Unable to extract segments')
  66. def quality_to_int(s):
  67. m = re.search('([0-9]+)', s)
  68. if m is None:
  69. return -1
  70. return int(m.group(1))
  71. entries = []
  72. for sd in sdata:
  73. video_id = sd['id']
  74. formats = [{
  75. 'preference': -10 if fd['delivery'] == 'hls' else None,
  76. 'format_id': '%s-%s-%s' % (
  77. fd['delivery'], fd['quality'], fd['quality_string']),
  78. 'url': fd['src'],
  79. 'protocol': fd['protocol'],
  80. 'quality': quality_to_int(fd['quality']),
  81. } for fd in sd['playlist_item_array']['sources']]
  82. # Check for geoblocking.
  83. # There is a property is_geoprotection, but that's always false
  84. geo_str = sd.get('geoprotection_string')
  85. if geo_str:
  86. try:
  87. http_url = next(
  88. f['url']
  89. for f in formats
  90. if re.match(r'^https?://.*\.mp4$', f['url']))
  91. except StopIteration:
  92. pass
  93. else:
  94. req = HEADRequest(http_url)
  95. self._request_webpage(
  96. req, video_id,
  97. note='Testing for geoblocking',
  98. errnote=((
  99. 'This video seems to be blocked outside of %s. '
  100. 'You may want to try the streaming-* formats.')
  101. % geo_str),
  102. fatal=False)
  103. self._check_formats(formats, video_id)
  104. self._sort_formats(formats)
  105. upload_date = unified_strdate(sd['created_date'])
  106. entries.append({
  107. '_type': 'video',
  108. 'id': video_id,
  109. 'title': sd['header'],
  110. 'formats': formats,
  111. 'description': sd.get('description'),
  112. 'duration': int(sd['duration_in_seconds']),
  113. 'upload_date': upload_date,
  114. 'thumbnail': sd.get('image_full_url'),
  115. })
  116. return {
  117. '_type': 'playlist',
  118. 'entries': entries,
  119. 'id': playlist_id,
  120. }
  121. class ORFOE1IE(InfoExtractor):
  122. IE_NAME = 'orf:oe1'
  123. IE_DESC = 'Radio Österreich 1'
  124. _VALID_URL = r'https?://oe1\.orf\.at/(?:programm/|konsole.*?#\?track_id=)(?P<id>[0-9]+)'
  125. # Audios on ORF radio are only available for 7 days, so we can't add tests.
  126. _TEST = {
  127. 'url': 'http://oe1.orf.at/konsole?show=on_demand#?track_id=394211',
  128. 'only_matching': True,
  129. }
  130. def _real_extract(self, url):
  131. show_id = self._match_id(url)
  132. data = self._download_json(
  133. 'http://oe1.orf.at/programm/%s/konsole' % show_id,
  134. show_id
  135. )
  136. timestamp = datetime.datetime.strptime('%s %s' % (
  137. data['item']['day_label'],
  138. data['item']['time']
  139. ), '%d.%m.%Y %H:%M')
  140. unix_timestamp = calendar.timegm(timestamp.utctimetuple())
  141. return {
  142. 'id': show_id,
  143. 'title': data['item']['title'],
  144. 'url': data['item']['url_stream'],
  145. 'ext': 'mp3',
  146. 'description': data['item'].get('info'),
  147. 'timestamp': unix_timestamp
  148. }
  149. class ORFFM4IE(InfoExtractor):
  150. IE_NAME = 'orf:fm4'
  151. IE_DESC = 'radio FM4'
  152. _VALID_URL = r'https?://fm4\.orf\.at/(?:7tage/?#|player/)(?P<date>[0-9]+)/(?P<show>\w+)'
  153. _TEST = {
  154. 'url': 'http://fm4.orf.at/player/20160110/IS/',
  155. 'md5': '01e736e8f1cef7e13246e880a59ad298',
  156. 'info_dict': {
  157. 'id': '2016-01-10_2100_tl_54_7DaysSun13_11244',
  158. 'ext': 'mp3',
  159. 'title': 'Im Sumpf',
  160. 'description': 'md5:384c543f866c4e422a55f66a62d669cd',
  161. 'duration': 7173,
  162. 'timestamp': 1452456073,
  163. 'upload_date': '20160110',
  164. },
  165. 'skip': 'Live streams on FM4 got deleted soon',
  166. }
  167. def _real_extract(self, url):
  168. mobj = re.match(self._VALID_URL, url)
  169. show_date = mobj.group('date')
  170. show_id = mobj.group('show')
  171. data = self._download_json(
  172. 'http://audioapi.orf.at/fm4/json/2.0/broadcasts/%s/4%s' % (show_date, show_id),
  173. show_id
  174. )
  175. def extract_entry_dict(info, title, subtitle):
  176. return {
  177. 'id': info['loopStreamId'].replace('.mp3', ''),
  178. 'url': 'http://loopstream01.apa.at/?channel=fm4&id=%s' % info['loopStreamId'],
  179. 'title': title,
  180. 'description': subtitle,
  181. 'duration': (info['end'] - info['start']) / 1000,
  182. 'timestamp': info['start'] / 1000,
  183. 'ext': 'mp3'
  184. }
  185. entries = [extract_entry_dict(t, data['title'], data['subtitle']) for t in data['streams']]
  186. return {
  187. '_type': 'playlist',
  188. 'id': show_id,
  189. 'title': data['title'],
  190. 'description': data['subtitle'],
  191. 'entries': entries
  192. }
  193. class ORFIPTVIE(InfoExtractor):
  194. IE_NAME = 'orf:iptv'
  195. IE_DESC = 'iptv.ORF.at'
  196. _VALID_URL = r'https?://iptv\.orf\.at/(?:#/)?stories/(?P<id>\d+)'
  197. _TEST = {
  198. 'url': 'http://iptv.orf.at/stories/2275236/',
  199. 'md5': 'c8b22af4718a4b4af58342529453e3e5',
  200. 'info_dict': {
  201. 'id': '350612',
  202. 'ext': 'flv',
  203. 'title': 'Weitere Evakuierungen um Vulkan Calbuco',
  204. 'description': 'md5:d689c959bdbcf04efeddedbf2299d633',
  205. 'duration': 68.197,
  206. 'thumbnail': 're:^https?://.*\.jpg$',
  207. 'upload_date': '20150425',
  208. },
  209. }
  210. def _real_extract(self, url):
  211. story_id = self._match_id(url)
  212. webpage = self._download_webpage(
  213. 'http://iptv.orf.at/stories/%s' % story_id, story_id)
  214. video_id = self._search_regex(
  215. r'data-video(?:id)?="(\d+)"', webpage, 'video id')
  216. data = self._download_json(
  217. 'http://bits.orf.at/filehandler/static-api/json/current/data.json?file=%s' % video_id,
  218. video_id)[0]
  219. duration = float_or_none(data['duration'], 1000)
  220. video = data['sources']['default']
  221. load_balancer_url = video['loadBalancerUrl']
  222. abr = int_or_none(video.get('audioBitrate'))
  223. vbr = int_or_none(video.get('bitrate'))
  224. fps = int_or_none(video.get('videoFps'))
  225. width = int_or_none(video.get('videoWidth'))
  226. height = int_or_none(video.get('videoHeight'))
  227. thumbnail = video.get('preview')
  228. rendition = self._download_json(
  229. load_balancer_url, video_id, transform_source=strip_jsonp)
  230. f = {
  231. 'abr': abr,
  232. 'vbr': vbr,
  233. 'fps': fps,
  234. 'width': width,
  235. 'height': height,
  236. }
  237. formats = []
  238. for format_id, format_url in rendition['redirect'].items():
  239. if format_id == 'rtmp':
  240. ff = f.copy()
  241. ff.update({
  242. 'url': format_url,
  243. 'format_id': format_id,
  244. })
  245. formats.append(ff)
  246. elif determine_ext(format_url) == 'f4m':
  247. formats.extend(self._extract_f4m_formats(
  248. format_url, video_id, f4m_id=format_id))
  249. elif determine_ext(format_url) == 'm3u8':
  250. formats.extend(self._extract_m3u8_formats(
  251. format_url, video_id, 'mp4', m3u8_id=format_id))
  252. else:
  253. continue
  254. self._sort_formats(formats)
  255. title = remove_end(self._og_search_title(webpage), ' - iptv.ORF.at')
  256. description = self._og_search_description(webpage)
  257. upload_date = unified_strdate(self._html_search_meta(
  258. 'dc.date', webpage, 'upload date'))
  259. return {
  260. 'id': video_id,
  261. 'title': title,
  262. 'description': description,
  263. 'duration': duration,
  264. 'thumbnail': thumbnail,
  265. 'upload_date': upload_date,
  266. 'formats': formats,
  267. }