]> code.communitydata.science - social-media-chapter.git/blob - code/data_collection/01_get_abstracts.py
initial import of material for public archive into git
[social-media-chapter.git] / code / data_collection / 01_get_abstracts.py
1 from request_functions import *
2 import argparse
3 import json
4 import subprocess
5
6
7 def main():
8
9     parser = argparse.ArgumentParser(description='Output JSON of abstracts and bibliography of all articles passed in.')
10     parser.add_argument('-i', help='JSON file which includes eids')
11     parser.add_argument('--eid', '-e', help='Single eid')
12     parser.add_argument('-o', help='Where to append JSON results')
13     args = parser.parse_args()
14
15     if args.eid:
16         eids = [args.eid]
17     elif args.i:
18         with open(args.i, 'r') as f:
19             eids = [json.loads(line)['eid'] for line in f]
20     else:
21         print('Need to either pass in an eid or a json file with eids')
22
23     # If the script gets interrupted, we need to start where we left off
24     try:
25         errors = []
26         with open(args.o, 'r') as f:
27             completed_eids = []
28             for line in f:
29                 try:
30                     result = json.loads(line)
31                     completed_eids.append(result['abstracts-retrieval-response']['coredata']['eid'])
32                 except ValueError:
33                     errors.append(line)
34     except IOError as e:
35         completed_eids = []
36
37
38     print('{} completed eids'.format(len(completed_eids)))
39     with open(args.o, 'a') as out_file:
40             for eid in eids:
41                 if eid not in completed_eids:
42                     result = get_abstract(eid)
43                     if result:
44                         out_file.write(result)
45                         out_file.write('\n')
46                     else:
47                         errors.append(eid)
48
49     if len(errors) > 0:
50         with open('raw_data/missing_eids.json', 'a') as l:
51             # Add the bad lines from the output file
52             (l.write(e) for e in errors)
53
54
55 if __name__ == '__main__':
56     main()

Community Data Science Collective || Want to submit a patch?