Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Author: Sayyad Shaha Hassan
- Date: 21 August 2016
- if (you no like verbos):
- hey there,
- this program fetches the ratings, plot, genres and other stuff of the movies on your disk,
- so you can decide which one to watch first and which ones to show to your little cousins.
- It also lists movies according to their genres so if you would like to watch all the sci-fies first you can check that list and enjoy.
- elif (you enjoy reading):
- With the rise of high capacity hard disks, we all love to keep as many movies and serials as we can.
- And this gives rise to a new problem. We soemtimes end up having so many unseen movies in our harddisk that we can't decide which movie to start with.
- I was gifted a hard disk by my beloved cowerkers and I immediately filled it up with everythng I could.
- The when it came to watching movies I was so overwhelmed to see so many movies that I could not decide which ones are worth watching and of those which are worth it where should I begin.
- Then I realised I could use imdb to fetch the genres, rating and plots of the movies and I immediately made an automation to do exactly that.
- Here I present to you a python script* that makes your work easier. You simply run it on a folder which contains your movies.
- Internet needed here. Not much data will be consumed. Even 2G net is enough, a few kb for each folder.
- You only need to keep one movie in one folder and the name of the folder should be renamed to the name of the movie. Now just run the script on the folder.
- Voila!!
- Each movie folder will now be renamed as "Movie name (ReleaseYear) [Rating**,imdbRating,RottenTomatoRating]"
- **Rating as in R, PG etc.
- Along with this, each folder will have a file that will contain the names of the actors, duration of the movie and the plot of the movie (from imdb).
- ----
- *script? But I thought this page is about C programmin you say?
- Well, yes. But I don't get as much time with my job and lazying and catching and training pokemons around at other times. Pokemons? o.O Ahh! It's so I can walk and stay healthy plus the pokemons :P.
- Also scripting is real cool. It helps you write so much in such less time. So well it's only good if you can write code in more than one language.
- '''
- #=================================================================
- #Python code begins:
- import os, sys, glob, re, json, urllib
- from collections import defaultdict
- longmsg = '''
- syntax:
- MovieRatings.py RootFolder [-u|-s]
- MovieRatings.py RootFolder -m CustomName
- -m manual override. In case you want to send the movie's name by yourself.
- For eg MovieRatings.py D:\mymovies\someName -m "Different movie name than the folder's"
- -u flag undoes all the stuff. (Deletes summary files and also ratings from folder names)
- -s summarize. Only makes the lists of movies by their genres.
- The movies should be kept as given below.
- One movie per folder.
- The parent folder of movie should contain
- >> [SNo] Movie Name [(Year)]
- SNo: In case of sequels/prequels this can be a digit or two
- Movie Name: Try to match it with movie title as much as possible. No spelling mistakes
- Year: Optional. Should be enclosed in round brackets.
- 3 Star Wars III
- 1 Madagascar (2005) This will work too.
- Madagascar (2005) This is the best
- Madagascar This is OK.
- movies
- movies\Madagascar series
- movies\Madagascar series\madagascar (2005)\file.mp4
- movies\Madagascar series\madagascar 2 \file.avi
- movies\Frozen\file.avi
- Output:
- it will rename the folders so they show Name (Year) [Rating, imdbRating, tomatoRating]
- and drop a summary file in the folder. which contains plot and other details of the movie.
- ProTips:
- Use full name only 'Part II' != 'Part 2'
- do not skip words in between. 'hangover part' is still ok, 'the hangover II' is not. (for searching 'the hangover part II')
- Author: Sayyad Shaha Hassan
- Date: 21/Aug/2016
- '''
- strFileMatch = "sh .*\(.+\) \[.*\]\.txt"
- reFileMatch = re.compile(strFileMatch)
- strYear = "(.*)(\()(\d{4})(\))(.*)"
- reYear = re.compile(strYear)
- dictReYearMatch = {"name":1, "bracket1":2, "year":3, "bracket2":4, "remName":5}
- strRating = "\[.{1,10},.{1,5},.{1,5}\]"
- def fSummarize(dpRoot):
- for root, dirs, files in os.walk(dpRoot):
- for file in files:
- if reFileMatch.search(file):
- with open(os.path.join(root,file)) as fIn:
- data = fIn.read()
- lines = data.split("\n")
- sTitle = lines[1]
- sGenres = lines[3][lines[3].find(":") + 1:]
- lGenres = sGenres.split(",")
- dctDic = defaultdict(list)
- for sGenre in lGenres:
- dctDic[sGenre].append(sTitle)
- tfpGenreList = os.path.join(dpRoot, sGenre) + ".txt"
- try:
- with open(tfpGenreList) as fIn:
- movieList = fIn.read()
- movieList = movieList.split("\n")
- if sTitle in movieList:
- continue
- except:
- pass
- with open(tfpGenreList, "a") as fOut:
- fOut.write(sTitle + "\n")
- return
- def fMakeFileNRenFolder(jsonResponse, root, PDir):
- if not jsonResponse:
- return None
- #formatting json response
- resTitle = fMakePathSafe(jsonResponse['Title'])
- resYear = fMakePathSafe(jsonResponse["Year"])
- resRated = fMakePathSafe(jsonResponse["Rated"])
- resimdbRate = fMakePathSafe(jsonResponse['imdbRating'])
- resTomatoUserRate = fMakePathSafe(jsonResponse['tomatoUserRating'])
- resGenre = fMakePathSafe(jsonResponse['Genre'])
- resActors = fMakePathSafe(jsonResponse['Actors'])
- resRuntime = fMakePathSafe(jsonResponse['Runtime'])
- 0
- tYear = ""
- if not reYear.search(PDir):
- tYear = ' (' + resYear + ')'
- dnNewPDirName = PDir + tYear + " [" + resRated + "," + resimdbRate + "," + resTomatoUserRate + "]"
- newRoot = root
- if root[2] != "\\":
- newRoot = root[:2] + "\\" + root[2:]
- fnSumFile = root + "\\sh " + fMakePathSafe(resTitle) + " (" + resYear + ") [" + resRated + "," + resimdbRate + "," + resTomatoUserRate + "].txt"
- with open(fnSumFile, "w") as fOut:
- fOut.write("\n" + resTitle + " (" + resYear + ")")
- fOut.write("\nRated: " + resRated)
- fOut.write("\nGenre: " + resGenre)
- fOut.write("\nActors: " + resActors)
- fOut.write("\nRuntime: " + resRuntime)
- fOut.write("\n\n Ratings: ")
- fOut.write("\nMetascore: " + jsonResponse['Metascore'])
- fOut.write("\nimdbRating: " + jsonResponse['imdbRating'])
- fOut.write("\ntomatoRating: " + jsonResponse['tomatoRating'])
- fOut.write("\ntomatoUserRating: " + jsonResponse['tomatoUserRating'])
- fOut.write("\n\nPlot Summary(Full) \n" + jsonResponse['Plot'].encode('ascii','ignore'))
- fOut.write("\n\nSayyad")
- try:
- os.system('ren "' + newRoot + '", "' + dnNewPDirName + '"')
- except:
- print "could not rename(" +newRoot + "," + dnNewPDirName + ')'
- pass
- return
- def fUnDoStuff(dpRoot):
- count = 0
- for root, dirs, files in os.walk(dpRoot):
- for file in files:
- if reFileMatch.search(file):
- os.remove(os.path.join(root, file))
- count += 1
- dpPDir = root
- dnPDir = root[root.rfind("\\") + 1:]
- locBrack = dnPDir.rfind("[")
- if locBrack == -1:
- continue
- dnNewPDir = dnPDir[:locBrack]
- try:
- os.system('ren "' + dpPDir + '", "' + dnNewPDir + '"')
- except:
- print "could not rename(" +dpPDir + ", " + dnNewPDir + '). Sorry! '
- pass
- print str(count) + " files deleted."
- return
- def fMakePathSafe(token):
- chars = '/\\:?"*|<>'
- token = token.encode('ascii','ignore')
- for char in chars:
- token = token.replace(char,'')
- return token
- def fTryUrl(sTitle, iYear):
- if sTitle == "":
- return None
- print sTitle +" :" + iYear
- urlStubYear = ""
- if iYear:
- urlStubYear = "&y="
- urlFull= "http://www.omdbapi.com/?t={}{}{}&type={}&plot={}&tomatoes={}&r={}".format(urllib.quote(sTitle), urlStubYear, iYear, "movie", "full", "true", "json")
- opener = urllib.FancyURLopener({})
- try:
- handle = opener.open(urlFull)
- except:
- return None
- jsonData = json.load(handle)
- if jsonData['Response'] == 'False':
- return None
- return jsonData
- def fGetResponse(PDir):
- match = reYear.search(PDir)
- iYear = ""
- sTitle = PDir
- if match:
- iYear = match.group(dictReYearMatch["year"])
- if int(iYear) < 1900 or int(iYear) > 2050:
- iYear = ""
- else:
- sTitle = match.group(dictReYearMatch["name"]) + match.group(dictReYearMatch["remName"])
- else:
- iYear = ""
- sTitle = sTitle.strip()
- sTitle = sTitle.replace(" ", "+")
- if sTitle == "":
- return None
- #in case of movie series where name is "2. Star Wars"
- firstWord = sTitle[:sTitle.find("+")]
- if not firstWord.isdigit():
- firstWord = sTitle[:sTitle.find(".")]
- if not firstWord.isdigit():
- firstWord = ""
- sAltTitle = ""
- if firstWord:
- sAltTitle = sTitle[len(firstWord)+1:]
- jsonData = fTryUrl(sTitle, iYear)
- if not jsonData:
- jsonData = fTryUrl(sAltTitle, iYear)
- if not jsonData:
- jsonData = fTryUrl(sTitle, "")
- if not jsonData:
- jsonData = fTryUrl(sAltTitle, "")
- if not jsonData:
- print "failed: not found"
- return jsonData
- def fHelp(minArgCount, maxArgCount):
- msgHelp = """
- invalid no of args. other blah blah
- """
- if not minArgCount:
- minArgCount = 1
- if not maxArgCount:
- maxArgCount = minArgCount
- count = len(sys.argv)
- if count < minArgCount or count > maxArgCount:
- print msgHelp
- print longmsg
- exit()
- return
- fHelp(2, 5)
- dnRoot = sys.argv[1]
- #undo
- if "-u" in sys.argv:
- fUnDoStuff(dnRoot)
- exit()
- elif "-m" in sys.argv:
- #title manual override
- # PathPDir -m manualTitle [manualYear]
- argC = 0
- for arg in sys.argv:
- if "-m" == arg:
- break
- argC += 1
- strManualTitle = sys.argv[argC + 1]
- strManualYear = ""
- try:
- strManualYear = sys.argv[argC + 2]
- except:
- pass
- jsonResponse = fTryUrl(strManualTitle, strManualYear)
- PDir = dnRoot[dnRoot.rfind("\\") + 1:]
- fMakeFileNRenFolder( jsonResponse, dnRoot, PDir)
- exit()
- elif "-s" in sys.argv:
- #summerize the movies
- fSummarize(dnRoot)
- exit()
- else:
- #normal Execution
- lastRoot = ""
- for root, dirs, files in os.walk(dnRoot):
- if not files:
- continue
- flag = 0
- for file in files:
- if file.startswith("sh "):
- flag = 1
- break
- if flag: continue
- tRoot = root
- if ":" in tRoot:
- si = 2
- if tRoot[2] == "\\":
- si += 1
- else:
- si = 0
- tRoot = tRoot[si:]
- PDir = tRoot[tRoot.rfind("\\")+1:]
- print "working on... ",
- print PDir
- jsonResponse = fGetResponse(PDir)
- if not jsonResponse: continue
- fMakeFileNRenFolder(jsonResponse, root, PDir)
- fSummarize(dnRoot)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement