Skip to content

Instantly share code, notes, and snippets.

@davidaurelio
Created September 1, 2010 08:29
Show Gist options
  • Save davidaurelio/560410 to your computer and use it in GitHub Desktop.
Save davidaurelio/560410 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""
Fixes file names for downloads from jil.org.
jil.org delivers downloaded files with file names in e-mail header format. The
fix_filename function from this module can decode such file names.
When this module is executed as main program, all file names passed as program
arguments are renamed properly:
$ jilorg_fix_filenames.py =?UTF-8?B?bXl3aWRnZXQud2d0?= path/to/=?UTF-8?B?bXl3aWRnZXQud2d0?=
Moves "=?UTF-8?B?bXl3aWRnZXQud2d0?=" to "mywidget.wgt" and
"path/to/=?UTF-8?B?bXl3aWRnZXQud2d0?=" to "path/to/mywidget.wgt"
The easiest way is to use shell wildcards:
$ jilorg_fix_filenames.py path/to/downloads/=*
"""
from os.path import basename, dirname, join
from email.header import decode_header
def fix_filename(file_path):
"""
Fixes filenames that are encoded as e-mail header.
Also removes suffixes added by download software.
>>> fix_filename("=?UTF-8?B?bXl3aWRnZXQud2d0?=")
u'mywidget.wgt'
>>> fix_filename("path/to/=?UTF-8?B?bXl3aWRnZXQud2d0?=")
u'path/to/mywidget.wgt'
>>> fix_filename("=?UTF-8?B?bXl3aWRnZXQud2d0?= (27)")
u'mywidget.wgt'
"""
cleaned = file_path[:file_path.rindex("=")+1]
file_name = basename(cleaned)
decoded = decode_header(file_name)[0]
file_name = decoded[0].decode(decoded[1])
dir_name = dirname(file_path)
return join(dir_name, file_name)
if __name__ == "__main__":
from sys import argv
from shutil import move
for path in argv[1:]:
move(path, fix_filename(path))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment