160
177
if source.props.active:
161
178
active_filters.append(source.props.id)
165
if "ie" in active_filters:
166
url = "http://www.medicines.ie/searchresults.aspx?term=%s&searchtype=AdvancedSearch" % search
167
self.append_results(url, model)
169
if "uk" in active_filters:
170
url = "http://www.medicines.org.uk/EMC/searchresults.aspx?term=%s&searchtype=QuickSearch" % search
171
self.append_results(url, model)
180
if self.preferences.props.remote_content_search != Unity.PreferencesManagerRemoteContent.ALL:
181
icon_hint = Gio.ThemedIcon.new("error").to_string()
182
model.append("open-privacy-settings",
186
("Online search results disabled"),
187
("Click here to change options"),
188
"open-privacy-settings")
191
if "ie" in active_filters:
192
url = "http://www.medicines.ie/searchresults.aspx?term=%s&searchtype=AdvancedSearch" % search
193
self.append_results(url, model)
195
if "uk" in active_filters:
196
url = "http://www.medicines.org.uk/EMC/searchresults.aspx?term=%s&searchtype=QuickSearch" % search
197
self.append_results(url, model)
173
199
def append_results(self, url, model):
174
200
""" Parses the html of the page of the url and adds results to
188
214
itemtype = item.xpath("td/table/tr/td/img/@alt")[0]
189
215
uri = item.xpath("td/table/tr/td/a/@href")[0]
190
216
company = item.xpath("td/a")[0]
192
218
if itemtype == "PIL":
194
220
icon_hint = Gio.ThemedIcon.new("application-pdf").to_string()
195
221
model.append(str(uri), icon_hint, group, "text/html", title.text_content().strip(), drug.text_content().strip() + "\n" + company.text_content().strip(), str(uri))
223
def activate_uri(self, scope, uri):
224
if re.search("PIL", uri):
225
if uri.startswith("http://www.medicines.ie"):
227
end = re.search("PIL", number)
228
number = (number[:end.start() - 1])
229
url = "http://www.medicines.ie/pdfviewer.aspx?isAttachment=true&documentid=%s" % number
232
if re.search("XPIL", number):
233
end = re.search("XPIL", number)
235
end = re.search("PIL", number)
236
number = (number[:end.start() - 1])
237
url = "http://www.medicines.org.uk/EMC/pdfviewer.aspx?isAttachment=true&documentid=%s" % number
239
command.append("evince")
241
subprocess.Popen(command)
242
return Unity.ActivationResponse(handled=Unity.HandledType.HIDE_DASH, goto_uri='')
244
return Unity.ActivationResponse(handled=Unity.HandledType.NOT_HANDLED)
246
def preview_uri(self, scope, uri):
247
"""Preview request handler"""
249
model = scope.props.results_model
250
iteration = model.get_first_iter()
251
end_iter = model.get_last_iter()
253
while iteration != end_iter:
254
if model.get_value(iteration, 0) == uri:
255
title = model.get_value(iteration, 4)
256
description = model.get_value(iteration, 5).title() + "\n"
258
if model.get_value(iteration, 2) == 1:
259
document_type = "PIL"
260
url = "http://www.medicines.ie/pdfviewer.aspx?isAttachment=true&documentid=7703"
262
document_type = "SPC"
264
# Get the appropriate wikipedia page for the drug in the product
265
word = description.split()[0]
267
word = word[0:len(word) - 1]
269
wikipage = "http://en.wikipedia.org/wiki/%s" % word.title()
270
tree = lxml.html.parse(wikipage)
271
tree.getroot().make_links_absolute()
273
# Get details fro the infobox on the wiki page for the preview description
274
link = tree.xpath("//table[@class='infobox']")[0]
275
infobox = link.text_content()
276
infobox = infobox.replace("\n", "\t")
279
match1 = re.search("Bioavailability", infobox)
280
match2 = re.search("Metabolism", infobox)
281
description = description + "\n<b>Bioavailability:</b>\t" + infobox[match1.end():match2.start()]
283
print("Bioavailability unavailable")
286
match1 = re.search("Metabolism", infobox)
287
match2 = re.search("Half-life", infobox)
288
description = description + "\n<b>Metabolism:</b>\t" + infobox[match1.end():match2.start()]
290
print("Metabolism unavailable")
293
match1 = re.search("Half-life", infobox)
294
match2 = re.search("Excretion", infobox)
295
description = description + "\n<b>Half-life:</b>\t\t" + infobox[match1.end():match2.start()]
297
print("Half Life unavailable")
300
match1 = re.search("Excretion", infobox)
301
match2 = re.search("Identifiers", infobox)
302
description = description + "\n<b>Excretion:</b>\t\t" + infobox[match1.end():match2.start()]
304
print("Excretion unavailable")
307
match1 = re.search("Formula", infobox)
308
match2 = re.search("Mol", infobox)
309
description = description + "\n<b>Formula:</b>\t\t" + infobox[match1.end():match2.start()]
311
print("Formula unavailable")
314
match1 = re.search("Mol", infobox)
315
match2 = re.search("g/mol", infobox)
316
description = description + "\n<b>Molecular Mass:</b>" + infobox[match1.end() + 6:match1.end()+ 14] + " g/mol"
318
print("Molecular Mass unavailable")
321
match1 = re.search("ATC code", infobox)
322
match2 = re.search("PubChem", infobox)
323
description = description + "\n<b>ATC Code:</b>\t\t" + infobox[match1.end():match2.start()]
325
print("ATC Code unavailable")
328
match1 = re.search("Pregnancy cat.", infobox)
329
match2 = re.search("Legal status", infobox)
330
description = description + "\n<b>Pregnancy Category:</b>" + infobox[match1.end():match2.start()]
332
print("Pregnancy Category unavailable")
335
match1 = re.search(" name", infobox)
336
match2 = re.search("Clinical data", infobox)
337
description = description + "\n\n<b>Systematic (IUPAC) name:</b>\n" + infobox[match1.end():match2.start()].replace("\t", "")
339
print("Systematic (IUPAC) name unavailable")
341
description += "\n\n<small>Chemical entity information from Wikipedia</small>"
343
if not os.path.exists('/tmp/unity-lens-medicines/%s.png' % word.title()):
344
# Get the preview image from Wikipedia
345
link = tree.xpath("//a[@class='image']/img/@src")[0]
347
# Change the image size to something better for previews
348
match = re.search(r"px-", link)
350
size = link[match.start()-3:match.start()]
351
if not re.search("png/", link):
352
image = link.replace(size, "500")
354
myopener = MyOpener()
355
myopener.retrieve(image, "/tmp/unity-lens-medicines/image.png")
358
img = Image.open('/tmp/unity-lens-medicines/image.png','r')
360
background = Image.new('RGBA', (img_w,img_h), (255, 255, 255, 255))
361
bg_w,bg_h=background.size
362
offset=((bg_w-img_w)/2,(bg_h-img_h)/2)
363
background.paste(img,offset, img)
364
background.save('/tmp/unity-lens-medicines/%s.png' % word.title())
366
myopener.retrieve(image, '/tmp/unity-lens-medicines/%s.png' % word.title())
367
print("No image transformation performed")
369
# Assemble the preview
370
self.preview = Unity.GenericPreview.new(title, description, None)
371
self.preview.props.image_source_uri = 'file:///tmp/unity-lens-medicines/%s.png' % word.title()
373
# Add the "View" action
374
view_action = Unity.PreviewAction.new("activate_uri", "View %s" % (document_type), None)
375
view_action.connect("activated", self.activate_uri)
376
self.preview.add_action(view_action)
379
iteration = model.next(iteration)
380
if self.preview is None:
381
print ("Couldn't find model row for requested preview uri: '%s'", uri)
197
384
if __name__ == "__main__":
198
385
session_bus_connection = Gio.bus_get_sync(Gio.BusType.SESSION, None)
199
386
session_bus = Gio.DBusProxy.new_sync(session_bus_connection, 0, None,