-
Notifications
You must be signed in to change notification settings - Fork 1
/
JIF_Puller_mainbackup.py
149 lines (132 loc) · 5.76 KB
/
JIF_Puller_mainbackup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import crossref_commons.retrieval
import streamlit as st
import pandas as pd
from habanero import counts
import time
CR_API_MAILTO = {"Mailto": "martindalete@ornl.gov"}
headers = {'Mailto':'martindalete@ornl.gov'}
#read in most recent JIF data
IFs = pd.read_csv(r"https://raw.githubusercontent.com/martindalete/JIF_Tool/main/JIFs_2022-08-26.csv?raw=true")
#IFs = pd.read_csv(r"C:\Users\9ex\OneDrive - Oak Ridge National Laboratory\streamlit\JIF\JIFs_2022-08-26.csv")
#IFs['ISSN'] = IFs['ISSN'].str.replace('-', '')
#IFs['eISSN'] = IFs['eISSN'].str.replace('-', '')
#st.write(IFs)
#create empty lists to which we will append API-gathered data
identifiers = []
csv = None
#convert dataframe to csv for exporting purposes
@st.cache(suppress_st_warning=True)
def convert_df(df):
# IMPORTANT: Cache the conversion to prevent computation on every rerun
return df.to_csv(index=False).encode('utf-8')
#main function that uses list of DOIs with API call
@st.cache(suppress_st_warning=True)
def crossref_loop(dataframe):
global csv
for i in range(len(df)):
percent_complete = (i+1)/len(df)
DOI = str(df.iloc[i]['DOIs'])
try:
crossref_payload = crossref_commons.retrieval.get_publication_as_json(str(DOI))
except:
ids = 'No ISSN(s) Found'
ISSN = 'No ISSN Found'
eISSN = 'No eISSN Found'
article_title = 'No Article Title Found'
times_cited = 0
source_title = 'No Source Title Found'
identifiers.append([DOI,ISSN,source_title,article_title,times_cited])
identifiers.append([DOI,eISSN,source_title,article_title,times_cited])
my_bar.progress(percent_complete)
continue
try:
ids = crossref_payload['ISSN']
#st.write(ids)
except:
ids = 'No ISSN(s) Found'
ISSN = 'No ISSN Found'
eISSN = 'No eISSN Found'
try:
article_title = crossref_payload['title'][0]
except:
article_title = 'No Article Title Found'
try:
source_title = crossref_payload['container-title'][0]
except:
source_title = 'No Source Title Found'
try:
times_cited = counts.citation_count(doi = str(DOI))
except:
times_cited = 0
identifiers.append([DOI,ISSN,source_title,article_title,times_cited])
identifiers.append([DOI,eISSN,source_title,article_title,times_cited])
my_bar.progress(percent_complete)
continue
try:
ISSN = ids[0]
except:
ISSN = 'No ISSN Found'
try:
eISSN = ids[1]
except:
eISSN = 'No eISSN Found'
try:
article_title = crossref_payload['title'][0]
except:
article_title = 'No Title Found'
try:
source_title = crossref_payload['container-title'][0]
except:
source_title = 'No Source Title Found'
try:
times_cited = counts.citation_count(doi = str(DOI))
except:
times_cited = 0
identifiers.append([DOI,ISSN,source_title,article_title,times_cited])
identifiers.append([DOI,eISSN,source_title,article_title,times_cited])
my_bar.progress(percent_complete)
time.sleep(0.05)
identifiers_df = pd.DataFrame(identifiers, columns = ['DOI','Identifier','Source Title','Article Title','Times Cited'])
#merge (join) found data with JIF data
identifiers_merged_1 = pd.merge(identifiers_df, IFs, how = "left", left_on=['Identifier'], right_on=['eISSN'])
identifiers_merged_2 = pd.merge(identifiers_df, IFs, how = "left", left_on=['Identifier'], right_on=['ISSN'])
#subset merged data to only show columns for DOI and JIF
identifiers_abbreviated_1 = identifiers_merged_1[['DOI','Identifier','Journal Impact Factor', 'Source Title', 'Article Title', 'Times Cited']]
identifiers_abbreviated_2 = identifiers_merged_2[['DOI','Identifier','Journal Impact Factor', 'Source Title', 'Article Title', 'Times Cited']]
#stack ISSN/eISSN dataframes on top of each other and then...
df_final_2 = pd.concat([identifiers_abbreviated_1, identifiers_abbreviated_2])
df_final_2 = df_final_2.reset_index(drop=True)
#st.write(df_final_2)
#display final dataframe
df_final_2 = df_final_2.drop_duplicates()
#test_df = df_final_2.sort_values('Journal Impact Factor', ascending=False)
#test_df = test_df[~test_df.duplicated('DOI')]
test_df['Journal Impact Factor'] = test_df['Journal Impact Factor'].astype(str)
test_df['Journal Impact Factor'] = test_df['Journal Impact Factor'].replace('nan', 'No JIF Found')
test_df = test_df.reset_index(drop=True)
st.dataframe(test_df)
#convert df to csv
csv = convert_df(test_df)
@st.cache(suppress_st_warning=True)
def show_download_button():
global csv
st.download_button(
label="Download data as CSV",
data=csv,
file_name='DOIs_with_JIFs.csv',
mime='text/csv')
#streamlit upload button
data = st.file_uploader("Upload a CSV of DOIs, one per line, no header column")
#read in uploaded CSV and write to dataframe
if data is not None:
df = pd.read_csv(data, header=None)
df = df.rename(columns={0: 'DOIs'})
#display dataframe of uploaded DOIs
st.dataframe(df)
#introduce streamlit proress bar widget
my_bar = st.progress(0.0)
crossref_loop(df)
if csv is not None:
st.balloons()
st.success('Your Download is Ready!')
show_download_button()