User:Feliciss/Sample code to get data from ADS

This is a sample code to get data from the ADS database and output them. You can change values of the variable "bibcode" and "fl" in the code to see output differences.

To run, you should:

#!/usr/local/bin/python3

from ads import SearchQuery
from requests import Response, get
from yaml import safe_load

# Using first author to dedup search queries.
# Using "," to separate author first and last names.
# Using solr's wildcard "*" to match author middle and first names.
# Query format: last name, first name
response: Response = get("https://raw.githubusercontent.com/adsabs/adsabs-dev-api/master/openapi/parameters.yaml")
yaml = safe_load(response.text)
search_field = yaml["fl"]["schema"]["items"]["enum"]
# delete ADS internal items before querying
search_field.remove("id")
search_field.remove("links_data")

search_query: SearchQuery = SearchQuery(
    bibcode="2022MNRAS.513.5847P", fl=search_field
)

for article in search_query:
    for key, value in sorted(article.items(), reverse=True):
        # ensure it has a key and the value isn't empty
        if hasattr(article, key) and value is not None:
            print(key, value)