xtube.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..compat import compat_urllib_parse_unquote
  5. from ..utils import (
  6. parse_duration,
  7. sanitized_Request,
  8. str_to_int,
  9. )
  10. class XTubeIE(InfoExtractor):
  11. _VALID_URL = r'(?:xtube:|https?://(?:www\.)?xtube\.com/watch\.php\?.*\bv=)(?P<id>[^/?&#]+)'
  12. _TEST = {
  13. 'url': 'http://www.xtube.com/watch.php?v=kVTUy_G222_',
  14. 'md5': '092fbdd3cbe292c920ef6fc6a8a9cdab',
  15. 'info_dict': {
  16. 'id': 'kVTUy_G222_',
  17. 'ext': 'mp4',
  18. 'title': 'strange erotica',
  19. 'description': 'contains:an ET kind of thing',
  20. 'uploader': 'greenshowers',
  21. 'duration': 450,
  22. 'age_limit': 18,
  23. }
  24. }
  25. def _real_extract(self, url):
  26. video_id = self._match_id(url)
  27. req = sanitized_Request('http://www.xtube.com/watch.php?v=%s' % video_id)
  28. req.add_header('Cookie', 'age_verified=1')
  29. webpage = self._download_webpage(req, video_id)
  30. video_title = self._html_search_regex(
  31. r'<p class="title">([^<]+)', webpage, 'title')
  32. video_uploader = self._html_search_regex(
  33. [r"var\s+contentOwnerId\s*=\s*'([^']+)",
  34. r'By:\s*<a href="/community/profile\.php\?user=([^"]+)'],
  35. webpage, 'uploader', fatal=False)
  36. video_description = self._html_search_regex(
  37. r'<p class="fieldsDesc">([^<]+)',
  38. webpage, 'description', fatal=False)
  39. duration = parse_duration(self._html_search_regex(
  40. r'<span class="bold">Runtime:</span> ([^<]+)</p>',
  41. webpage, 'duration', fatal=False))
  42. view_count = str_to_int(self._html_search_regex(
  43. r'<span class="bold">Views:</span> ([\d,\.]+)</p>',
  44. webpage, 'view count', fatal=False))
  45. comment_count = str_to_int(self._html_search_regex(
  46. r'<div id="commentBar">([\d,\.]+) Comments</div>',
  47. webpage, 'comment count', fatal=False))
  48. formats = []
  49. for format_id, video_url in re.findall(
  50. r'flashvars\.quality_(.+?)\s*=\s*"([^"]+)"', webpage):
  51. fmt = {
  52. 'url': compat_urllib_parse_unquote(video_url),
  53. 'format_id': format_id,
  54. }
  55. m = re.search(r'^(?P<height>\d+)[pP]', format_id)
  56. if m:
  57. fmt['height'] = int(m.group('height'))
  58. formats.append(fmt)
  59. if not formats:
  60. video_url = compat_urllib_parse_unquote(self._search_regex(
  61. r'flashvars\.video_url\s*=\s*"([^"]+)"',
  62. webpage, 'video URL'))
  63. formats.append({'url': video_url})
  64. self._sort_formats(formats)
  65. return {
  66. 'id': video_id,
  67. 'title': video_title,
  68. 'uploader': video_uploader,
  69. 'description': video_description,
  70. 'duration': duration,
  71. 'view_count': view_count,
  72. 'comment_count': comment_count,
  73. 'formats': formats,
  74. 'age_limit': 18,
  75. }
  76. class XTubeUserIE(InfoExtractor):
  77. IE_DESC = 'XTube user profile'
  78. _VALID_URL = r'https?://(?:www\.)?xtube\.com/community/profile\.php\?(.*?)user=(?P<username>[^&#]+)(?:$|[&#])'
  79. _TEST = {
  80. 'url': 'http://www.xtube.com/community/profile.php?user=greenshowers',
  81. 'info_dict': {
  82. 'id': 'greenshowers',
  83. 'age_limit': 18,
  84. },
  85. 'playlist_mincount': 155,
  86. }
  87. def _real_extract(self, url):
  88. mobj = re.match(self._VALID_URL, url)
  89. username = mobj.group('username')
  90. profile_page = self._download_webpage(
  91. url, username, note='Retrieving profile page')
  92. video_count = int(self._search_regex(
  93. r'<strong>%s\'s Videos \(([0-9]+)\)</strong>' % username, profile_page,
  94. 'video count'))
  95. PAGE_SIZE = 25
  96. urls = []
  97. page_count = (video_count + PAGE_SIZE + 1) // PAGE_SIZE
  98. for n in range(1, page_count + 1):
  99. lpage_url = 'http://www.xtube.com/user_videos.php?page=%d&u=%s' % (n, username)
  100. lpage = self._download_webpage(
  101. lpage_url, username,
  102. note='Downloading page %d/%d' % (n, page_count))
  103. urls.extend(
  104. re.findall(r'addthis:url="([^"]+)"', lpage))
  105. return {
  106. '_type': 'playlist',
  107. 'id': username,
  108. 'age_limit': 18,
  109. 'entries': [{
  110. '_type': 'url',
  111. 'url': eurl,
  112. 'ie_key': 'XTube',
  113. } for eurl in urls]
  114. }