Skip to content

Instantly share code, notes, and snippets.

@morganhein
Created February 28, 2014 23:25
Show Gist options
  • Save morganhein/9282120 to your computer and use it in GitHub Desktop.
Save morganhein/9282120 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
#usage: renamer.py /directory/to/files
import sys
from os import walk, path
from subprocess import call
args = sys.argv
'''
Get all filenames.
Strip all the random shit from the name & send the result with filename to the bash script
'''
f = []
for (dirpath, dirnames, filenames) in walk(args[1]):
f.extend(filenames)
break
files = {}
def process(input):
replace = ["720p", "1080p", "r5", "xvid", "ac3", "dd5 1", "5 1", "hq", "bluray", "x264", "480p", "brrip"\
"web-dl", "h264"]
file = ""
#convert to lowercase
for char in input:
file += char.lower()
#strip the file extension
file = path.splitext(file)[0]
try:
#strip anything after, and including the last dash i.e. "-FlaWlESS" the group name
file = file[0 : len(file) - 1 - file[::-1].index("-")]
except ValueError:
pass
#now replace all dots, dashes with spaces
file = file.replace(".", " ").replace("-", " ").replace("_", " ")
#remove all the junk
for rep in replace:
file = file.replace(rep, "")
return file.strip()
#process files
for file in f:
if file != "renamer.py" and file != "search.sh":
files[file] = process(file)
#send them to be search and organized
for key, value in files.items():
print "New Title: ", value, key
call(["search.sh", value, key], cwd=args[1])
print " "
#!/bin/sh
# Usage: script.sh moviename
#
wgeturl="`date`.html"
search="`echo $1 |sed 's/ /+/g'`"
file=$2
echo "Searching for: $search"
wget "http://www.imdb.com/find?s=all&q=$search" -qO "$wgeturl"
tt="`cat "$wgeturl" |grep -m1 -oE "tt[0-9]{7}" |grep -m1 -E "(.*)"`"
rm "$wgeturl"
#
wget "http://www.imdb.com/title/$tt/" -qO "$tt.html"
title="`cat $tt.html |pcregrep -oMi "<title>(.*)</title>" |sed 's/<title>//g' |sed 's/<\/title>//g' | tr -d '\n' |sed 's/ - IMDb//' |sed 's/\ / /'`"
rm "$tt.html"
#
#if [[ "$tt" != "" ]]; then echo "http://www.imdb.com/title/$tt" ;fi
#if [[ "$title" != "" ]]; then echo "Title: $title" ;fi
#
echo "Title found, moving to folder: $title.cp($tt)"
mkdir "$title.cp($tt)"
mv -v "$2" "$title.cp($tt)/$2"
#echo "$2 \"$title.cp($tt)/\""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment