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