-
Notifications
You must be signed in to change notification settings - Fork 0
/
pubmed.py
105 lines (89 loc) · 2.93 KB
/
pubmed.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
import urllib2
import urlparse
import json
import random
search_url_base = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi"
pubmed_url_base = "https://www.ncbi.nlm.nih.gov/pubmed/"
summary_url_base = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi"
username = "Search Result"
emojis = [
":hankey:",
":+1:",
":blue_heart:",
":sunny:",
":cat:",
":hamster:",
":rabbit:",
":dog:",
":mouse:",
":wolf:",
":musical_notes:",
":pig:",
":tulip:",
":maple_leaf:",
":sunflower:",
":mushroom:",
":octocat:",
":pizza:",
":cherries:",
":checkered_flag:",
":izakaya_lantern:",
":smiling_imp:",
":metal:",
":heart_eyes_cat:",
]
class Record:
def __init__(self,id):
self.id = str(id)
self.result = ""
def get(self):
if not self.result:
url = summary_url_base.rstrip("/") + "?db=pubmed&id=" + urllib2.quote(self.id) + "&retmode=json"
request = urllib2.urlopen(url)
result_json = request.read()
self.result = json.loads(result_json)["result"][self.id]
return self.result
def title(self):
self.get()
return self.result["title"]
def source(self):
self.get()
return self.result["source"]
def to_url(self):
return urlparse.urljoin(pubmed_url_base,self.id)
def author_string(self):
self.get()
return self.result["sortfirstauthor"] + "..." + self.result["lastauthor"]
def slack_payload(self):
self.get()
if self.title() and self.source():
return json.dumps({ "attachments": [{"title": self.title(),
"title_link": self.to_url(),
"text": self.author_string(),
"fields": [{"title": self.source()}]
}],
"icon_emoji": random.choice(emojis),
"username": username
})
class SearchResult:
def __init__(self, term):
self.term = term
self.result = ""
def search(self):
search_url = search_url_base.rstrip("/") + "?db=pubmed&term=" + urllib2.quote(self.term) + "&retmode=json"
request = urllib2.urlopen(search_url)
result_json = request.read()
self.result = json.loads(result_json)
return self.result
def ids_from_result(self):
if not self.result:
self.search()
id_list = self.result['esearchresult']['idlist']
return id_list
def to_url(self,id):
return urlparse.urljoin(pubmed_url_base,id)
def urls_from_result(self):
id_list = self.ids_from_result()
return [pubmed_url_base + a for a in id_list]
def records(self):
return [Record(elem) for elem in self.ids_from_result()]