newgrounds.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. import functools
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. clean_html,
  6. extract_attributes,
  7. get_element_by_id,
  8. int_or_none,
  9. parse_count,
  10. parse_duration,
  11. unified_timestamp,
  12. OnDemandPagedList,
  13. try_get,
  14. )
  15. class NewgroundsIE(InfoExtractor):
  16. _VALID_URL = r'https?://(?:www\.)?newgrounds\.com/(?:audio/listen|portal/view)/(?P<id>\d+)(?:/format/flash)?'
  17. _TESTS = [{
  18. 'url': 'https://www.newgrounds.com/audio/listen/549479',
  19. 'md5': 'fe6033d297591288fa1c1f780386f07a',
  20. 'info_dict': {
  21. 'id': '549479',
  22. 'ext': 'mp3',
  23. 'title': 'B7 - BusMode',
  24. 'uploader': 'Burn7',
  25. 'timestamp': 1378878540,
  26. 'upload_date': '20130911',
  27. 'duration': 143,
  28. 'view_count': int,
  29. 'description': 'md5:b8b3c2958875189f07d8e313462e8c4f',
  30. },
  31. }, {
  32. 'url': 'https://www.newgrounds.com/portal/view/1',
  33. 'md5': 'fbfb40e2dc765a7e830cb251d370d981',
  34. 'info_dict': {
  35. 'id': '1',
  36. 'ext': 'mp4',
  37. 'title': 'Scrotum 1',
  38. 'uploader': 'Brian-Beaton',
  39. 'timestamp': 955064100,
  40. 'upload_date': '20000406',
  41. 'view_count': int,
  42. 'description': 'Scrotum plays "catch."',
  43. 'age_limit': 17,
  44. },
  45. }, {
  46. # source format unavailable, additional mp4 formats
  47. 'url': 'http://www.newgrounds.com/portal/view/689400',
  48. 'info_dict': {
  49. 'id': '689400',
  50. 'ext': 'mp4',
  51. 'title': 'ZTV News Episode 8',
  52. 'uploader': 'ZONE-SAMA',
  53. 'timestamp': 1487965140,
  54. 'upload_date': '20170224',
  55. 'view_count': int,
  56. 'description': 'md5:aff9b330ec2e78ed93b1ad6d017accc6',
  57. 'age_limit': 17,
  58. },
  59. 'params': {
  60. 'skip_download': True,
  61. },
  62. }, {
  63. 'url': 'https://www.newgrounds.com/portal/view/297383',
  64. 'md5': '2c11f5fd8cb6b433a63c89ba3141436c',
  65. 'info_dict': {
  66. 'id': '297383',
  67. 'ext': 'mp4',
  68. 'title': 'Metal Gear Awesome',
  69. 'uploader': 'Egoraptor',
  70. 'timestamp': 1140663240,
  71. 'upload_date': '20060223',
  72. 'view_count': int,
  73. 'description': 'md5:9246c181614e23754571995104da92e0',
  74. 'age_limit': 13,
  75. }
  76. }, {
  77. 'url': 'https://www.newgrounds.com/portal/view/297383/format/flash',
  78. 'md5': '5d05585a9a0caca059f5abfbd3865524',
  79. 'info_dict': {
  80. 'id': '297383',
  81. 'ext': 'swf',
  82. 'title': 'Metal Gear Awesome',
  83. 'description': 'Metal Gear Awesome',
  84. 'uploader': 'Egoraptor',
  85. 'upload_date': '20060223',
  86. 'timestamp': 1140663240,
  87. 'age_limit': 13,
  88. }
  89. }]
  90. _AGE_LIMIT = {
  91. 'e': 0,
  92. 't': 13,
  93. 'm': 17,
  94. 'a': 18,
  95. }
  96. def _real_extract(self, url):
  97. media_id = self._match_id(url)
  98. formats = []
  99. uploader = None
  100. webpage = self._download_webpage(url, media_id)
  101. title = self._html_extract_title(webpage)
  102. media_url_string = self._search_regex(
  103. r'"url"\s*:\s*("[^"]+"),', webpage, 'media url', default=None)
  104. if media_url_string:
  105. media_url = self._parse_json(media_url_string, media_id)
  106. formats = [{
  107. 'url': media_url,
  108. 'format_id': 'source',
  109. 'quality': 1,
  110. }]
  111. else:
  112. json_video = self._download_json('https://www.newgrounds.com/portal/video/' + media_id, media_id, headers={
  113. 'Accept': 'application/json',
  114. 'Referer': url,
  115. 'X-Requested-With': 'XMLHttpRequest'
  116. })
  117. uploader = json_video.get('author')
  118. media_formats = json_video.get('sources', [])
  119. for media_format in media_formats:
  120. media_sources = media_formats[media_format]
  121. for source in media_sources:
  122. formats.append({
  123. 'format_id': media_format,
  124. 'quality': int_or_none(media_format[:-1]),
  125. 'url': source.get('src')
  126. })
  127. if not uploader:
  128. uploader = self._html_search_regex(
  129. (r'(?s)<h4[^>]*>(.+?)</h4>.*?<em>\s*(?:Author|Artist)\s*</em>',
  130. r'(?:Author|Writer)\s*<a[^>]+>([^<]+)'), webpage, 'uploader',
  131. fatal=False)
  132. age_limit = self._html_search_regex(
  133. r'<h2\s*class=["\']rated-([^"\'])["\'][^>]+>', webpage, 'age_limit', default='e')
  134. age_limit = self._AGE_LIMIT.get(age_limit)
  135. timestamp = unified_timestamp(self._html_search_regex(
  136. (r'<dt>\s*Uploaded\s*</dt>\s*<dd>([^<]+</dd>\s*<dd>[^<]+)',
  137. r'<dt>\s*Uploaded\s*</dt>\s*<dd>([^<]+)'), webpage, 'timestamp',
  138. default=None))
  139. duration = parse_duration(self._html_search_regex(
  140. r'"duration"\s*:\s*["\']?(\d+)["\']?', webpage,
  141. 'duration', default=None))
  142. description = clean_html(get_element_by_id('author_comments', webpage)) or self._og_search_description(webpage)
  143. view_count = parse_count(self._html_search_regex(
  144. r'(?s)<dt>\s*(?:Views|Listens)\s*</dt>\s*<dd>([\d\.,]+)</dd>', webpage,
  145. 'view count', default=None))
  146. filesize = int_or_none(self._html_search_regex(
  147. r'"filesize"\s*:\s*["\']?([\d]+)["\']?,', webpage, 'filesize',
  148. default=None))
  149. video_type_description = self._html_search_regex(
  150. r'"description"\s*:\s*["\']?([^"\']+)["\']?,', webpage, 'filesize',
  151. default=None)
  152. if len(formats) == 1:
  153. formats[0]['filesize'] = filesize
  154. if video_type_description == 'Audio File':
  155. formats[0]['vcodec'] = 'none'
  156. self._check_formats(formats, media_id)
  157. return {
  158. 'id': media_id,
  159. 'title': title,
  160. 'uploader': uploader,
  161. 'timestamp': timestamp,
  162. 'duration': duration,
  163. 'formats': formats,
  164. 'thumbnail': self._og_search_thumbnail(webpage),
  165. 'description': description,
  166. 'age_limit': age_limit,
  167. 'view_count': view_count,
  168. }
  169. class NewgroundsPlaylistIE(InfoExtractor):
  170. IE_NAME = 'Newgrounds:playlist'
  171. _VALID_URL = r'https?://(?:www\.)?newgrounds\.com/(?:collection|[^/]+/search/[^/]+)/(?P<id>[^/?#&]+)'
  172. _TESTS = [{
  173. 'url': 'https://www.newgrounds.com/collection/cats',
  174. 'info_dict': {
  175. 'id': 'cats',
  176. 'title': 'Cats',
  177. },
  178. 'playlist_mincount': 45,
  179. }, {
  180. 'url': 'https://www.newgrounds.com/collection/dogs',
  181. 'info_dict': {
  182. 'id': 'dogs',
  183. 'title': 'Dogs',
  184. },
  185. 'playlist_mincount': 26,
  186. }, {
  187. 'url': 'http://www.newgrounds.com/audio/search/title/cats',
  188. 'only_matching': True,
  189. }]
  190. def _real_extract(self, url):
  191. playlist_id = self._match_id(url)
  192. webpage = self._download_webpage(url, playlist_id)
  193. title = self._html_extract_title(webpage, default=None)
  194. # cut left menu
  195. webpage = self._search_regex(
  196. r'(?s)<div[^>]+\bclass=["\']column wide(.+)',
  197. webpage, 'wide column', default=webpage)
  198. entries = []
  199. for a, path, media_id in re.findall(
  200. r'(<a[^>]+\bhref=["\'][^"\']+((?:portal/view|audio/listen)/(\d+))[^>]+>)',
  201. webpage):
  202. a_class = extract_attributes(a).get('class')
  203. if a_class not in ('item-portalsubmission', 'item-audiosubmission'):
  204. continue
  205. entries.append(
  206. self.url_result(
  207. f'https://www.newgrounds.com/{path}',
  208. ie=NewgroundsIE.ie_key(), video_id=media_id))
  209. return self.playlist_result(entries, playlist_id, title)
  210. class NewgroundsUserIE(InfoExtractor):
  211. IE_NAME = 'Newgrounds:user'
  212. _VALID_URL = r'https?://(?P<id>[^\.]+)\.newgrounds\.com/(?:movies|audio)/?(?:[#?]|$)'
  213. _TESTS = [{
  214. 'url': 'https://burn7.newgrounds.com/audio',
  215. 'info_dict': {
  216. 'id': 'burn7',
  217. },
  218. 'playlist_mincount': 150,
  219. }, {
  220. 'url': 'https://burn7.newgrounds.com/movies',
  221. 'info_dict': {
  222. 'id': 'burn7',
  223. },
  224. 'playlist_mincount': 2,
  225. }, {
  226. 'url': 'https://brian-beaton.newgrounds.com/movies',
  227. 'info_dict': {
  228. 'id': 'brian-beaton',
  229. },
  230. 'playlist_mincount': 10,
  231. }]
  232. _PAGE_SIZE = 30
  233. def _fetch_page(self, channel_id, url, page):
  234. page += 1
  235. posts_info = self._download_json(
  236. f'{url}/page/{page}', channel_id,
  237. note=f'Downloading page {page}', headers={
  238. 'Accept': 'application/json, text/javascript, */*; q = 0.01',
  239. 'X-Requested-With': 'XMLHttpRequest',
  240. })
  241. sequence = posts_info.get('sequence', [])
  242. for year in sequence:
  243. posts = try_get(posts_info, lambda x: x['years'][str(year)]['items'])
  244. for post in posts:
  245. path, media_id = self._search_regex(
  246. r'<a[^>]+\bhref=["\'][^"\']+((?:portal/view|audio/listen)/(\d+))[^>]+>',
  247. post, 'url', group=(1, 2))
  248. yield self.url_result(f'https://www.newgrounds.com/{path}', NewgroundsIE.ie_key(), media_id)
  249. def _real_extract(self, url):
  250. channel_id = self._match_id(url)
  251. entries = OnDemandPagedList(functools.partial(
  252. self._fetch_page, channel_id, url), self._PAGE_SIZE)
  253. return self.playlist_result(entries, channel_id)