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