From e3acd5406ffacf44a32f8f2f79f5bdcf23939134 Mon Sep 17 00:00:00 2001 From: sh!zeeg Date: Thu, 5 Jan 2017 04:52:42 +0300 Subject: [PATCH 1/2] [Freesound] extended and fix "title" extraction closes #11602 --- youtube_dl/extractor/freesound.py | 53 ++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/youtube_dl/extractor/freesound.py b/youtube_dl/extractor/freesound.py index 5ff62af2a..099014cb9 100644 --- a/youtube_dl/extractor/freesound.py +++ b/youtube_dl/extractor/freesound.py @@ -3,6 +3,14 @@ from __future__ import unicode_literals import re from .common import InfoExtractor +from ..utils import ( + determine_ext, + get_element_by_class, + get_element_by_id, + int_or_none, + float_or_none, + unified_strdate, +) class FreesoundIE(InfoExtractor): @@ -23,17 +31,52 @@ class FreesoundIE(InfoExtractor): mobj = re.match(self._VALID_URL, url) music_id = mobj.group('id') webpage = self._download_webpage(url, music_id) - title = self._html_search_regex( - r'
.*?(.+?)', - webpage, 'music title', flags=re.DOTALL) description = self._html_search_regex( r'
(.*?)
', webpage, 'description', fatal=False, flags=re.DOTALL) + duration = float_or_none(get_element_by_class('duration', webpage)) + if duration: + duration = duration / 1000 + + tags = get_element_by_class('tags', webpage) + sound_info = get_element_by_id('sound_information_box', webpage) + release_date = get_element_by_id('sound_date', webpage) + filesize = float_or_none(self._search_regex( + r'Filesize
(\d+.\d+).*
', sound_info, 'file size (approx)', fatal=False)) + if filesize: + filesize = filesize * 1048576 + if release_date: + release_date = unified_strdate(release_date.replace('th', '')) + + channels = self._html_search_regex( + r'Channels
(.*)
', sound_info, 'Channels info', fatal=False) + bitdepth = self._html_search_regex( + r'Bitdepth
(.*)
', sound_info, 'Bitdepth', fatal=False) + + download_count = int_or_none(self._html_search_regex( + r'Downloaded.*>(\d+)<', webpage, 'downloaded', fatal=False)) + audio_url = self._og_search_property('audio', webpage, 'song url') + formats = [{ + 'url': audio_url, + 'id': music_id, + 'format_id': self._og_search_property('audio:type', webpage, 'audio format'), + 'format_note': '{0} {1} {2}'.format(determine_ext(audio_url), bitdepth, channels), + 'filesize_approx': filesize, + 'asr': int_or_none(self._html_search_regex( + r'Samplerate
(\d+).*
', + sound_info, 'samplerate', fatal=False)), + }] + return { 'id': music_id, - 'title': title, - 'url': self._og_search_property('audio', webpage, 'music url'), + 'title': self._og_search_property('audio:title', webpage, 'song title'), 'uploader': self._og_search_property('audio:artist', webpage, 'music uploader'), 'description': description, + 'duration': duration, + 'tags': [self._html_search_regex(r'>(.*)', t, 'tag', fatal=False) + for t in tags.split('\n') if t.strip()], + 'formats': formats, + 'release_date': release_date, + 'likes_count': download_count, } From 45372b7e7b2184d46d3bebecbba8872decbd2fb9 Mon Sep 17 00:00:00 2001 From: sh!zeeg Date: Sat, 7 Jan 2017 23:56:53 +0300 Subject: [PATCH 2/2] [Freesound] requested fixes --- youtube_dl/extractor/freesound.py | 40 ++++++++++++++++--------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/youtube_dl/extractor/freesound.py b/youtube_dl/extractor/freesound.py index 099014cb9..f0b2400cf 100644 --- a/youtube_dl/extractor/freesound.py +++ b/youtube_dl/extractor/freesound.py @@ -5,10 +5,11 @@ import re from .common import InfoExtractor from ..utils import ( determine_ext, + float_or_none, get_element_by_class, get_element_by_id, int_or_none, - float_or_none, + parse_filesize, unified_strdate, ) @@ -31,36 +32,37 @@ class FreesoundIE(InfoExtractor): mobj = re.match(self._VALID_URL, url) music_id = mobj.group('id') webpage = self._download_webpage(url, music_id) + + audio_url = self._og_search_property('audio', webpage, 'song url') + title = self._og_search_property('audio:title', webpage, 'song title') + duration = float_or_none(get_element_by_class('duration', webpage), scale=1000) + tags = get_element_by_class('tags', webpage) + sound_info = get_element_by_id('sound_information_box', webpage) + release_date = get_element_by_id('sound_date', webpage) + description = self._html_search_regex( r'
(.*?)
', webpage, 'description', fatal=False, flags=re.DOTALL) - duration = float_or_none(get_element_by_class('duration', webpage)) - if duration: - duration = duration / 1000 + download_count = int_or_none(self._html_search_regex( + r'Downloaded.*>(\d+)<', webpage, 'downloaded', fatal=False)) + + filesize = float_or_none(parse_filesize(self._search_regex( + r'Filesize
(.*)
', sound_info, 'file size (approx)', fatal=False))) - tags = get_element_by_class('tags', webpage) - sound_info = get_element_by_id('sound_information_box', webpage) - release_date = get_element_by_id('sound_date', webpage) - filesize = float_or_none(self._search_regex( - r'Filesize
(\d+.\d+).*
', sound_info, 'file size (approx)', fatal=False)) - if filesize: - filesize = filesize * 1048576 if release_date: release_date = unified_strdate(release_date.replace('th', '')) - channels = self._html_search_regex( - r'Channels
(.*)
', sound_info, 'Channels info', fatal=False) bitdepth = self._html_search_regex( r'Bitdepth
(.*)
', sound_info, 'Bitdepth', fatal=False) - download_count = int_or_none(self._html_search_regex( - r'Downloaded.*>(\d+)<', webpage, 'downloaded', fatal=False)) - audio_url = self._og_search_property('audio', webpage, 'song url') + channels = self._html_search_regex( + r'Channels
(.*)
', sound_info, 'Channels info', fatal=False) + formats = [{ 'url': audio_url, 'id': music_id, - 'format_id': self._og_search_property('audio:type', webpage, 'audio format'), + 'format_id': self._og_search_property('audio:type', webpage, 'audio format', fatal=False), 'format_note': '{0} {1} {2}'.format(determine_ext(audio_url), bitdepth, channels), 'filesize_approx': filesize, 'asr': int_or_none(self._html_search_regex( @@ -70,8 +72,8 @@ class FreesoundIE(InfoExtractor): return { 'id': music_id, - 'title': self._og_search_property('audio:title', webpage, 'song title'), - 'uploader': self._og_search_property('audio:artist', webpage, 'music uploader'), + 'title': title, + 'uploader': self._og_search_property('audio:artist', webpage, 'music uploader', fatal=False), 'description': description, 'duration': duration, 'tags': [self._html_search_regex(r'>(.*)', t, 'tag', fatal=False)