Created
August 24, 2020 04:56
-
-
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
This file contains hidden or 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
| 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) | |
Author
nwatab
commented
Aug 24, 2020

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