freesound.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. determine_ext,
  6. float_or_none,
  7. get_element_by_class,
  8. get_element_by_id,
  9. int_or_none,
  10. parse_filesize,
  11. unified_strdate,
  12. )
  13. class FreesoundIE(InfoExtractor):
  14. _VALID_URL = r'https?://(?:www\.)?freesound\.org/people/([^/]+)/sounds/(?P<id>[^/]+)'
  15. _TEST = {
  16. 'url': 'http://www.freesound.org/people/miklovan/sounds/194503/',
  17. 'md5': '12280ceb42c81f19a515c745eae07650',
  18. 'info_dict': {
  19. 'id': '194503',
  20. 'ext': 'mp3',
  21. 'title': 'gulls in the city.wav',
  22. 'uploader': 'miklovan',
  23. 'description': 'the sounds of seagulls in the city',
  24. }
  25. }
  26. def _real_extract(self, url):
  27. mobj = re.match(self._VALID_URL, url)
  28. music_id = mobj.group('id')
  29. webpage = self._download_webpage(url, music_id)
  30. audio_url = self._og_search_property('audio', webpage, 'song url')
  31. title = self._og_search_property('audio:title', webpage, 'song title')
  32. duration = float_or_none(get_element_by_class('duration', webpage), scale=1000)
  33. tags = get_element_by_class('tags', webpage)
  34. sound_info = get_element_by_id('sound_information_box', webpage)
  35. release_date = get_element_by_id('sound_date', webpage)
  36. description = self._html_search_regex(
  37. r'<div id="sound_description">(.*?)</div>', webpage, 'description',
  38. fatal=False, flags=re.DOTALL)
  39. download_count = int_or_none(self._html_search_regex(
  40. r'Downloaded.*>(\d+)<', webpage, 'downloaded', fatal=False))
  41. filesize = float_or_none(parse_filesize(self._search_regex(
  42. r'Filesize</dt><dd>(.*)</dd>', sound_info, 'file size (approx)', fatal=False)))
  43. if release_date:
  44. release_date = unified_strdate(release_date.replace('th', ''))
  45. bitdepth = self._html_search_regex(
  46. r'Bitdepth</dt><dd>(.*)</dd>', sound_info, 'Bitdepth', fatal=False)
  47. channels = self._html_search_regex(
  48. r'Channels</dt><dd>(.*)</dd>', sound_info, 'Channels info', fatal=False)
  49. formats = [{
  50. 'url': audio_url,
  51. 'id': music_id,
  52. 'format_id': self._og_search_property('audio:type', webpage, 'audio format', fatal=False),
  53. 'format_note': '{0} {1} {2}'.format(determine_ext(audio_url), bitdepth, channels),
  54. 'filesize_approx': filesize,
  55. 'asr': int_or_none(self._html_search_regex(
  56. r'Samplerate</dt><dd>(\d+).*</dd>',
  57. sound_info, 'samplerate', fatal=False)),
  58. }]
  59. return {
  60. 'id': music_id,
  61. 'title': title,
  62. 'uploader': self._og_search_property('audio:artist', webpage, 'music uploader', fatal=False),
  63. 'description': description,
  64. 'duration': duration,
  65. 'tags': [self._html_search_regex(r'>(.*)</a>', t, 'tag', fatal=False)
  66. for t in tags.split('\n') if t.strip()],
  67. 'formats': formats,
  68. 'release_date': release_date,
  69. 'likes_count': download_count,
  70. }