newgrounds.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. from __future__ import unicode_literals
  2. from .common import InfoExtractor
  3. from ..utils import int_or_none
  4. class NewgroundsIE(InfoExtractor):
  5. _VALID_URL = r'https?://(?:www\.)?newgrounds\.com/(?:audio/listen|portal/view)/(?P<id>[0-9]+)'
  6. _TESTS = [{
  7. 'url': 'https://www.newgrounds.com/audio/listen/549479',
  8. 'md5': 'fe6033d297591288fa1c1f780386f07a',
  9. 'info_dict': {
  10. 'id': '549479',
  11. 'ext': 'mp3',
  12. 'title': 'B7 - BusMode',
  13. 'uploader': 'Burn7',
  14. }
  15. }, {
  16. 'url': 'https://www.newgrounds.com/portal/view/673111',
  17. 'md5': '3394735822aab2478c31b1004fe5e5bc',
  18. 'info_dict': {
  19. 'id': '673111',
  20. 'ext': 'mp4',
  21. 'title': 'Dancin',
  22. 'uploader': 'Squirrelman82',
  23. },
  24. }, {
  25. # source format unavailable, additional mp4 formats
  26. 'url': 'http://www.newgrounds.com/portal/view/689400',
  27. 'info_dict': {
  28. 'id': '689400',
  29. 'ext': 'mp4',
  30. 'title': 'ZTV News Episode 8',
  31. 'uploader': 'BennettTheSage',
  32. },
  33. 'params': {
  34. 'skip_download': True,
  35. },
  36. }]
  37. def _real_extract(self, url):
  38. media_id = self._match_id(url)
  39. webpage = self._download_webpage(url, media_id)
  40. title = self._html_search_regex(
  41. r'<title>([^>]+)</title>', webpage, 'title')
  42. video_url = self._parse_json(self._search_regex(
  43. r'"url"\s*:\s*("[^"]+"),', webpage, ''), media_id)
  44. formats = [{
  45. 'url': video_url,
  46. 'format_id': 'source',
  47. 'quality': 1,
  48. }]
  49. max_resolution = int_or_none(self._search_regex(
  50. r'max_resolution["\']\s*:\s*(\d+)', webpage, 'max resolution',
  51. default=None))
  52. if max_resolution:
  53. url_base = video_url.rpartition('.')[0]
  54. for resolution in (360, 720, 1080):
  55. if resolution > max_resolution:
  56. break
  57. formats.append({
  58. 'url': '%s.%dp.mp4' % (url_base, resolution),
  59. 'format_id': '%dp' % resolution,
  60. 'height': resolution,
  61. })
  62. self._check_formats(formats, media_id)
  63. self._sort_formats(formats)
  64. uploader = self._html_search_regex(
  65. r'(?:Author|Writer)\s*<a[^>]+>([^<]+)', webpage, 'uploader',
  66. fatal=False)
  67. return {
  68. 'id': media_id,
  69. 'title': title,
  70. 'uploader': uploader,
  71. 'formats': formats,
  72. }