Skip to content

Instantly share code, notes, and snippets.

@sdouglas
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdouglas/eee6b8ded333bf99d8b8 to your computer and use it in GitHub Desktop.
Save sdouglas/eee6b8ded333bf99d8b8 to your computer and use it in GitHub Desktop.
find PMID in txt file, fetch XML from pubmed, parse into jekyll post, write markdown file to ~/Desktop
#!/usr/bin/env python
# encoding: utf-8
"""
ifttt_rss2md.py
Created on 2014-07-14.
The MIT License (MIT)
Copyright (c) 2014. Shawn Douglas
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
DESCRIPTION:
1. Extracts a PMID from an IFTTT-generated txt file.
2. Fetches XML for PMID and parses into a jekyll blog post.
3. Writes the blog post as markdown to ~/Desktop/
4. relocates the IFTTT file to a "Processed" subdirectory.
DEPENDENCIES:
Install https://github.com/martinblech/xmltodict
$ pip install xmltodict
INSTRUCTIONS:
1. Find an RSS feed url that you want to track.
2. Create IFTTT rule like https://ifttt.com/recipes/191206-pubmed-rss-to-dropbox-txt
Set to place new feed items in "~/Dropbox/IFTTT/Feeds/myfeed"
I created the following directory structure in Dropbox:
IFTTT/
Feeds/
myfeed/
Processed/
myfeed/
scripts/
ifttt_rs2md.py (this file)
3. Create an Automator "Folder Action" workflow that receives files
and folders added to "~/Dropbox/IFTTT/Feeds/myfeed" and "Runs Shell Script" with
Shell: /bin/bash
Pass input: as arguments
for f in "$@"
do
python ~/Dropbox/IFTTT/scripts/ifttt_rss2md.py "$f"
done
"""
import re
import sys
import urllib2
import xmltodict
import json
import shutil
from os.path import expanduser
from xattr import xattr
from struct import unpack
pmid_re = "PMID: ([0-9]+)"
post_template = """---
layout: post
published: true
title: "%s"
date: %s 12:00:00
pmid: %s
authors: "%s"
firstauthor: "%s"
journalname: "%s"
journalvolume: %s
journalissue: %s
journalpages: %s
---
%s
"""
def main(argv=None):
if argv is None:
argv = sys.argv
pmidString = None
with open(argv[1], 'r') as f:
read_data = f.read()
pmidObj = re.search(pmid_re, read_data)
if pmidObj:
pmidString = pmidObj.group(1)
if pmidString:
pubmid_url = "http://www.ncbi.nlm.nih.gov/pubmed/%s?report=xml&format=text" % pmidString
response = urllib2.urlopen(pubmid_url)
html = response.read()
html = html.replace("&lt;", "<").replace("&gt;", ">")
d = xmltodict.parse(html)
article = d["pre"]["PubmedArticle"]
citation = article["MedlineCitation"]
pmid = citation["PMID"]["#text"]
date = citation["DateCreated"]
dateStr = "%s-%s-%s" % (date["Year"], date["Month"], date["Day"])
journal = citation["Article"]["Journal"]
journalname = journal["ISOAbbreviation"]
journalvolume = ""
journalissue = ""
journalpages = ""
pubdate = journal["JournalIssue"]["PubDate"]
if "Month" in pubdate and "Day" in pubdate:
pubdateStr = "%s %s %s" % (pubdate["Year"], pubdate["Month"], pubdate["Day"])
else:
pubdateStr = pubdate["Year"]
ji = journal["JournalIssue"]
vipStr = ""
if "Volume" in ji and "Issue" in ji:
vipStr = "<b>%s</b>(%s)" % (ji["Volume"], ji["Issue"])
journalvolume = ji["Volume"]
journalissue = ji["Issue"]
if "Pagination" in citation["Article"]:
vipStr += ":%s" % citation["Article"]["Pagination"]["MedlinePgn"]
journalpages = citation["Article"]["Pagination"]["MedlinePgn"]
citeStr = "*%s %s;%s*" % (journalname, pubdateStr, vipStr)
else:
citeStr = "*%s %s*" % (journalname, pubdateStr)
title = citation["Article"]["ArticleTitle"].replace('"', "'")
# print title
abstract = citation["Article"]["Abstract"]["AbstractText"]
authorCount = len(citation["Article"]["AuthorList"]["Author"])
authors = citation["Article"]["AuthorList"]["Author"]
if type(authors) is dict:
authors = [authors]
authorList = []
for author in authors:
authorList.append("%s %s" % (author["LastName"], author["Initials"]))
authorStr = ", ".join(authorList)
firstAuthorLN = authors[0]["LastName"]
# Write the markdown file in ~/Desktop/
mdFilename = "%s-%s.md" % (dateStr, firstAuthorLN.lower())
mdContent = post_template % (title, dateStr, pmid, authorStr, authorList[0], journalname, journalvolume, journalissue, journalpages, abstract)
mdContent = mdContent.encode("utf-8")
# print mdFilename
# print mdContent
home = expanduser("~")
f = open(home+"/Desktop/"+mdFilename, 'w')
f.write(mdContent)
f.close()
# label the new file red for increased visiblity
# from https://github.com/danthedeckie/finder_colors
color = 'green'
COLORS = {'none': 0, 'gray': 2, 'green': 4, 'purple': 6,
'blue': 8, 'yellow': 10, 'red': 12, 'orange': 14}
_FINDER_INFO_TAG = u'com.apple.FinderInfo'
BLANK = 32*chr(0)
attrs = xattr(home+"/Desktop/"+mdFilename)
if _FINDER_INFO_TAG in attrs:
previous = attrs[_FINDER_INFO_TAG]
else:
previous = BLANK
prev_color_extra_bits = ord(previous[9]) & (255-14)
# these are all the bits in previous[9] not used for color.
new = previous[:9] \
+ chr(COLORS[color] \
+ prev_color_extra_bits) \
+ previous[10:]
attrs.set(_FINDER_INFO_TAG, new)
# Clean up - move the file to "Processed directory"
# something like ['','Users', 'shawn', 'Dropbox', 'IFTTT', 'Feeds', 'cmp', 'file..txt']
src = argv[1].split("/")
# something like ['','Users', 'shawn', 'Dropbox', 'IFTTT', 'Processed', 'cmp', 'file..txt']
dest = "/".join(src[:-3] + ["Processed"] + src[-2:])
shutil.move(argv[1],dest)
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment