orf.py 11 KB

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