vcinema/imdb_utils/IMDbUtils.py

61 lines
1.2 KiB
Python
Raw Normal View History

2022-02-20 19:39:18 +00:00
from imdb import IMDb
2022-04-08 22:47:49 +01:00
import requests
from bs4 import BeautifulSoup
import re
2022-02-20 19:39:18 +00:00
def get_movie(imdb_id):
ia = IMDb()
movie = ia.get_movie(imdb_id)
return movie
2022-04-03 20:37:37 +01:00
def get_movie_keywords(imdb_id):
ia = IMDb()
movie = ia.get_movie(imdb_id, info="keywords")
return movie
2022-04-08 22:47:49 +01:00
def get_api_keyword_count(keyword):
ia = IMDb()
count = len(ia.get_keyword(keyword))
return count
def get_website_keyword_count(keyword):
try:
page = requests.get("https://www.imdb.com/search/keyword/?keywords=" + keyword)
except ConnectionError:
raise
soup = BeautifulSoup(page.content, 'html.parser')
elements = soup.findAll("div", class_="desc")
pagination_label = elements[0].text.replace("\n", "")
pagination_label_reg = "(\d+,?\d*) titles"
pattern_match = re.compile(pagination_label_reg).search(pagination_label)
if pattern_match is not None:
return int(pattern_match.group(1).replace(',', ''))
else:
return 1
def get_keyword_count(keyword):
count = get_api_keyword_count(keyword)
if count == 50:
try:
count = get_website_keyword_count(keyword)
except Exception as e:
raise
return count