Skip to content

Instantly share code, notes, and snippets.

@goerz
Created February 19, 2016 21:21
Show Gist options
  • Save goerz/6d0536a8c187236f0bbd to your computer and use it in GitHub Desktop.
Save goerz/6d0536a8c187236f0bbd to your computer and use it in GitHub Desktop.
Rename Amtrak Tickets
#!/usr/bin/env python
"""command line script that renames a bunch of Amtrak ticket PDF to a more
useful name. Relies on `pdftotext`."""
import os
import re
import logging
from collections import namedtuple
from dateutil.parser import parse as parse_date
from glob import glob
import click
from click import echo
from sh import pdftotext
def parse_ticket(filename):
data = {}
lines = list(pdftotext(filename, '-', _iter=True))
data['from'] = lines[5].strip()
data['to'] = lines[7].strip()
data['date'] = parse_date(lines[15].strip())
if "round-trip" in lines[9].lower():
for line in lines:
# the last line containing a date is the return date
if re.match(r'^\w{3} \d+, \d{4}$', line):
data['date_return'] = parse_date(line.strip())
return data
def rename_amtrack_ticket(ticket_file, dry_run=False):
data = parse_ticket(ticket_file)
date = data['date']
if 'date_return' in data:
date2 = data['date_return']
new_filename \
= "Amtrak_%04d-%02d-%02d_%s_%s_%04d-%02d-%02d.pdf" % (
date.year, date.month, date.day,
data['from'], data['to'],
date2.year, date2.month, date2.day)
else:
new_filename \
= "Amtrak_%04d-%02d-%02d_%s_%s.pdf" % (
date.year, date.month, date.day,
data['from'], data['to'])
echo("%s -> %s" % (ticket_file, new_filename))
if not dry_run:
os.rename(ticket_file, new_filename)
@click.command()
@click.help_option('--help', '-h')
@click.option('--debug', is_flag=True,
help='enable debug logging')
@click.option('--dry-run', '-n', is_flag=True,
help='Skip rename')
@click.argument('files', nargs=-1)
def main(debug, dry_run, files):
"""Rename Amtrak tickets"""
logging.basicConfig(level=logging.WARNING)
logger = logging.getLogger(__name__)
if debug:
logger.setLevel(logging.DEBUG)
logger.debug("Enabled debug output")
for filename in files:
rename_amtrack_ticket(filename, dry_run=dry_run)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment