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.

166 lines
5.3 KiB

10 years ago
10 years ago
  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. extract_attributes,
  6. int_or_none,
  7. parse_duration,
  8. parse_filesize,
  9. unified_timestamp,
  10. )
  11. class NewgroundsIE(InfoExtractor):
  12. _VALID_URL = r'https?://(?:www\.)?newgrounds\.com/(?:audio/listen|portal/view)/(?P<id>[0-9]+)'
  13. _TESTS = [{
  14. 'url': 'https://www.newgrounds.com/audio/listen/549479',
  15. 'md5': 'fe6033d297591288fa1c1f780386f07a',
  16. 'info_dict': {
  17. 'id': '549479',
  18. 'ext': 'mp3',
  19. 'title': 'B7 - BusMode',
  20. 'uploader': 'Burn7',
  21. 'timestamp': 1378878540,
  22. 'upload_date': '20130911',
  23. 'duration': 143,
  24. },
  25. }, {
  26. 'url': 'https://www.newgrounds.com/portal/view/673111',
  27. 'md5': '3394735822aab2478c31b1004fe5e5bc',
  28. 'info_dict': {
  29. 'id': '673111',
  30. 'ext': 'mp4',
  31. 'title': 'Dancin',
  32. 'uploader': 'Squirrelman82',
  33. 'timestamp': 1460256780,
  34. 'upload_date': '20160410',
  35. },
  36. }, {
  37. # source format unavailable, additional mp4 formats
  38. 'url': 'http://www.newgrounds.com/portal/view/689400',
  39. 'info_dict': {
  40. 'id': '689400',
  41. 'ext': 'mp4',
  42. 'title': 'ZTV News Episode 8',
  43. 'uploader': 'BennettTheSage',
  44. 'timestamp': 1487965140,
  45. 'upload_date': '20170224',
  46. },
  47. 'params': {
  48. 'skip_download': True,
  49. },
  50. }]
  51. def _real_extract(self, url):
  52. media_id = self._match_id(url)
  53. webpage = self._download_webpage(url, media_id)
  54. title = self._html_search_regex(
  55. r'<title>([^>]+)</title>', webpage, 'title')
  56. media_url = self._parse_json(self._search_regex(
  57. r'"url"\s*:\s*("[^"]+"),', webpage, ''), media_id)
  58. formats = [{
  59. 'url': media_url,
  60. 'format_id': 'source',
  61. 'quality': 1,
  62. }]
  63. max_resolution = int_or_none(self._search_regex(
  64. r'max_resolution["\']\s*:\s*(\d+)', webpage, 'max resolution',
  65. default=None))
  66. if max_resolution:
  67. url_base = media_url.rpartition('.')[0]
  68. for resolution in (360, 720, 1080):
  69. if resolution > max_resolution:
  70. break
  71. formats.append({
  72. 'url': '%s.%dp.mp4' % (url_base, resolution),
  73. 'format_id': '%dp' % resolution,
  74. 'height': resolution,
  75. })
  76. self._check_formats(formats, media_id)
  77. self._sort_formats(formats)
  78. uploader = self._search_regex(
  79. r'(?:Author|Writer)\s*<a[^>]+>([^<]+)', webpage, 'uploader',
  80. fatal=False)
  81. timestamp = unified_timestamp(self._search_regex(
  82. r'<dt>Uploaded</dt>\s*<dd>([^<]+)', webpage, 'timestamp',
  83. default=None))
  84. duration = parse_duration(self._search_regex(
  85. r'<dd>Song\s*</dd><dd>.+?</dd><dd>([^<]+)', webpage, 'duration',
  86. default=None))
  87. filesize_approx = parse_filesize(self._html_search_regex(
  88. r'<dd>Song\s*</dd><dd>(.+?)</dd>', webpage, 'filesize',
  89. default=None))
  90. if len(formats) == 1:
  91. formats[0]['filesize_approx'] = filesize_approx
  92. if '<dd>Song' in webpage:
  93. formats[0]['vcodec'] = 'none'
  94. return {
  95. 'id': media_id,
  96. 'title': title,
  97. 'uploader': uploader,
  98. 'timestamp': timestamp,
  99. 'duration': duration,
  100. 'formats': formats,
  101. }
  102. class NewgroundsPlaylistIE(InfoExtractor):
  103. _VALID_URL = r'https?://(?:www\.)?newgrounds\.com/(?:collection|[^/]+/search/[^/]+)/(?P<id>[^/?#&]+)'
  104. _TESTS = [{
  105. 'url': 'https://www.newgrounds.com/collection/cats',
  106. 'info_dict': {
  107. 'id': 'cats',
  108. 'title': 'Cats',
  109. },
  110. 'playlist_mincount': 46,
  111. }, {
  112. 'url': 'http://www.newgrounds.com/portal/search/author/ZONE-SAMA',
  113. 'info_dict': {
  114. 'id': 'ZONE-SAMA',
  115. 'title': 'Portal Search: ZONE-SAMA',
  116. },
  117. 'playlist_mincount': 47,
  118. }, {
  119. 'url': 'http://www.newgrounds.com/audio/search/title/cats',
  120. 'only_matching': True,
  121. }]
  122. def _real_extract(self, url):
  123. playlist_id = self._match_id(url)
  124. webpage = self._download_webpage(url, playlist_id)
  125. title = self._search_regex(
  126. r'<title>([^>]+)</title>', webpage, 'title', default=None)
  127. # cut left menu
  128. webpage = self._search_regex(
  129. r'(?s)<div[^>]+\bclass=["\']column wide(.+)',
  130. webpage, 'wide column', default=webpage)
  131. entries = []
  132. for a, path, media_id in re.findall(
  133. r'(<a[^>]+\bhref=["\']/?((?:portal/view|audio/listen)/(\d+))[^>]+>)',
  134. webpage):
  135. a_class = extract_attributes(a).get('class')
  136. if a_class not in ('item-portalsubmission', 'item-audiosubmission'):
  137. continue
  138. entries.append(
  139. self.url_result(
  140. 'https://www.newgrounds.com/%s' % path,
  141. ie=NewgroundsIE.ie_key(), video_id=media_id))
  142. return self.playlist_result(entries, playlist_id, title)