ora.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import compat_urlparse
  6. from ..utils import (
  7. get_element_by_attribute,
  8. js_to_json,
  9. qualities,
  10. unescapeHTML,
  11. )
  12. class OraTVIE(InfoExtractor):
  13. _VALID_URL = r'https?://(?:www\.)?(ora\.tv|unsafespeech\.com)/([^/]+/)*(?P<id>[^/\?#]+)'
  14. _TEST = {
  15. 'url': 'https://www.ora.tv/larrykingnow/2015/12/16/vine-youtube-stars-zach-king-king-bach-on-their-viral-videos-0_36jupg6090pq',
  16. 'md5': 'fa33717591c631ec93b04b0e330df786',
  17. 'info_dict': {
  18. 'id': '50178',
  19. 'ext': 'mp4',
  20. 'title': 'Vine & YouTube Stars Zach King & King Bach On Their Viral Videos!',
  21. 'description': 'md5:ebbc5b1424dd5dba7be7538148287ac1',
  22. }
  23. }
  24. def _real_extract(self, url):
  25. display_id = self._match_id(url)
  26. webpage = self._download_webpage(url, display_id)
  27. ora_meta = self._parse_json(self._search_regex(
  28. r'(?s);\s*ora_meta = ({.*?});</script>', webpage, 'ora_meta'), display_id,
  29. transform_source=lambda data: js_to_json(re.sub('":(document|\().*?(:false|\(\)),', '":null,', data)))
  30. video_data = ora_meta.get('video', ora_meta.get('current'))
  31. m3u8_url = video_data['hls_stream']
  32. if m3u8_url:
  33. formats = self._extract_m3u8_formats(
  34. m3u8_url, display_id, 'mp4', 'm3u8_native',
  35. m3u8_id='hls', fatal=False)
  36. # similar to GameSpotIE
  37. m3u8_path = compat_urlparse.urlparse(m3u8_url).path
  38. QUALITIES_RE = r'((,[a-z]+\d+)+,?)'
  39. available_qualities = self._search_regex(
  40. QUALITIES_RE, m3u8_path, 'qualities').strip(',').split(',')
  41. http_path = m3u8_path[1:].split('/', 1)[1]
  42. http_template = re.sub(QUALITIES_RE, r'%s', http_path)
  43. http_template = http_template.replace('.csmil/master.m3u8', '')
  44. http_template = compat_urlparse.urljoin(
  45. 'http://videocdn-pmd.ora.tv/', http_template)
  46. preference = qualities(
  47. ['mobile400', 'basic400', 'basic600', 'sd900', 'sd1200', 'sd1500', 'hd720', 'hd1080'])
  48. for q in available_qualities:
  49. formats.append({
  50. 'url': http_template % q,
  51. 'format_id': q,
  52. 'preference': preference(q),
  53. })
  54. self._sort_formats(formats)
  55. else:
  56. return self.url_result(self._search_regex(
  57. r'"youtube_id"\s*:\s*"([^"]+)', webpage, 'youtube id'), 'Youtube')
  58. return {
  59. 'id': video_data.get('id', display_id),
  60. 'display_id': display_id,
  61. 'title': unescapeHTML(self._og_search_title(webpage)),
  62. 'description': get_element_by_attribute(
  63. 'class', 'video_txt_decription', webpage),
  64. 'thumbnail': self._proto_relative_url(video_data.get('thumb')),
  65. 'formats': formats,
  66. }