You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

312 lines
11 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  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. '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. def _real_extract(self, url):
  51. playlist_id = self._match_id(url)
  52. webpage = self._download_webpage(url, playlist_id)
  53. data_json = self._search_regex(
  54. r'initializeAdworx\((.+?)\);\n', webpage, 'video info')
  55. all_data = json.loads(data_json)
  56. def get_segments(all_data):
  57. for data in all_data:
  58. if data['name'] in (
  59. 'Tracker::EPISODE_DETAIL_PAGE_OVER_PROGRAM',
  60. 'Tracker::EPISODE_DETAIL_PAGE_OVER_TOPIC'):
  61. return data['values']['segments']
  62. sdata = get_segments(all_data)
  63. if not sdata:
  64. raise ExtractorError('Unable to extract segments')
  65. def quality_to_int(s):
  66. m = re.search('([0-9]+)', s)
  67. if m is None:
  68. return -1
  69. return int(m.group(1))
  70. entries = []
  71. for sd in sdata:
  72. video_id = sd['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['playlist_item_array']['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. upload_date = unified_strdate(sd['created_date'])
  105. entries.append({
  106. '_type': 'video',
  107. 'id': video_id,
  108. 'title': sd['header'],
  109. 'formats': formats,
  110. 'description': sd.get('description'),
  111. 'duration': int(sd['duration_in_seconds']),
  112. 'upload_date': upload_date,
  113. 'thumbnail': sd.get('image_full_url'),
  114. })
  115. return {
  116. '_type': 'playlist',
  117. 'entries': entries,
  118. 'id': playlist_id,
  119. }
  120. class ORFOE1IE(InfoExtractor):
  121. IE_NAME = 'orf:oe1'
  122. IE_DESC = 'Radio Österreich 1'
  123. _VALID_URL = r'https?://oe1\.orf\.at/(?:programm/|konsole\?.*?\btrack_id=)(?P<id>[0-9]+)'
  124. # Audios on ORF radio are only available for 7 days, so we can't add tests.
  125. _TESTS = [{
  126. 'url': 'http://oe1.orf.at/konsole?show=on_demand#?track_id=394211',
  127. 'only_matching': True,
  128. }, {
  129. 'url': 'http://oe1.orf.at/konsole?show=ondemand&track_id=443608&load_day=/programm/konsole/tag/20160726',
  130. 'only_matching': True,
  131. }]
  132. def _real_extract(self, url):
  133. show_id = self._match_id(url)
  134. data = self._download_json(
  135. 'http://oe1.orf.at/programm/%s/konsole' % show_id,
  136. show_id
  137. )
  138. timestamp = datetime.datetime.strptime('%s %s' % (
  139. data['item']['day_label'],
  140. data['item']['time']
  141. ), '%d.%m.%Y %H:%M')
  142. unix_timestamp = calendar.timegm(timestamp.utctimetuple())
  143. return {
  144. 'id': show_id,
  145. 'title': data['item']['title'],
  146. 'url': data['item']['url_stream'],
  147. 'ext': 'mp3',
  148. 'description': data['item'].get('info'),
  149. 'timestamp': unix_timestamp
  150. }
  151. class ORFFM4IE(InfoExtractor):
  152. IE_NAME = 'orf:fm4'
  153. IE_DESC = 'radio FM4'
  154. _VALID_URL = r'https?://fm4\.orf\.at/(?:7tage/?#|player/)(?P<date>[0-9]+)/(?P<show>\w+)'
  155. _TEST = {
  156. 'url': 'http://fm4.orf.at/player/20160110/IS/',
  157. 'md5': '01e736e8f1cef7e13246e880a59ad298',
  158. 'info_dict': {
  159. 'id': '2016-01-10_2100_tl_54_7DaysSun13_11244',
  160. 'ext': 'mp3',
  161. 'title': 'Im Sumpf',
  162. 'description': 'md5:384c543f866c4e422a55f66a62d669cd',
  163. 'duration': 7173,
  164. 'timestamp': 1452456073,
  165. 'upload_date': '20160110',
  166. },
  167. 'skip': 'Live streams on FM4 got deleted soon',
  168. }
  169. def _real_extract(self, url):
  170. mobj = re.match(self._VALID_URL, url)
  171. show_date = mobj.group('date')
  172. show_id = mobj.group('show')
  173. data = self._download_json(
  174. 'http://audioapi.orf.at/fm4/json/2.0/broadcasts/%s/4%s' % (show_date, show_id),
  175. show_id
  176. )
  177. def extract_entry_dict(info, title, subtitle):
  178. return {
  179. 'id': info['loopStreamId'].replace('.mp3', ''),
  180. 'url': 'http://loopstream01.apa.at/?channel=fm4&id=%s' % info['loopStreamId'],
  181. 'title': title,
  182. 'description': subtitle,
  183. 'duration': (info['end'] - info['start']) / 1000,
  184. 'timestamp': info['start'] / 1000,
  185. 'ext': 'mp3'
  186. }
  187. entries = [extract_entry_dict(t, data['title'], data['subtitle']) for t in data['streams']]
  188. return {
  189. '_type': 'playlist',
  190. 'id': show_id,
  191. 'title': data['title'],
  192. 'description': data['subtitle'],
  193. 'entries': entries
  194. }
  195. class ORFIPTVIE(InfoExtractor):
  196. IE_NAME = 'orf:iptv'
  197. IE_DESC = 'iptv.ORF.at'
  198. _VALID_URL = r'https?://iptv\.orf\.at/(?:#/)?stories/(?P<id>\d+)'
  199. _TEST = {
  200. 'url': 'http://iptv.orf.at/stories/2275236/',
  201. 'md5': 'c8b22af4718a4b4af58342529453e3e5',
  202. 'info_dict': {
  203. 'id': '350612',
  204. 'ext': 'flv',
  205. 'title': 'Weitere Evakuierungen um Vulkan Calbuco',
  206. 'description': 'md5:d689c959bdbcf04efeddedbf2299d633',
  207. 'duration': 68.197,
  208. 'thumbnail': 're:^https?://.*\.jpg$',
  209. 'upload_date': '20150425',
  210. },
  211. }
  212. def _real_extract(self, url):
  213. story_id = self._match_id(url)
  214. webpage = self._download_webpage(
  215. 'http://iptv.orf.at/stories/%s' % story_id, story_id)
  216. video_id = self._search_regex(
  217. r'data-video(?:id)?="(\d+)"', webpage, 'video id')
  218. data = self._download_json(
  219. 'http://bits.orf.at/filehandler/static-api/json/current/data.json?file=%s' % video_id,
  220. video_id)[0]
  221. duration = float_or_none(data['duration'], 1000)
  222. video = data['sources']['default']
  223. load_balancer_url = video['loadBalancerUrl']
  224. abr = int_or_none(video.get('audioBitrate'))
  225. vbr = int_or_none(video.get('bitrate'))
  226. fps = int_or_none(video.get('videoFps'))
  227. width = int_or_none(video.get('videoWidth'))
  228. height = int_or_none(video.get('videoHeight'))
  229. thumbnail = video.get('preview')
  230. rendition = self._download_json(
  231. load_balancer_url, video_id, transform_source=strip_jsonp)
  232. f = {
  233. 'abr': abr,
  234. 'vbr': vbr,
  235. 'fps': fps,
  236. 'width': width,
  237. 'height': height,
  238. }
  239. formats = []
  240. for format_id, format_url in rendition['redirect'].items():
  241. if format_id == 'rtmp':
  242. ff = f.copy()
  243. ff.update({
  244. 'url': format_url,
  245. 'format_id': format_id,
  246. })
  247. formats.append(ff)
  248. elif determine_ext(format_url) == 'f4m':
  249. formats.extend(self._extract_f4m_formats(
  250. format_url, video_id, f4m_id=format_id))
  251. elif determine_ext(format_url) == 'm3u8':
  252. formats.extend(self._extract_m3u8_formats(
  253. format_url, video_id, 'mp4', m3u8_id=format_id))
  254. else:
  255. continue
  256. self._sort_formats(formats)
  257. title = remove_end(self._og_search_title(webpage), ' - iptv.ORF.at')
  258. description = self._og_search_description(webpage)
  259. upload_date = unified_strdate(self._html_search_meta(
  260. 'dc.date', webpage, 'upload date'))
  261. return {
  262. 'id': video_id,
  263. 'title': title,
  264. 'description': description,
  265. 'duration': duration,
  266. 'thumbnail': thumbnail,
  267. 'upload_date': upload_date,
  268. 'formats': formats,
  269. }