-
-
Save cruxrebels/c6267db8b63c0950dc18 to your computer and use it in GitHub Desktop.
Clean Movie Folder Names and Append IMDB Rating
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import urllib2 | |
from BeautifulSoup import BeautifulSoup | |
import re | |
def imdb(movie_name): | |
http_proxy_full_auth_string = "http://%s:%s@%s:%s" % ('username','password','proxy','port') | |
proxy_handler = urllib2.ProxyHandler({"http": http_proxy_full_auth_string}) | |
opener = urllib2.build_opener(proxy_handler) | |
urllib2.install_opener(opener) | |
movie_name= movie_name.split() | |
movie_url='+'.join(movie_name) | |
url_pref = "http://www.imdb.com/find?q=" | |
url_suf = "&s=all" | |
imdb_url = url_pref+movie_url+url_suf; | |
print imdb_url | |
source = urllib2.urlopen(imdb_url).read() | |
title = re.search(r'/title/tt[0-9]*/', source) | |
movie_url = "http://imdb.com"+title.group() | |
movie_source = urllib2.urlopen(movie_url).read() | |
print "Movie Page Fetched" | |
soup = BeautifulSoup(movie_source) | |
rating = soup.findAll("span", itemprop="ratingValue") | |
for node in rating: | |
return (''.join(node.findAll(text=True))) | |
filenames = os.listdir("/media/abhishek/iStorage/Blockbusters/Hollywood") | |
print filenames | |
for i in filenames: | |
new_name = i | |
(filepath,filename)= os.path.split(i) | |
loc=filename.find('[') | |
#print "loc_1"+str(loc) | |
if(loc!=-1): | |
new_name = filename[:(loc)] | |
print new_name | |
loc=new_name.find('(') | |
#print "loc_2"+str(loc) | |
if(loc!=-1): | |
new_name=new_name[:loc] | |
print new_name | |
new_name= new_name.replace('.',' ') | |
name = new_name.split() | |
final_name ='' | |
for i in name: | |
i=i.capitalize() | |
final_name=final_name+i+" " | |
print final_name | |
rating = imdb(final_name) | |
os.rename("/media/abhishek/iStorage/Blockbusters/Hollywood/"+filename,"/media/abhishek/iStorage/Blockbusters/Hollywood/"+final_name+"[IMDB - "+rating+"]") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment