xtube.py 7.7 KB

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