Skip to content

Instantly share code, notes, and snippets.

@juliusmh
Created February 15, 2016 20:26
Show Gist options
  • Save juliusmh/58fe8933743e229590a7 to your computer and use it in GitHub Desktop.
Save juliusmh/58fe8933743e229590a7 to your computer and use it in GitHub Desktop.
import requests, math, os
from bs4 import BeautifulSoup
SAMPLESPERPAGE = 15
SAMPLEDIRECTORY = "samples"
class Fetcher:
def __init__(self):
self.s = requests.session()
def start(self, query=None, n=None):
mydir = SAMPLEDIRECTORY
if query is not None:
mydir += "_" + query.lower()
if not os.path.exists(mydir):
os.makedirs(mydir)
url = "https://freesound.org/search/?s=created+desc&g=0&q=" + query +"&f=&page="
pages = int(math.floor(n / SAMPLESPERPAGE))
for p in range(1, pages+1): #LOOP THROUGH PAGES
page = self.s.get(url + str(p)).text
soup = BeautifulSoup(page, 'html.parser')
samples = soup.findAll('div', { "class" : "sample_player_small" })
for sample in samples:
href = sample.find("a", {"class" : "mp3_file"})
file_url = href.get("href")
file_name = href.text.replace(".", "").replace('"', "").replace('/', "").replace("\\", "").replace(" - mp3 version", "").replace(".wav", "").replace("-", "").replace(" ", "_")
if not ".mp3" in file_name: file_name += ".mp3"
print "Fetching <%s>" % file_name
self.download_file("https://freesound.org" + file_url, mydir + "/" + file_name)
def download_file(self, url, local_filename):
if not os.path.exists(local_filename):
r = self.s.get(url, stream=True)
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
f.flush()
fetcher = Fetcher()
try:
a = ["clack"]
for q in a:
fetcher.start(q, 15)
except KeyboardInterrupt:
print "Bye"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment