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.

308 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. }
  165. def _real_extract(self, url):
  166. mobj = re.match(self._VALID_URL, url)
  167. show_date = mobj.group('date')
  168. show_id = mobj.group('show')
  169. data = self._download_json(
  170. 'http://audioapi.orf.at/fm4/json/2.0/broadcasts/%s/4%s' % (show_date, show_id),
  171. show_id
  172. )
  173. def extract_entry_dict(info, title, subtitle):
  174. return {
  175. 'id': info['loopStreamId'].replace('.mp3', ''),
  176. 'url': 'http://loopstream01.apa.at/?channel=fm4&id=%s' % info['loopStreamId'],
  177. 'title': title,
  178. 'description': subtitle,
  179. 'duration': (info['end'] - info['start']) / 1000,
  180. 'timestamp': info['start'] / 1000,
  181. 'ext': 'mp3'
  182. }
  183. entries = [extract_entry_dict(t, data['title'], data['subtitle']) for t in data['streams']]
  184. return {
  185. '_type': 'playlist',
  186. 'id': show_id,
  187. 'title': data['title'],
  188. 'description': data['subtitle'],
  189. 'entries': entries
  190. }
  191. class ORFIPTVIE(InfoExtractor):
  192. IE_NAME = 'orf:iptv'
  193. IE_DESC = 'iptv.ORF.at'
  194. _VALID_URL = r'https?://iptv\.orf\.at/(?:#/)?stories/(?P<id>\d+)'
  195. _TEST = {
  196. 'url': 'http://iptv.orf.at/stories/2275236/',
  197. 'md5': 'c8b22af4718a4b4af58342529453e3e5',
  198. 'info_dict': {
  199. 'id': '350612',
  200. 'ext': 'flv',
  201. 'title': 'Weitere Evakuierungen um Vulkan Calbuco',
  202. 'description': 'md5:d689c959bdbcf04efeddedbf2299d633',
  203. 'duration': 68.197,
  204. 'thumbnail': 're:^https?://.*\.jpg$',
  205. 'upload_date': '20150425',
  206. },
  207. }
  208. def _real_extract(self, url):
  209. story_id = self._match_id(url)
  210. webpage = self._download_webpage(
  211. 'http://iptv.orf.at/stories/%s' % story_id, story_id)
  212. video_id = self._search_regex(
  213. r'data-video(?:id)?="(\d+)"', webpage, 'video id')
  214. data = self._download_json(
  215. 'http://bits.orf.at/filehandler/static-api/json/current/data.json?file=%s' % video_id,
  216. video_id)[0]
  217. duration = float_or_none(data['duration'], 1000)
  218. video = data['sources']['default']
  219. load_balancer_url = video['loadBalancerUrl']
  220. abr = int_or_none(video.get('audioBitrate'))
  221. vbr = int_or_none(video.get('bitrate'))
  222. fps = int_or_none(video.get('videoFps'))
  223. width = int_or_none(video.get('videoWidth'))
  224. height = int_or_none(video.get('videoHeight'))
  225. thumbnail = video.get('preview')
  226. rendition = self._download_json(
  227. load_balancer_url, video_id, transform_source=strip_jsonp)
  228. f = {
  229. 'abr': abr,
  230. 'vbr': vbr,
  231. 'fps': fps,
  232. 'width': width,
  233. 'height': height,
  234. }
  235. formats = []
  236. for format_id, format_url in rendition['redirect'].items():
  237. if format_id == 'rtmp':
  238. ff = f.copy()
  239. ff.update({
  240. 'url': format_url,
  241. 'format_id': format_id,
  242. })
  243. formats.append(ff)
  244. elif determine_ext(format_url) == 'f4m':
  245. formats.extend(self._extract_f4m_formats(
  246. format_url, video_id, f4m_id=format_id))
  247. elif determine_ext(format_url) == 'm3u8':
  248. formats.extend(self._extract_m3u8_formats(
  249. format_url, video_id, 'mp4', m3u8_id=format_id))
  250. else:
  251. continue
  252. self._sort_formats(formats)
  253. title = remove_end(self._og_search_title(webpage), ' - iptv.ORF.at')
  254. description = self._og_search_description(webpage)
  255. upload_date = unified_strdate(self._html_search_meta(
  256. 'dc.date', webpage, 'upload date'))
  257. return {
  258. 'id': video_id,
  259. 'title': title,
  260. 'description': description,
  261. 'duration': duration,
  262. 'thumbnail': thumbnail,
  263. 'upload_date': upload_date,
  264. 'formats': formats,
  265. }