-
Notifications
You must be signed in to change notification settings - Fork 14
/
load_campaign.py
152 lines (110 loc) · 4.61 KB
/
load_campaign.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
150
from time import time
from app import db
from util import elapsed
from util import safe_commit
import argparse
from models.person import add_or_overwrite_person_from_orcid_id
from models.person import make_person
from models.person import Person
from models.orcid import clean_orcid
from models.orcid import NoOrcidException
# needs to be imported so the definitions get loaded into the registry
import jobs_defs
"""
Call from command line to add ORCID profiles based on IDs in a local CSV.
"""
def load_campaign(filename, campaign=None, limit=None):
with open("data/" + filename, "r") as f:
lines = f.read().split("\n")
print "found {} ORCID lines".format(len(lines))
print len(lines)
if limit:
lines = lines[:limit]
total_start = time()
row_num = 0
for line in lines:
row_num += 1
# can have # as comments
if line.startswith("#"):
print "skipping comment line"
continue
loop_start = time()
email = None
if "," in line:
(dirty_orcid, email, twitter) = line.split(",")
else:
dirty_orcid = line
try:
orcid_id = clean_orcid(dirty_orcid)
except NoOrcidException:
try:
print u"\n\nWARNING: no valid orcid_id in line {}; skipping\n\n".format(line)
except UnicodeDecodeError:
print u"\n\nWARNING: no valid orcid_id and line throws UnicodeDecodeError; skipping\n\n"
continue
my_person = Person.query.filter_by(orcid_id=orcid_id).first()
if my_person:
print u"row {}, already have person {}, skipping".format(row_num, orcid_id)
else:
print u"row {}, making person {}".format(row_num, orcid_id)
my_person = make_person(orcid_id, store_in_db=True)
my_person.campaign = campaign
my_person.email = email
my_person.twitter = twitter
db.session.merge(my_person)
commit_success = safe_commit(db)
if not commit_success:
print u"COMMIT fail on {}".format(my_person.orcid_id)
print "row {}: finished {} in {}s\n".format(row_num, orcid_id, elapsed(loop_start))
print "finished load_campaign on {} profiles in {}s\n".format(len(lines), elapsed(total_start))
def just_add_twitter(filename, limit=None, create=True):
with open("data/" + filename, "r") as f:
lines = f.read().split("\n")
print "found {} ORCID lines".format(len(lines))
if limit:
lines = lines[:limit]
total_start = time()
for line in lines:
loop_start = time()
email = None
twitter = None
if "," in line:
(dirty_orcid, email, twitter) = line.split(",")
else:
dirty_orcid = line
if twitter:
twitter = twitter.replace("@", "")
try:
orcid_id = clean_orcid(dirty_orcid)
except NoOrcidException:
try:
print u"\n\nWARNING: no valid orcid_id in line {}; skipping\n\n".format(line)
except UnicodeDecodeError:
print u"\n\nWARNING: no valid orcid_id and line throws UnicodeDecodeError; skipping\n\n"
continue
my_person = Person.query.filter_by(orcid_id=orcid_id).first()
if my_person:
my_person.twitter = twitter
db.session.merge(my_person)
commit_success = safe_commit(db)
if not commit_success:
print u"COMMIT fail on {}".format(orcid_id)
print u"added twitter {} to {}".format(twitter, orcid_id)
else:
print u"no person found with id {}".format(orcid_id)
print "loaded {} profiles in {}s\n".format(len(lines), elapsed(total_start))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Run stuff.")
# just for updating lots
parser.add_argument('file_name', type=str, help="filename to import")
parser.add_argument('campaign', type=str, help="name of campaign")
parser.add_argument('--limit', "-l", nargs="?", type=int, help="how many lines to import")
parser.add_argument('--just_add_twitter', nargs="?", type=bool, default=False, help="just add twitter")
parsed = parser.parse_args()
start = time()
if parsed.just_add_twitter:
just_add_twitter(parsed.file_name, limit=parsed.limit)
else:
load_campaign(parsed.file_name, campaign=parsed.campaign, limit=parsed.limit)
db.session.remove()
print "finished update in {}sec".format(elapsed(start))