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.

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