-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpubmed_to_hugo.py
executable file
·52 lines (47 loc) · 1.73 KB
/
pubmed_to_hugo.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
#!/usr/bin/env python3
MD = """+++
authors = [{{authors}}]
title = "{{title}}"
journal = "{{journal}}"
what = "article"
doi = "{{doi}}"
pubmed = "{{pmid}}"
date = "{{date}}"
keywords = [{{keywords}}]
+++
{{abstract}}"""
if __name__ == "__main__":
import xml.etree.ElementTree as ET
from sys import argv
from jinja2 import Environment
from os.path import exists
from datetime import date as da
template = Environment().from_string(MD)
root = ET.parse(argv[1]).getroot()
for child in root:
article = {}
article["pmid"] = child.find(".//PMID").text
article["title"] = child.find(".//ArticleTitle").text
if child.find(".//AbstractText") is not None:
article["abstract"] = child.find(".//AbstractText").text
else:
article["abstract"] = ""
article["authors"] = ", ".join([
'"' + x.find("ForeName").text + " " + x.find("LastName").text + '"'
for x in child.findall(".//Author")
])
article["keywords"] = ", ".join([
'"' + x.text + '"' for x in child.findall(".//Keyword")
])
article["doi"] = child.find(
".//ArticleId[@IdType='doi']").text
article["journal"] = child.find(".//Journal/Title").text
date = child.find(".//PubMedPubDate[@PubStatus='pubmed']")
article["date"] = da(int(date.find("Year").text),
int(date.find("Month").text),
int(date.find("Day").text)).isoformat()
print(article["keywords"])
filename = "content/pubs/PM{}.md".format(article["pmid"])
if not exists(filename):
with open(filename, "w") as f:
f.write(template.render(**article))