Skip to content

Instantly share code, notes, and snippets.

@alexras
Created June 22, 2012 22:02
Show Gist options
  • Select an option

  • Save alexras/2975455 to your computer and use it in GitHub Desktop.

Select an option

Save alexras/2975455 to your computer and use it in GitHub Desktop.
Script that I used to import my Mendeley library into paper-pile
#!/usr/bin/env python
import yaml, os, sys, subprocess
venue_strings = ["booktitle", "journal", "institution"]
with open(sys.argv[1], 'r') as fp:
collection_yaml = yaml.load(fp.read())
transformed_entries = {}
for entry_name in collection_yaml["entries"]:
if os.path.exists(os.path.expanduser("~/Desktop/imported_papers/%s" % (entry_name))):
print "Skipping '%s'..." % (entry_name)
continue
entry = collection_yaml["entries"][entry_name]
if "year" in entry:
year = int(entry["year"])
else:
sys.exit("Can't find year for %s" % (entry_name))
venue = None
for venue_str in venue_strings:
if venue_str in entry:
venue = entry[venue_str]
break
if "type" in entry and entry["type"] == "techreport":
for key in ["institution", "publisher"]:
if key in entry:
venue = "%s Tech Report" % (entry[key])
if venue == None:
sys.exit("Can't find venue for %s" % (entry_name))
authors = []
if "author" in entry:
for author in entry["author"]:
if "first" not in author:
sys.exit("An author in %s has no first name" % (entry_name))
if "middle" in author:
author_name = "%(first)s %(middle)s %(last)s" % author
else:
author_name = "%(first)s %(last)s" % author
authors.append(author_name)
if "title" not in entry:
sys.exit("Can't find title for %s" % (entry_name))
title = entry["title"].strip("{}")
if "file" not in entry:
sys.exit("Can't find file for %s" % (entry_name))
filename = "/" + entry["file"].split(":")[1]
date_read = None
transformed_entries[entry_name] = {
"title" : title,
"authors" : authors,
"venue" : venue,
"year" : year,
"date-read" : None}
print "Importing %s (%s) ..." % (entry_name, filename)
cmd = '~/src/paper-pile.git/paper-pile add -n "%s" "%s"' % (entry_name, filename)
cmd_obj = subprocess.Popen(cmd, shell=True)
cmd_obj.communicate()
if cmd_obj.returncode != 0:
sys.exit("Add failed")
with open(os.path.expanduser("~/Desktop/imported_papers/%s/metadata.yaml" % (entry_name)), "w") as fp:
fp.write(yaml.dump(transformed_entries[entry_name]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment