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.

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