Created
July 25, 2012 13:08
-
-
Save lukaszb/3176104 to your computer and use it in GitHub Desktop.
www.freesound.org fetcher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
I couldn't register at www.freesound.org so I wrote this little script to make | |
downloading sounds from their site easy. | |
Usage | |
----- | |
getfreesound.py URL | |
example:: | |
getfreesound.py http://www.freesound.org/people/yewbic/sounds/33796/ | |
""" | |
import os | |
import sys | |
import urllib2 | |
import unittest | |
from lxml.html import fromstring | |
def get_content(url): | |
return urllib2.urlopen(url).read() | |
def get_meta_content(content, property_name): | |
tree = fromstring(content) | |
for meta in tree.cssselect('head meta'): | |
if meta.get('property') == property_name: | |
return meta.get('content') | |
return '' | |
def get_sound_url(content): | |
return get_meta_content(content, 'og:audio') | |
def get_sound_name(content): | |
return get_meta_content(content, 'og:audio:title') | |
def get_sound_for_url(url): | |
content = get_content(url) | |
sound_url = get_sound_url(content) | |
print "Got proper URL: %s" % sound_url | |
sound_name = get_sound_name(content) | |
print "Got filename: %s" % sound_name | |
if os.path.exists(sound_name): | |
print "Error: %s already exists!" % sound_name | |
sys.exit(1) | |
with open(sound_name, 'w') as fout: | |
fout.write(get_content(sound_url)) | |
print "Sound saved!" | |
class Tests(unittest.TestCase): | |
def setUp(self): | |
self.content = """ | |
<html> | |
<head> | |
<script type="text/javascript"> | |
swfobject.registerObject("player_33796", "10"); | |
</script> | |
<meta property="og:title" content="ambience03.wav by yewbic" /> | |
<meta property="og:type" content="song" /> | |
<meta property="og:audio" content="http://www.freesound.org/data/previews/33/33796_208116-lq.mp3" /> | |
<meta property="og:audio:artist" content="yewbic" /> | |
<meta property="og:audio:title" content="ambience03.wav" /> | |
<meta property="og:url" content="http://www.freesound.org/people/yewbic/sounds/33796/" /> | |
<meta property="og:audio:type" content="application/mp3" /> | |
<!-- <meta property="og:image" content="http://www.freesound.org/data/displays/33/33796_208116_wave_M.png" /> --> | |
<meta property="og:site_name" content="Freesound.org" /> | |
<meta property="fb:admins" content="100002130282170" /> | |
</head> | |
<body></body> | |
</html>""" | |
def test_get_sound_url(self): | |
self.assertEqual(get_sound_url(self.content), | |
'http://www.freesound.org/data/previews/33/33796_208116-lq.mp3') | |
def test_get_sound_name(self): | |
self.assertEqual(get_sound_name(self.content), 'ambience03.wav') | |
def main(): | |
url = sys.argv[1] | |
get_sound_for_url(url) | |
if __name__ == '__main__': | |
#unittest.main() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment