ora.py 3.0 KB

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