added mood based movie recommendation api

This commit is contained in:
codeswithroh 2022-10-10 09:37:50 +05:30
parent fda7e05751
commit 30e60db9d0
3 changed files with 77 additions and 0 deletions

View File

@ -0,0 +1,3 @@
# Mood based movie recommendation API
This is a simple api created with Python and IMDB api. This takes in mood as the input and returns back the recommended movie to watch.

View File

@ -0,0 +1,72 @@
from bs4 import BeautifulSoup as SOUP
from fastapi.middleware.cors import CORSMiddleware
from fastapi import FastAPI
import requests as HTTP
import json
import re
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=False,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
async def root():
return {"message": "Hello World"}
@app.get("/movies/")
async def read_item(emotion: str):
urlhere=""
if(emotion == "Sad"):
urlhere = 'http://www.imdb.com/search/title?genres=drama&title_type=feature&sort=moviemeter, asc'
elif(emotion == "Disgust"):
urlhere = 'http://www.imdb.com/search/title?genres=musical&title_type=feature&sort=moviemeter, asc'
elif(emotion == "Anger"):
urlhere = 'http://www.imdb.com/search/title?genres=family&title_type=feature&sort=moviemeter, asc'
elif(emotion == "Anticipation"):
urlhere = 'http://www.imdb.com/search/title?genres=thriller&title_type=feature&sort=moviemeter, asc'
elif(emotion == "Fear"):
urlhere = 'http://www.imdb.com/search/title?genres=sport&title_type=feature&sort=moviemeter, asc'
elif(emotion == "Enjoyment"):
urlhere = 'http://www.imdb.com/search/title?genres=thriller&title_type=feature&sort=moviemeter, asc'
elif(emotion == "Trust"):
urlhere = 'http://www.imdb.com/search/title?genres=western&title_type=feature&sort=moviemeter, asc'
elif(emotion == "Surprise"):
urlhere = 'http://www.imdb.com/search/title?genres=film_noir&title_type=feature&sort=moviemeter, asc'
response = HTTP.get(urlhere)
data = response.text
soup = SOUP(data, "lxml")
movie_title = soup.find_all("a", attrs = {"href" : re.compile(r'\/title\/tt+\d*\/')})
movie = []
title = []
for i in movie_title:
tmp = str(i).split('>')
if(len(tmp) == 3):
title.append(tmp[1][:-3])
for i in title[:4]:
movie_url = f"https://www.omdbapi.com/?apikey=5677e549&t={i}&plot=full"
response = HTTP.get(movie_url)
movie_data = response.text
movie.append(json.loads(movie_data))
return movie

View File

@ -0,0 +1,2 @@
bs4
fastapi