Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged new_feature with master #11

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,23 @@
A simple script for fetching movie details right from the terminal

# Requirements
1. Requests
2. BeautifulSoup4
3. Colorama
1. sys
2. requests
3. bs4
4. time
5. colorama
6. urllib
7. os
8. demjson

Note:- You can install them using sudo pip install name-of-the-package

# Usage
![Screenshot](https://user-images.githubusercontent.com/29385192/36164961-073b7642-1114-11e8-93b3-6718b54bc00b.PNG)
1.python movie-py.py "movie name". </br>
Note:- Replace movie name with the movie name you want to search.</br>
![Screenshot](https://raw.githubusercontent.com/mmkumr/pictures/master/Screenshot%20from%202019-01-13%2023-56-32.png?token=AQpd0Y4sMiaii8XsTO6mXzOgDy_SYh63ks5cRL7NwA%3D%3D)
2.Many results will display enter your desired serial no.</br>
3.Then details will be printed on the screen.</br>
![Screenshot](https://raw.githubusercontent.com/mmkumr/pictures/master/Screenshot%20from%202019-01-14%2000-06-58.png?token=AQpd0dYEhscNie4SWB9xtEii-PGHq4sWks5cRMAwwA%3D%3D)
=======
![Screenshot](https://raw.githubusercontent.com/mmkumr/pictures/master/Screenshot%20from%202019-01-13%2023-56-32.png?token=AQpd0Y4sMiaii8XsTO6mXzOgDy_SYh63ks5cRL7NwA%3D%3D)
108 changes: 70 additions & 38 deletions movie-py.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,101 @@
from bs4 import BeautifulSoup
import time
from colorama import init,Fore
import urllib
import os
import demjson
init(convert=True)

print(Fore.LIGHTBLACK_EX+"Loading...")
time.sleep(0.5)
print(Fore.LIGHTBLACK_EX+"Please wait...\n")
print(Fore.LIGHTBLACK_EX+"Loading...") # for displaying Loading....
time.sleep(0.5) # For 0.5 seconds delay.
print(Fore.LIGHTBLACK_EX+"Please wait...") # for displaying Please wait....
#try and except block below is used to checking the internet conectivity.
try :
data = urllib.urlopen("http://www.imdb.com/")
except :
print "No internet connection!"
quit()

try:
args = sys.argv[1:]
movie = (' ').join(args)
page = requests.get('http://www.imdb.com/find?ref_=nv_sr_fn&q=' + movie + '&s=tt');
soup1 = BeautifulSoup(page.content, 'html.parser')
movieid = soup1.select(".findList tr a")[0].get('href')
movielink = "http://www.imdb.com" + movieid
args = sys.argv[1:] #for fetching movie name. i.,e program name is 0 and film name is 1 index.
movie = (' ').join(args).replace(" ", "+") #for replacing spaces with + in the movie string.
page = requests.get("https://www.imdb.com/find?q=" + movie + "&s=tt&ttype=ft&ref_=fn_ft") #for requesting movie website data from the server.
soup1 = BeautifulSoup(page.content, 'html.parser') # parsing html
i = 0
for name in soup1.find_all("td", "result_text"): #looping through elements in td tags having class=result_text in html code soup1.
i = i + 1
print "%d" %(i) + name.text #for print search results of the movie.
option = input('Enter the serial number of the movie: ') #for reading the serial no. of movie.
os.system("reset") #for clearing command line
#below if block is for checking whether the option is valid or not.
if option > i and option < 0:
print("Invalid option.")
quit()
movieid = soup1.find_all("td", "result_text")[option - 1].select("a")[0].get('href') #for finding td tags having class = result_text and selecting <a> tags for getting href attribute value.

movielink = "http://www.imdb.com" + movieid #movie link of select option.
mlinkpage = requests.get(movielink)
soup2 = BeautifulSoup(mlinkpage.content, 'html.parser')
titlenyear = soup2.select(".title_wrapper h1")[0].text
movietitle = titlenyear[0:len(titlenyear) - 8]
movieyear = titlenyear[len(titlenyear) - 6:len(titlenyear) - 2]
movierating = soup2.select(".ratingValue span")[0].text
metascore = soup2.select(".metacriticScore")
metascore = metascore[0].text.strip() if metascore else None
contentrating = soup2.find('meta',{'itemprop':'contentRating'})
contentrating = contentrating['content'].strip() if contentrating else None
movielength = soup2.select(".subtext time")[0].text.strip()
metascore = soup2.find_all("div", class_ = "metacriticScore score_favorable titleReviewBarSubItem") #for fetching the div tags of specified class and extracting the text between them.
if metascore : #metascore is the score rating given by metacriticscore.com .
metascore = metascore[0].text[1:len(metascore) - 1] #for getting string by trimming first and last letter
else: metascore = None
detailsjson = soup2.find_all("script", attrs = { "type" : True }) #for fetching the script tag having attribute type.
for detail in detailsjson :
if detail.get("type") == "application/ld+json" :
detailsjson = detail.text
details = demjson.decode(detailsjson) #Converting json to dictonary
movietitle = details["name"]
contentrating = details["contentRating"]
movielength = details["duration"][2:len(details["duration"])]
genresndate = [i.text for i in soup2.select(".subtext a")]
releasedate = genresndate[-1].strip()
moviegenres = ""
ratingcount = details["aggregateRating"]["ratingCount"]
worstmovierating = details["aggregateRating"]["worstRating"]
movierating = details["aggregateRating"]["ratingValue"]
bestmovierating = details["aggregateRating"]["bestRating"]
for i in soup2.find_all("div","txt-block"):
if i.h4:
if i.h4.text=="Budget:":moviebudget = i.h4.next_element.next_element.strip()
if i.h4.text=="Budget:": moviebudget = i.h4.next_element.next_element.strip()
if i.h4.text=="Opening Weekend USA:":movieopening = i.h4.next_element.next_element.strip()[:-1]
if i.h4.text=="Gross USA:":movieusagross = i.h4.next_element.next_element.strip()[:-1]
if i.h4.text=="Cumulative Worldwide Gross:":movieworldgross = i.h4.next_element.next_element.strip()[:-1]
if i.h4.text=="Aspect Ratio:":movieratio = i.h4.next_element.next_element.strip()
if i.h4.text=="Taglines:":movietaglines = i.h4.next_element.next_element.strip()
if i.h4.text=="Also Known As:":moviealsoknown = i.h4.next_element.next_element.strip()
if i.h4.text=="Country:":moviecountry = i.h4.next_sibling.next_element.text.strip()
for x in range(len(genresndate) - 1):
moviegenres = moviegenres + ',' + genresndate[x]
moviegenres = moviegenres[1:]
moviedesc = soup2.select(".summary_text")[0].text.strip()
moviecast = [i.text for i in soup2.select(".credit_summary_item span a span")]
moviedirector = moviecast[0]
movieactors = moviecast[3] + ',' + moviecast[4] + ',' + moviecast[5];

moviegenres = ', '.join([str(x) for x in details["genre"]])
moviedesc = details["description"]
moviedirector = details["director"]["name"]
i = 0
actor = [None] * len(details["actor"]) #Creating a null list of length = length of actors list
for x in details["actor"] :
actor[i] = details["actor"][i]["name"] #Fetching actors name from details dictonary.
i = i + 1
movieactors = ", ".join(actor) #joining all element in actors list and having ", " as separator.
#Data printing block
print(Fore.LIGHTRED_EX + "Title: " + Fore.LIGHTGREEN_EX + movietitle)
print(Fore.LIGHTRED_EX + "IMDB Rating: " + Fore.LIGHTYELLOW_EX + movierating + "/10")
print(Fore.LIGHTRED_EX + "IMDB Rating: ")
print("\t" + Fore.LIGHTRED_EX + "Rating count: " + Fore.LIGHTYELLOW_EX + str(ratingcount))
print("\t" + Fore.LIGHTRED_EX + "Worst movie rating: " + Fore.LIGHTYELLOW_EX + worstmovierating + "/10")
print("\t" + Fore.LIGHTRED_EX + "Average IMDB rating: " + Fore.LIGHTYELLOW_EX + movierating + "/10")
print("\t" + Fore.LIGHTRED_EX + "Best IMDB rating: " + Fore.LIGHTYELLOW_EX + bestmovierating + "/10")
if metascore: print(Fore.LIGHTRED_EX + "Metascore: " + Fore.LIGHTYELLOW_EX + metascore + "/100")
print(Fore.LIGHTRED_EX + "Length: " + Fore.LIGHTCYAN_EX + movielength)
print(Fore.LIGHTRED_EX + "Year: " + Fore.LIGHTMAGENTA_EX + movieyear)
print(Fore.LIGHTRED_EX + "Genre: " + Fore.LIGHTBLUE_EX + moviegenres)
print(Fore.LIGHTRED_EX + "Description: " + Fore.LIGHTWHITE_EX + moviedesc)
print(Fore.LIGHTRED_EX + "Release date: " + Fore.LIGHTCYAN_EX + releasedate)
if contentrating: print(Fore.LIGHTRED_EX + "Rating: " + Fore.LIGHTCYAN_EX + contentrating)
print(Fore.LIGHTRED_EX + "Director: " + Fore.LIGHTBLACK_EX + moviedirector)
print(Fore.LIGHTRED_EX + "Director: " + Fore.LIGHTBLACK_EX + moviedirector)
print(Fore.LIGHTRED_EX + "Lead Cast: " + Fore.LIGHTBLACK_EX + movieactors)
print(Fore.LIGHTRED_EX + "Country: " + Fore.LIGHTBLUE_EX + moviecountry)
print(Fore.LIGHTRED_EX + "Also Known As: " + Fore.LIGHTBLUE_EX + moviealsoknown)
print(Fore.LIGHTRED_EX + "Budget: " + Fore.LIGHTBLUE_EX + moviebudget)
print(Fore.LIGHTRED_EX + "Opening Weekend USA: " + Fore.LIGHTBLUE_EX + movieopening)
print(Fore.LIGHTRED_EX + "Gross USA: " + Fore.LIGHTBLUE_EX + movieusagross)
print(Fore.LIGHTRED_EX + "Cumulative Worldwide Gross: " + Fore.LIGHTBLUE_EX + movieworldgross)
print(Fore.LIGHTRED_EX + "Ratio: " + Fore.LIGHTBLUE_EX + movieratio)
print(Fore.LIGHTRED_EX + "Taglines: " + Fore.LIGHTBLUE_EX + movietaglines)
if moviealsoknown in globals(): print(Fore.LIGHTRED_EX + "Also Known As: " + Fore.LIGHTBLUE_EX + moviealsoknown)
if "moviebudget" in globals(): print(Fore.LIGHTRED_EX + "Budget: " + Fore.LIGHTBLUE_EX + moviebudget)
if "movieopening" in globals(): print(Fore.LIGHTRED_EX + "Opening Weekend USA: " + Fore.LIGHTBLUE_EX + movieopening)
if "movieusagross" in globals(): print(Fore.LIGHTRED_EX + "Gross USA: " + Fore.LIGHTBLUE_EX + movieusagross)
if "movieworldgross" in globals(): print(Fore.LIGHTRED_EX + "Cumulative Worldwide Gross: " + Fore.LIGHTBLUE_EX + movieworldgross)
if "movieratio" in globals(): print(Fore.LIGHTRED_EX + "Ratio: " + Fore.LIGHTBLUE_EX + movieratio)
if "movietaglines" in globals(): print(Fore.LIGHTRED_EX + "Taglines: " + Fore.LIGHTBLUE_EX + movietaglines)
#End of data printing blockexcept:
except:
print(Fore.LIGHTRED_EX+"Something's wrong,Try Again Later")
9 changes: 0 additions & 9 deletions requirements.txt

This file was deleted.