ntivirikin / xeno-canto-py

Python wrapper for the xeno-canto.org API to aid in downloading and managing recordings.
MIT License
35 stars 14 forks source link

Empty download #14

Open MordicusEtCubitus opened 5 months ago

MordicusEtCubitus commented 5 months ago

Hi,

I'm trying to use your nice tool from command line but always got an empty dataset:

import json
from urllib import request, error
url = 'https://www.xeno-canto.org/api/2/recordings?query=pinson'
try:
    r = request.urlopen(url)
except error.HTTPError as e:
    print("An error has occurred: " + str(e))
    exit()

data = json.loads(r.read().decode('UTF-8'))
print(data)

got

  {'numRecordings': '0', 'numSpecies': '0', 'page': 1, 'numPages': 1, 'recordings': []}

Same with

import requests
url = 'https://xeno-canto.org/api/2/recordings'
headers = {"User-Agent": "Mozilla/5.0" }
params = {"query" : "pinson", "page" : 1 }
r = requests.get(url, params=params)
r.json()

I think there is another security on the server preventing use outside browser, but I do not find what it is

If you have an idea ?

Thanks for helping

MordicusEtCubitus commented 5 months ago

Ok, I think I got it, this may help to update your code:

I've started to add my browser headers one by one:

headers = {"User-Agent": "Mozilla/5.0"
          , "Accept-Encoding": "gzip, deflate, br, zstd"
           , "Accept-Language": "fr,en;q=0.5"
          }

When adding "Accept-Language" I did got data !

Maybe the 2 others are required too.