Skip to content

Instantly share code, notes, and snippets.

@keunwoochoi
Last active July 11, 2020 06:38
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keunwoochoi/1a62e98f73e0986c156ca55b1b44c510 to your computer and use it in GitHub Desktop.
Save keunwoochoi/1a62e98f73e0986c156ca55b1b44c510 to your computer and use it in GitHub Desktop.
how to crawl freesound
# Keunwoo Choi
# This example crawl snoring sound by searching keyword 'snore'.
from __future__ import print_function
import freesound # $ git clone https://github.com/MTG/freesound-python
import os
import sys
api_key = 'YOUR_API_KEY'
folder = 'data_freesound/' # folder to save
freesound_client = freesound.FreesoundClient()
freesound_client.set_token(api_key)
try:
os.mkdir(folder)
except:
pass
# Search Example
print("Searching for 'snore':")
print("----------------------------")
results_pager = freesound_client.text_search(
query="snore",
# filter="tag:tenuto duration:[1.0 TO 15.0]",
sort="rating_desc",
fields="id,name,previews,username"
)
print("Num results:", results_pager.count)
print("\t----- PAGE 1 -----")
for sound in results_pager:
print("\t-", sound.name, "by", sound.username)
filename = sound.id + '_' + sound.name.replace(u'/', '_')+".mp3"
if not os.path.exists(folder + filename):
sound.retrieve_preview(folder, filename)
for page_idx in range(results_pager.count):
print("\t----- PAGE {} -----".format())
results_pager = results_pager.next_page(page_idx + 2)
for sound in results_pager:
print("\t-", sound.name, "by", sound.username)
filename = sound.id + '_' + sound.name.replace(u'/', '_')+".mp3"
if not os.path.exists(folder + filename):
sound.retrieve_preview(folder, filename)
print()
@PlanNoa
Copy link

PlanNoa commented Apr 16, 2019

next page() take only one value. so I think that you should change like this:

results_pager = results_pager.next_page()
for page_idx in range(results_pager.count): 
    print("\t----- PAGE", str(page_idx + 2), "-----") 
    results_pager = results_pager.next_page()

@PlanNoa
Copy link

PlanNoa commented Apr 16, 2019

Also sound.id is int value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment