fourtube.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. parse_duration,
  6. parse_iso8601,
  7. sanitized_Request,
  8. str_to_int,
  9. )
  10. class FourTubeIE(InfoExtractor):
  11. IE_NAME = '4tube'
  12. _VALID_URL = r'https?://(?:www\.)?4tube\.com/videos/(?P<id>\d+)'
  13. _TEST = {
  14. 'url': 'http://www.4tube.com/videos/209733/hot-babe-holly-michaels-gets-her-ass-stuffed-by-black',
  15. 'md5': '6516c8ac63b03de06bc8eac14362db4f',
  16. 'info_dict': {
  17. 'id': '209733',
  18. 'ext': 'mp4',
  19. 'title': 'Hot Babe Holly Michaels gets her ass stuffed by black',
  20. 'uploader': 'WCP Club',
  21. 'uploader_id': 'wcp-club',
  22. 'upload_date': '20131031',
  23. 'timestamp': 1383263892,
  24. 'duration': 583,
  25. 'view_count': int,
  26. 'like_count': int,
  27. 'categories': list,
  28. 'age_limit': 18,
  29. }
  30. }
  31. def _real_extract(self, url):
  32. video_id = self._match_id(url)
  33. webpage = self._download_webpage(url, video_id)
  34. title = self._html_search_meta('name', webpage)
  35. timestamp = parse_iso8601(self._html_search_meta(
  36. 'uploadDate', webpage))
  37. thumbnail = self._html_search_meta('thumbnailUrl', webpage)
  38. uploader_id = self._html_search_regex(
  39. r'<a class="img-avatar" href="[^"]+/channels/([^/"]+)" title="Go to [^"]+ page">',
  40. webpage, 'uploader id', fatal=False)
  41. uploader = self._html_search_regex(
  42. r'<a class="img-avatar" href="[^"]+/channels/[^/"]+" title="Go to ([^"]+) page">',
  43. webpage, 'uploader', fatal=False)
  44. categories_html = self._search_regex(
  45. r'(?s)><i class="icon icon-tag"></i>\s*Categories / Tags\s*.*?<ul class="list">(.*?)</ul>',
  46. webpage, 'categories', fatal=False)
  47. categories = None
  48. if categories_html:
  49. categories = [
  50. c.strip() for c in re.findall(
  51. r'(?s)<li><a.*?>(.*?)</a>', categories_html)]
  52. view_count = str_to_int(self._search_regex(
  53. r'<meta itemprop="interactionCount" content="UserPlays:([0-9,]+)">',
  54. webpage, 'view count', fatal=False))
  55. like_count = str_to_int(self._search_regex(
  56. r'<meta itemprop="interactionCount" content="UserLikes:([0-9,]+)">',
  57. webpage, 'like count', fatal=False))
  58. duration = parse_duration(self._html_search_meta('duration', webpage))
  59. media_id = self._search_regex(
  60. r'<button[^>]+data-id=(["\'])(?P<id>\d+)\1[^>]+data-quality=', webpage,
  61. 'media id', default=None, group='id')
  62. sources = [
  63. quality
  64. for _, quality in re.findall(r'<button[^>]+data-quality=(["\'])(.+?)\1', webpage)]
  65. if not (media_id and sources):
  66. player_js = self._download_webpage(
  67. self._search_regex(
  68. r'<script[^>]id=(["\'])playerembed\1[^>]+src=(["\'])(?P<url>.+?)\2',
  69. webpage, 'player JS', group='url'),
  70. video_id, 'Downloading player JS')
  71. params_js = self._search_regex(
  72. r'\$\.ajax\(url,\ opts\);\s*\}\s*\}\)\(([0-9,\[\] ]+)\)',
  73. player_js, 'initialization parameters')
  74. params = self._parse_json('[%s]' % params_js, video_id)
  75. media_id = params[0]
  76. sources = ['%s' % p for p in params[2]]
  77. token_url = 'http://tkn.4tube.com/{0}/desktop/{1}'.format(
  78. media_id, '+'.join(sources))
  79. headers = {
  80. b'Content-Type': b'application/x-www-form-urlencoded',
  81. b'Origin': b'http://www.4tube.com',
  82. }
  83. token_req = sanitized_Request(token_url, b'{}', headers)
  84. tokens = self._download_json(token_req, video_id)
  85. formats = [{
  86. 'url': tokens[format]['token'],
  87. 'format_id': format + 'p',
  88. 'resolution': format + 'p',
  89. 'quality': int(format),
  90. } for format in sources]
  91. self._sort_formats(formats)
  92. return {
  93. 'id': video_id,
  94. 'title': title,
  95. 'formats': formats,
  96. 'categories': categories,
  97. 'thumbnail': thumbnail,
  98. 'uploader': uploader,
  99. 'uploader_id': uploader_id,
  100. 'timestamp': timestamp,
  101. 'like_count': like_count,
  102. 'view_count': view_count,
  103. 'duration': duration,
  104. 'age_limit': 18,
  105. }