Create a gist now

Instantly share code, notes, and snippets.

@yadomi /sanitize.py
Last active Aug 29, 2015

Utility to sanitize some movie filenames
#!/usr/bin/python3
# -*- coding: utf8 -*-
"""Utility to sanitize some warez movies filename"""
__author__ = "Felix Yadomi"
__version__ = "0.0.3"
__email__ = "dev.yadomi@gmail.com"
import sys, os, re
from titlecase import titlecase
def sanitize(filename):
title = []
warez = ['truefrench','fansub','bluray','720', '720p', 'x264','french', 'fr', 'divx','hdcam','xvid','appz','bdrip','board','cam','dvd','dvd-r','dvdrip','dupecheck','fake','fs','gamez','hddvd','hddvdrip','hdrip','hdtv','pdtv','internal','int','keygen','leecher','limited','nuke','proper','repack','retail','rip','rip','screener','serial','subforced','hardsub','stv','telecine','telesync','tvrip','unrated','vhsrip','vo','vost','vostfr','workprint','french','wp','subbed','unsubbed', 'r5', 'r6', 'md']
filename = re.sub('[\(\[].*?[\)\]]', '', os.path.splitext(filename)[0].strip().lower())
match = re.search('\d{4}', filename)
year = match.group(0) if match else False
filename = re.sub('[^\w]|[\_]', ' ', filename).split()
filename.remove(year)
for word in filename:
if word not in warez:
title.append(word)
if year:
title.append( '(' + year + ')' )
title = ' '.join(title)
return titlecase(title)
def usage():
print('usage: ' + sys.argv[0] + ' target [--dry]')
args = sys.argv[1:]
if ( len(args) > 2) or len(args) < 1:
usage()
sys.exit()
f = os.path.split(sys.argv[1])
if not f[0]:
filepath = '.'
else:
filepath = f[0]
filename = os.path.splitext(os.path.basename(sys.argv[1]))[0]
extension = os.path.splitext(sys.argv[1])[1]
newname = sanitize(filename)
print(newname + extension)
if '--dry' not in sys.argv:
os.rename(filepath + '/' + filename + extension, filepath + '/' + newname + extension)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment