logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

xtube.py (8579B)


  1. from __future__ import unicode_literals
  2. import itertools
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. int_or_none,
  7. js_to_json,
  8. orderedSet,
  9. parse_duration,
  10. sanitized_Request,
  11. str_to_int,
  12. url_or_none,
  13. )
  14. class XTubeIE(InfoExtractor):
  15. _VALID_URL = r'''(?x)
  16. (?:
  17. xtube:|
  18. https?://(?:www\.)?xtube\.com/(?:watch\.php\?.*\bv=|video-watch/(?:embedded/)?(?P<display_id>[^/]+)-)
  19. )
  20. (?P<id>[^/?&#]+)
  21. '''
  22. _TESTS = [{
  23. # old URL schema
  24. 'url': 'http://www.xtube.com/watch.php?v=kVTUy_G222_',
  25. 'md5': '092fbdd3cbe292c920ef6fc6a8a9cdab',
  26. 'info_dict': {
  27. 'id': 'kVTUy_G222_',
  28. 'ext': 'mp4',
  29. 'title': 'strange erotica',
  30. 'description': 'contains:an ET kind of thing',
  31. 'uploader': 'greenshowers',
  32. 'duration': 450,
  33. 'view_count': int,
  34. 'comment_count': int,
  35. 'age_limit': 18,
  36. }
  37. }, {
  38. # FLV videos with duplicated formats
  39. 'url': 'http://www.xtube.com/video-watch/A-Super-Run-Part-1-YT-9299752',
  40. 'md5': 'a406963eb349dd43692ec54631efd88b',
  41. 'info_dict': {
  42. 'id': '9299752',
  43. 'display_id': 'A-Super-Run-Part-1-YT',
  44. 'ext': 'flv',
  45. 'title': 'A Super Run - Part 1 (YT)',
  46. 'description': 'md5:4cc3af1aa1b0413289babc88f0d4f616',
  47. 'uploader': 'tshirtguy59',
  48. 'duration': 579,
  49. 'view_count': int,
  50. 'comment_count': int,
  51. 'age_limit': 18,
  52. },
  53. }, {
  54. # new URL schema
  55. 'url': 'http://www.xtube.com/video-watch/strange-erotica-625837',
  56. 'only_matching': True,
  57. }, {
  58. 'url': 'xtube:625837',
  59. 'only_matching': True,
  60. }, {
  61. 'url': 'xtube:kVTUy_G222_',
  62. 'only_matching': True,
  63. }, {
  64. 'url': 'https://www.xtube.com/video-watch/embedded/milf-tara-and-teen-shared-and-cum-covered-extreme-bukkake-32203482?embedsize=big',
  65. 'only_matching': True,
  66. }]
  67. def _real_extract(self, url):
  68. mobj = re.match(self._VALID_URL, url)
  69. video_id = mobj.group('id')
  70. display_id = mobj.group('display_id')
  71. if not display_id:
  72. display_id = video_id
  73. if video_id.isdigit() and len(video_id) < 11:
  74. url_pattern = 'http://www.xtube.com/video-watch/-%s'
  75. else:
  76. url_pattern = 'http://www.xtube.com/watch.php?v=%s'
  77. webpage = self._download_webpage(
  78. url_pattern % video_id, display_id, headers={
  79. 'Cookie': 'age_verified=1; cookiesAccepted=1',
  80. })
  81. title, thumbnail, duration, sources, media_definition = [None] * 5
  82. config = self._parse_json(self._search_regex(
  83. r'playerConf\s*=\s*({.+?})\s*,\s*(?:\n|loaderConf|playerWrapper)', webpage, 'config',
  84. default='{}'), video_id, transform_source=js_to_json, fatal=False)
  85. if config:
  86. config = config.get('mainRoll')
  87. if isinstance(config, dict):
  88. title = config.get('title')
  89. thumbnail = config.get('poster')
  90. duration = int_or_none(config.get('duration'))
  91. sources = config.get('sources') or config.get('format')
  92. media_definition = config.get('mediaDefinition')
  93. if not isinstance(sources, dict) and not media_definition:
  94. sources = self._parse_json(self._search_regex(
  95. r'(["\'])?sources\1?\s*:\s*(?P<sources>{.+?}),',
  96. webpage, 'sources', group='sources'), video_id,
  97. transform_source=js_to_json)
  98. formats = []
  99. format_urls = set()
  100. if isinstance(sources, dict):
  101. for format_id, format_url in sources.items():
  102. format_url = url_or_none(format_url)
  103. if not format_url:
  104. continue
  105. if format_url in format_urls:
  106. continue
  107. format_urls.add(format_url)
  108. formats.append({
  109. 'url': format_url,
  110. 'format_id': format_id,
  111. 'height': int_or_none(format_id),
  112. })
  113. if isinstance(media_definition, list):
  114. for media in media_definition:
  115. video_url = url_or_none(media.get('videoUrl'))
  116. if not video_url:
  117. continue
  118. if video_url in format_urls:
  119. continue
  120. format_urls.add(video_url)
  121. format_id = media.get('format')
  122. if format_id == 'hls':
  123. formats.extend(self._extract_m3u8_formats(
  124. video_url, video_id, 'mp4', entry_protocol='m3u8_native',
  125. m3u8_id='hls', fatal=False))
  126. elif format_id == 'mp4':
  127. height = int_or_none(media.get('quality'))
  128. formats.append({
  129. 'url': video_url,
  130. 'format_id': '%s-%d' % (format_id, height) if height else format_id,
  131. 'height': height,
  132. })
  133. self._remove_duplicate_formats(formats)
  134. self._sort_formats(formats)
  135. if not title:
  136. title = self._search_regex(
  137. (r'<h1>\s*(?P<title>[^<]+?)\s*</h1>', r'videoTitle\s*:\s*(["\'])(?P<title>.+?)\1'),
  138. webpage, 'title', group='title')
  139. description = self._og_search_description(
  140. webpage, default=None) or self._html_search_meta(
  141. 'twitter:description', webpage, default=None) or self._search_regex(
  142. r'</h1>\s*<p>([^<]+)', webpage, 'description', fatal=False)
  143. uploader = self._search_regex(
  144. (r'<input[^>]+name="contentOwnerId"[^>]+value="([^"]+)"',
  145. r'<span[^>]+class="nickname"[^>]*>([^<]+)'),
  146. webpage, 'uploader', fatal=False)
  147. if not duration:
  148. duration = parse_duration(self._search_regex(
  149. r'<dt>Runtime:?</dt>\s*<dd>([^<]+)</dd>',
  150. webpage, 'duration', fatal=False))
  151. view_count = str_to_int(self._search_regex(
  152. (r'["\']viewsCount["\'][^>]*>(\d+)\s+views',
  153. r'<dt>Views:?</dt>\s*<dd>([\d,\.]+)</dd>'),
  154. webpage, 'view count', fatal=False))
  155. comment_count = str_to_int(self._html_search_regex(
  156. r'>Comments? \(([\d,\.]+)\)<',
  157. webpage, 'comment count', fatal=False))
  158. return {
  159. 'id': video_id,
  160. 'display_id': display_id,
  161. 'title': title,
  162. 'description': description,
  163. 'thumbnail': thumbnail,
  164. 'uploader': uploader,
  165. 'duration': duration,
  166. 'view_count': view_count,
  167. 'comment_count': comment_count,
  168. 'age_limit': 18,
  169. 'formats': formats,
  170. }
  171. class XTubeUserIE(InfoExtractor):
  172. IE_DESC = 'XTube user profile'
  173. _VALID_URL = r'https?://(?:www\.)?xtube\.com/profile/(?P<id>[^/]+-\d+)'
  174. _TEST = {
  175. 'url': 'http://www.xtube.com/profile/greenshowers-4056496',
  176. 'info_dict': {
  177. 'id': 'greenshowers-4056496',
  178. 'age_limit': 18,
  179. },
  180. 'playlist_mincount': 154,
  181. }
  182. def _real_extract(self, url):
  183. user_id = self._match_id(url)
  184. entries = []
  185. for pagenum in itertools.count(1):
  186. request = sanitized_Request(
  187. 'http://www.xtube.com/profile/%s/videos/%d' % (user_id, pagenum),
  188. headers={
  189. 'Cookie': 'popunder=4',
  190. 'X-Requested-With': 'XMLHttpRequest',
  191. 'Referer': url,
  192. })
  193. page = self._download_json(
  194. request, user_id, 'Downloading videos JSON page %d' % pagenum)
  195. html = page.get('html')
  196. if not html:
  197. break
  198. for video_id in orderedSet([video_id for _, video_id in re.findall(
  199. r'data-plid=(["\'])(.+?)\1', html)]):
  200. entries.append(self.url_result('xtube:%s' % video_id, XTubeIE.ie_key()))
  201. page_count = int_or_none(page.get('pageCount'))
  202. if not page_count or pagenum == page_count:
  203. break
  204. playlist = self.playlist_result(entries, user_id)
  205. playlist['age_limit'] = 18
  206. return playlist