Skip to content

Instantly share code, notes, and snippets.

@retorquere
Created May 6, 2021 12:59
Show Gist options
  • Save retorquere/676d1d7536d3aa1ac48a9582fb9a40f8 to your computer and use it in GitHub Desktop.
Save retorquere/676d1d7536d3aa1ac48a9582fb9a40f8 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# run as ris.py importdir1 importdir2 ...
import glob, sys, os, re
for importdir in sys.argv[1:]:
importdir = os.path.abspath(os.path.expanduser(importdir))
ris = ''
for pdf in glob.glob(os.path.join(importdir, '*.pdf')):
m = re.match(r'^(.+?)@(.+?)\$(.+?).pdf$', os.path.basename(pdf))
if m:
title, author, year = [g.strip() for g in m.groups()]
ris += 'TY - JOUR\n'
ris += 'TI - ' + title + '\n'
ris += 'AU - ' + author + '\n'
ris += 'DA - ' + year + '///\n'
ris += 'PY - ' + year + '\n'
ris += 'L1 - ' + pdf + '\n'
ris += 'ER -\n'
ris += '\n'
if ris != '':
with open(os.path.join(importdir, os.path.basename(importdir) + '.ris'), 'w') as f:
f.write(ris)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment