# -*- coding: utf-8 -*-
"""
Created on Mon Jul  5 11:59:22 2021

@author: lukas
"""
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 28 18:38:03 2021

@author: lukas
"""
import json
import requests
import time



start_time = time.time()




data={}
greek_dois={}

f = open('greek-dois.json',)
  
data = json.load(f)
  
f.close()

errors=[]
final={}       
with open("articles_by_popularity.csv","r") as f:
    for line in f:
            sp=line.split()
            if sp[2] in data:
                try:
                    r=requests.get('https://api.crossref.org/works/{}'.format(sp[2]))
                    time.sleep(0.05)
                except: 
                    errors.append(sp[2])
                    with open('errors.json', 'w') as outfile:    
                        json.dump(errors, outfile)
                if r.status_code==200:
                    if r.text=='Resource not found.':
                        continue;
                    else:
                        resp=r.json()
                        if resp['status']=='ok':
                            message=resp['message']
                            final.setdefault("data",[]).append({"doi": sp[2],"title":message['title'], "venue":message['container-title'], "date":message['issued'],  "influence": sp[3], 
                                       "popularity_alt": sp[4], "popularity": sp[5], "influence_alt": sp[6], "social": sp[7] })
                            
                    
        

# affiliations=[]
# errors=[]
# for doi in data:
#         try:
#             r=requests.get('https://api.crossref.org/works/{}'.format(doi))
#             time.sleep(0.05)
#         except: 
#             errors.append(doi)
#             with open('errors.json', 'w') as outfile:    
#                 json.dump(errors, outfile)
#         if r.status_code==200:
#             if r.text=='Resource not found.':
#                 continue;
#             else:
#                 resp=r.json()
#                 if resp['status']=='ok':
#                     message=resp['message']
#                     print(message['container-title'])
#                     print(message['title'])
#                     print(message['issued'])
                    # if 'author' in message:
                    #     authors=message['author']
                    #     for author in authors:
                    #         if author['affiliation']:
                    #             for affil in author['affiliation']:
                    #                 keywords=['Greece', ' GR ', ' Gr ', ' gr ', 'greece', 'China']
                    #                 if any(x in affil['name'] for x in keywords):
                    #                     country=affil['name']
                    #                     if doi not in affiliations:
                    #                         affiliations.append(doi)
                    #                         with open('greek-dois.json', 'w') as outfile:    
                    #                             json.dump(affiliations, outfile)
                    #                     else:
                    #                         break


  

with open('final-metadata.json', 'w') as outfile:    
    json.dump(final, outfile)
    
print("--- %s seconds ---" % (time.time() - start_time))
