forked from nhshackday/lict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.py
55 lines (44 loc) · 1.68 KB
/
import.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
from neo4jrestclient.client import GraphDatabase
from pyquery import PyQuery as pq
from lict_common import pubmed_ids
import logging
NEO4J_URL="http://localhost:7474/db/data/"
DESIRED_ARTICLE_TYPE="research-article"
gdb = GraphDatabase(NEO4J_URL)
one_index_to_rule_them_all = gdb.nodes.indexes.create("one_index_to_rule_them_all")
def import_stuff(file_list):
for filename in file_list:
article_doc = pq(filename=filename)
try:
article_tag = article_doc('article')
article_type = article_tag.attr('article-type')
if article_type != DESIRED_ARTICLE_TYPE:
logging.debug("Skipping article of type %s" % article_type)
continue
(pmid, pmc) = pubmed_ids(article_doc)
logging.info("Processing article %d / %d" % (pmc, pmid))
save_article_to_neo4j(article_doc)
except Exception as e:
# When in doubt, wrap it all in a massive try/except for bonus insanity!
logging.error(e)
def save_article_to_neo4j(article_doc):
# Which key? PMC
(pmid, pmc) = pubmed_ids(article_doc)
pmc_str = str(pmc)
pmid_str = str(pmid)
try:
node = one_index_to_rule_them_all.get('pmc', pmc_str)[0]
except IndexError:
node = gdb.node()
node['pmc'] = pmc
node['pmid'] = pmid
node['title'] = article_doc('article-title').text()
one_index_to_rule_them_all.add('pmc', pmc_str, node)
one_index_to_rule_them_all.add('pmid', pmid_str, node)
if __name__ == "__main__":
import sys
import glob
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
file_list = sys.argv[1:]
import_stuff(file_list)