You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
5.2 KiB

  1. import json
  2. import os
  3. import re
  4. import sys
  5. from .common import InfoExtractor
  6. from ..utils import (
  7. compat_urllib_parse_urlparse,
  8. compat_urllib_request,
  9. ExtractorError,
  10. unescapeHTML,
  11. unified_strdate,
  12. )
  13. from ..aes import (
  14. aes_decrypt_text
  15. )
  16. class YouPornIE(InfoExtractor):
  17. _VALID_URL = r'^(?:https?://)?(?:\w+\.)?youporn\.com/watch/(?P<videoid>[0-9]+)/(?P<title>[^/]+)'
  18. _TEST = {
  19. u'url': u'http://www.youporn.com/watch/505835/sex-ed-is-it-safe-to-masturbate-daily/',
  20. u'file': u'505835.mp4',
  21. u'md5': u'71ec5fcfddacf80f495efa8b6a8d9a89',
  22. u'info_dict': {
  23. u"upload_date": u"20101221",
  24. u"description": u"Love & Sex Answers: http://bit.ly/DanAndJenn -- Is It Unhealthy To Masturbate Daily?",
  25. u"uploader": u"Ask Dan And Jennifer",
  26. u"title": u"Sex Ed: Is It Safe To Masturbate Daily?",
  27. u"age_limit": 18,
  28. }
  29. }
  30. def _print_formats(self, formats):
  31. """Print all available formats"""
  32. print(u'Available formats:')
  33. print(u'ext\t\tformat')
  34. print(u'---------------------------------')
  35. for format in formats:
  36. print(u'%s\t\t%s' % (format['ext'], format['format']))
  37. def _specific(self, req_format, formats):
  38. for x in formats:
  39. if x["format"] == req_format:
  40. return x
  41. return None
  42. def _real_extract(self, url):
  43. mobj = re.match(self._VALID_URL, url)
  44. video_id = mobj.group('videoid')
  45. req = compat_urllib_request.Request(url)
  46. req.add_header('Cookie', 'age_verified=1')
  47. webpage = self._download_webpage(req, video_id)
  48. age_limit = self._rta_search(webpage)
  49. # Get JSON parameters
  50. json_params = self._search_regex(r'var currentVideo = new Video\((.*)\);', webpage, u'JSON parameters')
  51. try:
  52. params = json.loads(json_params)
  53. except:
  54. raise ExtractorError(u'Invalid JSON')
  55. self.report_extraction(video_id)
  56. try:
  57. video_title = params['title']
  58. upload_date = unified_strdate(params['release_date_f'])
  59. video_description = params['description']
  60. video_uploader = params['submitted_by']
  61. thumbnail = params['thumbnails'][0]['image']
  62. except KeyError:
  63. raise ExtractorError('Missing JSON parameter: ' + sys.exc_info()[1])
  64. # Get all of the formats available
  65. DOWNLOAD_LIST_RE = r'(?s)<ul class="downloadList">(?P<download_list>.*?)</ul>'
  66. download_list_html = self._search_regex(DOWNLOAD_LIST_RE,
  67. webpage, u'download list').strip()
  68. # Get all of the links from the page
  69. LINK_RE = r'(?s)<a href="(?P<url>[^"]+)">'
  70. links = re.findall(LINK_RE, download_list_html)
  71. # Get link of hd video if available
  72. mobj = re.search(r'var encryptedQuality720URL = \'(?P<encrypted_video_url>[a-zA-Z0-9+/]+={0,2})\';', webpage)
  73. if mobj != None:
  74. encrypted_video_url = mobj.group(u'encrypted_video_url')
  75. video_url = aes_decrypt_text(encrypted_video_url, video_title, 32).decode('utf-8')
  76. links = [video_url] + links
  77. if not links:
  78. raise ExtractorError(u'ERROR: no known formats available for video')
  79. self.to_screen(u'Links found: %d' % len(links))
  80. formats = []
  81. for link in links:
  82. # A link looks like this:
  83. # http://cdn1.download.youporn.phncdn.com/201210/31/8004515/480p_370k_8004515/YouPorn%20-%20Nubile%20Films%20The%20Pillow%20Fight.mp4?nvb=20121113051249&nva=20121114051249&ir=1200&sr=1200&hash=014b882080310e95fb6a0
  84. # A path looks like this:
  85. # /201210/31/8004515/480p_370k_8004515/YouPorn%20-%20Nubile%20Films%20The%20Pillow%20Fight.mp4
  86. video_url = unescapeHTML( link )
  87. path = compat_urllib_parse_urlparse( video_url ).path
  88. extension = os.path.splitext( path )[1][1:]
  89. format = path.split('/')[4].split('_')[:2]
  90. # size = format[0]
  91. # bitrate = format[1]
  92. format = "-".join( format )
  93. # title = u'%s-%s-%s' % (video_title, size, bitrate)
  94. formats.append({
  95. 'id': video_id,
  96. 'url': video_url,
  97. 'uploader': video_uploader,
  98. 'upload_date': upload_date,
  99. 'title': video_title,
  100. 'ext': extension,
  101. 'format': format,
  102. 'thumbnail': thumbnail,
  103. 'description': video_description,
  104. 'age_limit': age_limit,
  105. })
  106. if self._downloader.params.get('listformats', None):
  107. self._print_formats(formats)
  108. return
  109. req_format = self._downloader.params.get('format', 'best')
  110. self.to_screen(u'Format: %s' % req_format)
  111. if req_format is None or req_format == 'best':
  112. return [formats[0]]
  113. elif req_format == 'worst':
  114. return [formats[-1]]
  115. elif req_format in ('-1', 'all'):
  116. return formats
  117. else:
  118. format = self._specific( req_format, formats )
  119. if format is None:
  120. raise ExtractorError(u'Requested format not available')
  121. return [format]