youporn.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. }
  28. }
  29. def _print_formats(self, formats):
  30. """Print all available formats"""
  31. print(u'Available formats:')
  32. print(u'ext\t\tformat')
  33. print(u'---------------------------------')
  34. for format in formats:
  35. print(u'%s\t\t%s' % (format['ext'], format['format']))
  36. def _specific(self, req_format, formats):
  37. for x in formats:
  38. if x["format"] == req_format:
  39. return x
  40. return None
  41. def _real_extract(self, url):
  42. mobj = re.match(self._VALID_URL, url)
  43. video_id = mobj.group('videoid')
  44. req = compat_urllib_request.Request(url)
  45. req.add_header('Cookie', 'age_verified=1')
  46. webpage = self._download_webpage(req, video_id)
  47. # Get JSON parameters
  48. json_params = self._search_regex(r'var currentVideo = new Video\((.*)\);', webpage, u'JSON parameters')
  49. try:
  50. params = json.loads(json_params)
  51. except:
  52. raise ExtractorError(u'Invalid JSON')
  53. self.report_extraction(video_id)
  54. try:
  55. video_title = params['title']
  56. upload_date = unified_strdate(params['release_date_f'])
  57. video_description = params['description']
  58. video_uploader = params['submitted_by']
  59. thumbnail = params['thumbnails'][0]['image']
  60. except KeyError:
  61. raise ExtractorError('Missing JSON parameter: ' + sys.exc_info()[1])
  62. # Get all of the formats available
  63. DOWNLOAD_LIST_RE = r'(?s)<ul class="downloadList">(?P<download_list>.*?)</ul>'
  64. download_list_html = self._search_regex(DOWNLOAD_LIST_RE,
  65. webpage, u'download list').strip()
  66. # Get all of the links from the page
  67. LINK_RE = r'(?s)<a href="(?P<url>[^"]+)">'
  68. links = re.findall(LINK_RE, download_list_html)
  69. # Get link of hd video if available
  70. mobj = re.search(r'var encryptedQuality720URL = \'(?P<encrypted_video_url>[a-zA-Z0-9+/]+={0,2})\';', webpage)
  71. if mobj != None:
  72. encrypted_video_url = mobj.group(u'encrypted_video_url')
  73. video_url = aes_decrypt_text(encrypted_video_url, video_title, 32).decode('utf-8')
  74. links = [video_url] + links
  75. if not links:
  76. raise ExtractorError(u'ERROR: no known formats available for video')
  77. self.to_screen(u'Links found: %d' % len(links))
  78. formats = []
  79. for link in links:
  80. # A link looks like this:
  81. # 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
  82. # A path looks like this:
  83. # /201210/31/8004515/480p_370k_8004515/YouPorn%20-%20Nubile%20Films%20The%20Pillow%20Fight.mp4
  84. video_url = unescapeHTML( link )
  85. path = compat_urllib_parse_urlparse( video_url ).path
  86. extension = os.path.splitext( path )[1][1:]
  87. format = path.split('/')[4].split('_')[:2]
  88. # size = format[0]
  89. # bitrate = format[1]
  90. format = "-".join( format )
  91. # title = u'%s-%s-%s' % (video_title, size, bitrate)
  92. formats.append({
  93. 'id': video_id,
  94. 'url': video_url,
  95. 'uploader': video_uploader,
  96. 'upload_date': upload_date,
  97. 'title': video_title,
  98. 'ext': extension,
  99. 'format': format,
  100. 'thumbnail': thumbnail,
  101. 'description': video_description
  102. })
  103. if self._downloader.params.get('listformats', None):
  104. self._print_formats(formats)
  105. return
  106. req_format = self._downloader.params.get('format', 'best')
  107. self.to_screen(u'Format: %s' % req_format)
  108. if req_format is None or req_format == 'best':
  109. return [formats[0]]
  110. elif req_format == 'worst':
  111. return [formats[-1]]
  112. elif req_format in ('-1', 'all'):
  113. return formats
  114. else:
  115. format = self._specific( req_format, formats )
  116. if format is None:
  117. raise ExtractorError(u'Requested format not available')
  118. return [format]