A collection of python tools to download, plot and export OpenAlex data in graph format.
To download the data of articles published by an author,
you first need to know his OpenAlex ids.
If you only know his name,
you can use the following interactive command to save his ids,
one per row, in authors.txt.
$ ./download.py author "paul erdos" >> authors.txt
- page 1/1
Do you want to include Paul Erdős (Hungarian Academy of Sciences)? y/n y
Do you want to include Paul L. Erdos (unknown institution)? y/n n
Do you want to include PAUL ERDOS (unknown institution)? y/n n
Do you want to include Paul Erdös (unknown institution)? y/n nTo download the data of the works of the authors in authors.txt,
you can use:
./download.py works authors.txt > works.jsonlTo download the data of the articles cited by the works in works.jsonl,
you can use:
./download.py cited_by works.jsonl > cited_by.jsonlTo download the data of the articles that cite some work in works.jsonl,
you can use:
./download.py cites works.jsonl > cites.jsonlAll the works downloads can include duplicated items. This can be due to, for example, a work being cited by two different works. All the tools in this repository are implemented in such a way that this fact does not alter the results nor increase the number of queries required. But if you want to use any external tool, you may want to remove duplicated works:
sort works.jsonl | uniq > unique_works.jsonlAll the previous commands accept the same options, described in this section.
OpenAlex has a polite pool that is much faster
and gives more consistent response times.
To get into the polite tool,
you can set your email or api key with the --email or --api-key options.
When the result of a query is too big, OpenAlex paginates the response.
You can decide the number of results to include per page
with the --per-page option.
In principle, it can be up to 200.
But sometimes the server can not return so big results,
so it is recommended to use a smaller value.
The default is 50.
In case a query fails, you may want to retry it.
You can set the maximum number of retries with --max-retries (default: 0),
the factor to determine the delay between retries
with --retry-backoff-factor (default: 0.1),
and the HTTP error codes that trigger a retry
with --retry-http-codes (default: 429, 500 and 503).
Even with that, a query can fail.
To avoid having to restart the download from the beginning,
you can restart a query from the subquery and page were it stopped
with the options -q or --start-query (default: 1)
and -p or --start-page (default: 1).
As the result format is a jsonl where each line is an article,
you can just append the new results to the old ones:
./download.py cites works.jsonl -q 15 -p 4 >> cites.jsonlIn addition, every command in this repository has an option -h or --help
that shows a help message.
You can plot an histogram of the number of citations in cites.jsonl per year
that cite the works in works.jsonl with the following command:
./plot.py works.jsonl cites.jsonl plot.pdfBy default, it plots the citations per year of publication of the cited work.
If you want to plot them per year of the publication of the work that cites,
you can add the option -y or --per-year-of-citation.
You can also color the citations by the cited work
with the options -c works or --color works,
and color them by the works that cites with -c cites or --color cites.
As too many colors can be needed,
you can limit the number of colors to be used
with the option -n or --ncolors (default: 10).
When this option is used,
less cited works or works that cite less are painted with the same color,
in order to only use the given number of colors.
If you want to export the data of a works file to a graph,
you can use the ./graph.py command.
You can use the option -f or --format
to choose between GML and GraphML as output format (default: GML).
You can produce the graph in two different ways: the nodes can be the works or the authors.
This command will create a graph
where the nodes will be the works in works.jsonl,
and there will be an edge from a node A to a node B if A cites B.
./graph.py works works.jsonl graph_works.gmlFor example, if you want to create a graph with all the Erdős works, the works they cite, and the works that cite them, you can use the following commands with the files downloaded in the download section of this readme:
cat works.jsonl cites.jsonl cited_by.jsonl > all.jsonl
./graph.py works all.jsonl graph_works.gmlThe edge metadata is just the date of publication of the work that cites.
The node metadata is, by default, the title, the publication date,
the authors, and the primary location.
You can change the node metadata with option -m or --metadata.
A similar graph you can produce is the one where the nodes are the authors.
./graph.py authors all.jsonl graph_authors.gmlIn this case, the weight of an edge from an author
where