import time
import json
from zotero import get_cr_metadata, xwalk_cr_to_zotero, insert_collection_item
from datetime import datetime
import pandas as pd
USER        = "8957093" # todo : parameterize the user
COLLECTIONS = ["EGWRJNSS"]
INPUT_DOIS = pd.read_csv("../outputs/full_nsf_doi_project_summary.tsv", sep='\t')[['doi']].drop_duplicates()['doi'].to_list()

date_time = datetime.now().strftime("%Y%m%d_%H%M%S")
    
# with open(INPUT_DOIS) as f, \
#      open(f"{date_time}.txt", "a") as f_err:
#     # todo : parameterize the do list
# f
#     i = 1
with open(f"{date_time}.txt", "a") as f_err:
    for i, doi  in enumerate(INPUT_DOIS):
        md = get_cr_metadata(doi)

        try:
            print(f"[info] ({i}) processing {md['DOI']} for zotero")
            # print(md['created']['date-time'].split('-')[0])

            zotero_md = xwalk_cr_to_zotero(md, collections=COLLECTIONS) # todo : parameterize the collections

            if zotero_md:
                print(f"[info] inserting {md['DOI']} into zotero")
                insert_collection_item(USER, [zotero_md])
                i+=1
            else:
                print(f"[warn] skipping {md['DOI']}: crosswalk failed")
        except Exception as e:
            print(e)
            f_err.write(f"{doi}\n")
            if not md:
                print(f"[warn] issue processing {doi} for zotero")
            else:
                print(f"[warn] issue processing {doi} for zotero")
                print(md)
                print("--")
    print("[done]")
[info] (0) processing 10.1016/j.geomorph.2015.03.039 for zotero
[info] inserting 10.1016/j.geomorph.2015.03.039 into zotero
[info] POST OK
[info] (1) processing 10.2110/sedred.2013.4.9 for zotero
[info] inserting 10.2110/sedred.2013.4.9 into zotero
[info] POST OK
[info] (2) processing 10.1029/2021ef002088 for zotero
[info] inserting 10.1029/2021ef002088 into zotero
[info] POST OK
[info] (3) processing 10.1029/2017jf004576 for zotero
[info] inserting 10.1029/2017jf004576 into zotero
[info] POST OK
[info] (4) processing 10.1126/science.342.6162.1041-b for zotero
[info] inserting 10.1126/science.342.6162.1041-b into zotero
[info] POST OK
[info] (5) processing 10.1002/2014eo200001 for zotero
[info] inserting 10.1002/2014eo200001 into zotero
[info] POST OK
[info] (6) processing 10.1130/gsatg248gw.1 for zotero
[info] inserting 10.1130/gsatg248gw.1 into zotero
[info] POST OK
[info] (7) processing 10.1130/2022.2558(01) for zotero
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
Cell In [2], line 20
     17 print(f"[info] ({i}) processing {md['DOI']} for zotero")
     18 # print(md['created']['date-time'].split('-')[0])
---> 20 zotero_md = xwalk_cr_to_zotero(md, collections=COLLECTIONS) # todo : parameterize the collections
     22 if zotero_md:
     23     print(f"[info] inserting {md['DOI']} into zotero")

File ~\Desktop\projects\earthcube\jbreport\analysis\notebooks\zotero.py:90, in xwalk_cr_to_zotero(cr_md, collections, type)
     88 # do the walkin'
     89 template['title'] = cr_md.get('title', "?")
---> 90 template["abstractNote"] = get_semschol_abstract(cr_md['DOI'])  # we can get abstracts from another service
     91 template["publicationTitle"] = cr_md.get('title', "?")
     92 template["volume"] =  cr_md.get("volume", "")

File ~\Desktop\projects\earthcube\jbreport\analysis\notebooks\zotero.py:66, in get_semschol_abstract(doi, be_nice)
     64 def get_semschol_abstract(doi, be_nice=2.25):
     65     import time
---> 66     time.sleep(be_nice)
     68     with requests.get(f"https://api.semanticscholar.org/v1/paper/{doi}") as r:
     69         if r.status_code == 200:

KeyboardInterrupt: