import json

from pybiblionet.openalex.core import string_generator_from_lite_regex, retrieve_articles, create_citation_graph, \
    create_coauthorship_graph, export_articles_to_csv, export_authors_to_csv, export_venues_to_csv, \
    export_institutions_to_csv

# This script retrieves academic articles from OpenAlex related to the concept of "15-minute city"
# using a regex-based query. It exports the retrieved articles and their authors to CSV files,
# then generates and saves two graph representations: a citation graph and a co-authorship graph.
# Finally, it prints the number of nodes and edges in each graph to give an overview of their structure.
if __name__ == "__main__" :


    queries = string_generator_from_lite_regex("(15)( )(minute|min)( )(city)")

    mail = "youremail@example.com"
    api_key = "YOUR_KEY" #how to obtain an api key https://developers.openalex.org/guides/authentication
                         #check your daily usage limit here https://openalex.org/settings/usage
    from_publication_date = "2019-01-01"

    to_publication_date = None
    json_file_path = retrieve_articles(queries, mail, api_key, from_publication_date, to_publication_date)
    print(json_file_path)
    export_institutions_to_csv(json_file_path,
                           fields_to_export=None,
                           export_path="15minute_institutions.csv"
                           )

    export_venues_to_csv(json_file_path,
                           fields_to_export=None,
                           export_path="15minute_venues.csv"
                           )

    export_articles_to_csv(json_file_path,
                           fields_to_export=None,
                           export_path="15minute_articles.csv"
                           )
    export_authors_to_csv(json_file_path,
                           fields_to_export=None,#["author id", "author orcid", "author display_name"],
                           export_path="15minute_authors.csv"
                           )

    articles = json.load(open(json_file_path))
    G_citation=create_citation_graph(articles,"15minute_citation_graph.GML", base_set=True)

    print(G_citation.number_of_nodes(),G_citation.number_of_edges())

    G_coauthorship=create_coauthorship_graph(articles,"15minute_coauthorship_graph.GML", base_set=True)

    print(G_coauthorship.number_of_nodes(),G_coauthorship.number_of_edges())

