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.

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