Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eelsdb queries #64

Closed
PauTorru opened this issue Jul 1, 2024 · 2 comments · Fixed by #66
Closed

eelsdb queries #64

PauTorru opened this issue Jul 1, 2024 · 2 comments · Fixed by #66

Comments

@PauTorru
Copy link
Contributor

PauTorru commented Jul 1, 2024

The following code:


from exspy.misc.eels import eelsdb
results = eelsdb.eelsdb(spectrum_type="coreloss",element="Cu",edge="L2,3")

Produces this error

JSONDecodeError                           Traceback (most recent call last)
File ~\AppData\Local\anaconda3\Lib\site-packages\requests\models.py:971, in Response.json(self, **kwargs)
    970 try:
--> 971     return complexjson.loads(self.text, **kwargs)
    972 except JSONDecodeError as e:
    973     # Catch JSON-related errors and raise as requests.JSONDecodeError
    974     # This aliases json.JSONDecodeError and simplejson.JSONDecodeError

File ~\AppData\Local\anaconda3\Lib\json\__init__.py:346, in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    343 if (cls is None and object_hook is None and
    344         parse_int is None and parse_float is None and
    345         parse_constant is None and object_pairs_hook is None and not kw):
--> 346     return _default_decoder.decode(s)
    347 if cls is None:

File ~\AppData\Local\anaconda3\Lib\json\decoder.py:337, in JSONDecoder.decode(self, s, _w)
    333 """Return the Python representation of ``s`` (a ``str`` instance
    334 containing a JSON document).
    335 
    336 """
--> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338 end = _w(s, end).end()

File ~\AppData\Local\anaconda3\Lib\json\decoder.py:355, in JSONDecoder.raw_decode(self, s, idx)
    354 except StopIteration as err:
--> 355     raise JSONDecodeError("Expecting value", s, err.value) from None
    356 return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

JSONDecodeError                           Traceback (most recent call last)
Cell In[2], line 1
----> 1 results = eelsdb.eelsdb(spectrum_type="coreloss",element="Cu",edge="L2,3")

File ~\AppData\Local\anaconda3\Lib\site-packages\exspy\misc\eels\eelsdb.py:255, in eelsdb(spectrum_type, title, author, element, formula, edge, min_energy, max_energy, resolution, min_energy_compare, max_energy_compare, resolution_compare, max_n, monochromated, order, order_direction, verify_certificate, show_progressbar)
    251 request = requests.get(
    252     "https://api.eelsdb.eu/spectra", params=params, verify=verify_certificate
    253 )
    254 spectra = []
--> 255 jsons = request.json()
    256 if "message" in jsons:
    257     # Invalid query, EELSdb raises error.
    258     raise IOError(
    259         "Please report the following error to the eXSpy developers: "
    260         f"{jsons['message']}."
    261     )

File ~\AppData\Local\anaconda3\Lib\site-packages\requests\models.py:975, in Response.json(self, **kwargs)
    971     return complexjson.loads(self.text, **kwargs)
    972 except JSONDecodeError as e:
    973     # Catch JSON-related errors and raise as requests.JSONDecodeError
    974     # This aliases json.JSONDecodeError and simplejson.JSONDecodeError
--> 975     raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

upon further investigation, the eels db rejects the conection. I manually fixed the issue by:

headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36"}
request = requests.get(
    "https://api.eelsdb.eu/spectra", params={"type":"coreloss","element":"Cu","edge":"L2,3"}, verify=True,headers=headers
)
@ericpre
Copy link
Member

ericpre commented Jul 1, 2024

I have seen it and did notice that the json was empty but thought that it was transient as it happens once in a while.

I don't know why this would be necessary now but specifying the "user-agent" shouldn't hurt - for example, we do this to check for broken extremal links when building the hyperspy documentation to publisher and setting the user-agent helps to get some of them to work.

Do you want to make a pull request? The relevant code should be

request = requests.get(
"https://api.eelsdb.eu/spectra", params=params, verify=verify_certificate
)
spectra = []
jsons = request.json()

@llajaunie, any idea why it would be necessary now?

@PauTorru
Copy link
Contributor Author

PauTorru commented Jul 2, 2024

I just made the pull request.

@ericpre ericpre linked a pull request Jul 2, 2024 that will close this issue
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants