Skip to content

Instantly share code, notes, and snippets.

@sharma-rohit
Created February 18, 2016 15:10
Show Gist options
  • Save sharma-rohit/0aac1d7373e25285d4a2 to your computer and use it in GitHub Desktop.
Save sharma-rohit/0aac1d7373e25285d4a2 to your computer and use it in GitHub Desktop.
Prefix IMDB rating on movie names, which are placed under movie directory.
from urllib import request
import os
import json
from distlib.compat import raw_input
class Rename:
def getMovieData(self, searchString):
url = "http://www.omdbapi.com/?t=" + searchString.replace(" ", "+") + "&r=json"
with request.urlopen(url) as f:
return f.read().decode("UTF-8")
def getAllMoviesNames(self, moviesDir):
return [m for m in os.listdir(moviesDir)]
def renameByRating(self, movieDir):
for movie in self.getAllMoviesNames(movieDir):
jsonData = self.getMovieData(movie)
movieInfo = json.loads(jsonData)
if movieInfo["Response"] == "True":
rating = movieInfo["imdbRating"]
os.rename(movieDir + movie, movie.replace(movie, movieDir + str(rating) + "_" + movie))
else:
print("Movie not found: {}".format(movie))
if __name__ == "__main__":
jr = Rename()
while True:
movieDir = raw_input("Enter your movies directory: ")
if os.path.exists(movieDir):
jr.renameByRating(movieDir)
break
else:
print("Entered input was not a movie directory!! \n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment