Skip to content

Instantly share code, notes, and snippets.

@nwatab
Created August 24, 2020 04:56
Show Gist options
  • Select an option

  • Save nwatab/7e29ebfc772a8b8a5110ff5207ca4c95 to your computer and use it in GitHub Desktop.

Select an option

Save nwatab/7e29ebfc772a8b8a5110ff5207ca4c95 to your computer and use it in GitHub Desktop.
Fetch A class bird song .mp3's recorded in Japan from XENO CANTO
import requests
import os
// page has 1 to 4
url = 'https://www.xeno-canto.org/api/2/recordings?query=cnt:japan+q:a&page=1'
res = requests.get(url, headers={'Accept-language': 'ja'}).json()
def download_media(url, filename):
try:
res = requests.get(medialink)
except requests.exceptions.InvalidURL as e:
print(e)
return
with open (filename, 'wb') as localfile:
for data in res.iter_content():
localfile.write(data)
return
for record in res['recordings']:
print(record['url'])
medialink = 'https:' + record['file']
bird_name = record['en']
record_id = record['id']
filename = record_id + '.mp3'
savedir = os.path.join('data', bird_name)
if not os.path.exists(savedir): os.makedirs(savedir)
pathname = os.path.join(savedir, filename)
if record['file']:
download_media(medialink, pathname)
@nwatab
Copy link
Copy Markdown
Author

nwatab commented Aug 24, 2020

image

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