From f47c6c419d887429ff91f0eb2d37a062f7feb3a4 Mon Sep 17 00:00:00 2001 From: yasir Date: Tue, 17 Jan 2023 15:01:40 +0700 Subject: [PATCH] Change API and add justwatch --- misskaty/helper/tools.py | 36 ++++++++++++++++++ misskaty/plugins/imdb_search.py | 63 +++++++++++++++++-------------- misskaty/plugins/inline_search.py | 7 +++- 3 files changed, 76 insertions(+), 30 deletions(-) diff --git a/misskaty/helper/tools.py b/misskaty/helper/tools.py index 4d6ae857..2e6e5d76 100644 --- a/misskaty/helper/tools.py +++ b/misskaty/helper/tools.py @@ -2,7 +2,9 @@ import os import random import string import time +import json from http.cookies import SimpleCookie +from urllib.parse import urlparse import psutil @@ -82,3 +84,37 @@ async def rentry(teks): .json() .get("url") ) + +def get_provider(url): + + def pretty(names): + name = names[1] + if names[0] == "play": + name = "Google Play Movies" + return name.title() + + netloc = urlparse(url).netloc + return pretty(netloc.split('.')) + +async def search_jw(movie_name: str, locale: str): + m_t_ = "" + response = await http.get(f"https://justwatch.imdbot.workers.dev/?q={movie_name}&L={locale}".format( + q=movie_name, + L=locale + )) + soup = json.loads(response.text) + items = soup["items"] + for item in items: + if movie_name.lower() == item.get("title", "").lower(): + offers = item.get("offers", []) + t_m_ = [] + for offer in offers: + url = offer.get("urls").get("standard_web") + if url not in t_m_: + p_o = get_provider(url) + m_t_ += f"{p_o} | " + t_m_.append(url) + if m_t_ != "": + m_t_ = m_t_[:-2].strip() + break + return m_t_ \ No newline at end of file diff --git a/misskaty/plugins/imdb_search.py b/misskaty/plugins/imdb_search.py index bfb49e57..c92a215b 100644 --- a/misskaty/plugins/imdb_search.py +++ b/misskaty/plugins/imdb_search.py @@ -18,12 +18,12 @@ from database.imdb_db import * from misskaty import BOT_USERNAME, app from misskaty.core.decorator.errors import capture_err from misskaty.helper.http import http -from misskaty.helper.tools import GENRES_EMOJI, get_random_string +from misskaty.helper.tools import get_random_string, search_jw from misskaty.vars import COMMAND_HANDLER -from utils import demoji LOGGER = logging.getLogger(__name__) LIST_CARI = {} +IMDB_API = "https://imdb.yasirapi.eu.org/search" # IMDB Choose Language @@ -104,17 +104,18 @@ async def imdb_search_id(kueri, message): msg = "" buttons = InlineKeyboard(row_width=4) try: - r = await http.get(f"https://yasirapi.eu.org/imdb-search?q={kueri}") - res = json.loads(r.text).get("result") + # https://yasirapi.eu.org/imdb-search?q=doraemon # Second API + r = await http.get(f"{IMDB_API}?query={kueri}") + res = json.loads(r.text).get("results") if not res: return await k.edit_caption(f"â›”ī¸ Tidak ditemukan hasil untuk kueri: {kueri}") msg += f"đŸŽŦ Ditemukan ({len(res)}) hasil untuk kueri: {kueri}\n\n" for num, movie in enumerate(res, start=1): - title = movie.get("l") - year = f"({movie.get('y')})" if movie.get("y") else "" - type = movie.get("q").replace("feature", "movie").capitalize() + title = movie.get("title") + year = f"({movie.get('year', 'N/A')})" + typee = movie.get("type", 'N/A').capitalize() movieID = re.findall(r"tt(\d+)", movie.get("id"))[0] - msg += f"{num}. {title} {year} - {type}\n" + msg += f"{num}. {title} {year} - {typee}\n" BTN.append( InlineKeyboardButton( text=num, @@ -149,17 +150,17 @@ async def imdb_search_en(kueri, message): msg = "" buttons = InlineKeyboard(row_width=4) try: - r = await http.get(f"https://yasirapi.eu.org/imdb-search?q={kueri}") - res = json.loads(r.text).get("result") + r = await http.get(f"{IMDB_API}/search?query={kueri}") + res = json.loads(r.text).get("results") if not res: return await k.edit_caption(f"â›”ī¸ Result not found for keywords: {kueri}") msg += f"đŸŽŦ Found ({len(res)}) result for keywords: {kueri}\n\n" for num, movie in enumerate(res, start=1): - title = movie.get("l") - year = f"({movie.get('y')})" if movie.get("y") else "" - type = movie.get("q").replace("feature", "movie").capitalize() + title = movie.get("title") + year = f"({movie.get('year', 'N/A')})" + typee = movie.get("type", "N/A").capitalize() movieID = re.findall(r"tt(\d+)", movie.get("id"))[0] - msg += f"{num}. {title} {year} - {type}\n" + msg += f"{num}. {title} {year} - {typee}\n" BTN.append( InlineKeyboardButton( text=num, @@ -199,17 +200,17 @@ async def imdbcari_id(client, query): msg = "" buttons = InlineKeyboard(row_width=4) try: - r = await http.get(f"https://yasirapi.eu.org/imdb-search?q={kueri}") - res = json.loads(r.text).get("result") + r = await http.get(f"{IMDB_API}/search?query={kueri}") + res = json.loads(r.text).get("results") if not res: return await query.message.edit_caption(f"â›”ī¸ Tidak ditemukan hasil untuk kueri: {kueri}") msg += f"đŸŽŦ Ditemukan ({len(res)}) hasil dari: {kueri} ~ {query.from_user.mention}\n\n" for num, movie in enumerate(res, start=1): - title = movie.get("l") - year = f"({movie.get('y')})" if movie.get("y") else "" - type = movie.get("q").replace("feature", "movie").capitalize() + title = movie.get("title") + year = f"({movie.get('year', 'N/A')})" + typee = movie.get("type", "N/A").capitalize() movieID = re.findall(r"tt(\d+)", movie.get("id"))[0] - msg += f"{num}. {title} {year} - {type}\n" + msg += f"{num}. {title} {year} - {typee}\n" BTN.append(InlineKeyboardButton(text=num, callback_data=f"imdbres_id#{uid}#{movieID}")) BTN.extend( ( @@ -238,17 +239,17 @@ async def imdbcari_en(client, query): msg = "" buttons = InlineKeyboard(row_width=4) try: - r = await http.get(f"https://yasirapi.eu.org/imdb-search?q={kueri}") - res = json.loads(r.text).get("result") + r = await http.get(f"{IMDB_API}/search?query={kueri}") + res = json.loads(r.text).get("results") if not res: return await query.message.edit_caption(f"â›”ī¸ Result not found for keywords: {kueri}") msg += f"đŸŽŦ Found ({len(res)}) result for keywords: {kueri} ~ {query.from_user.mention}\n\n" for num, movie in enumerate(res, start=1): - title = movie.get("l") - year = f"({movie.get('y')})" if movie.get("y") else "" - type = movie.get("q").replace("feature", "movie").capitalize() + title = movie.get("title") + year = f"({movie.get('year', 'N/A')})" + typee = movie.get("type", "N/A").capitalize() movieID = re.findall(r"tt(\d+)", movie.get("id"))[0] - msg += f"{num}. {title} {year} - {type}\n" + msg += f"{num}. {title} {year} - {typee}\n" BTN.append(InlineKeyboardButton(text=num, callback_data=f"imdbres_en#{uid}#{movieID}")) BTN.extend( ( @@ -274,10 +275,11 @@ async def imdb_id_callback(_, query): f"https://yasirapi.eu.org/imdb-page?url={url}", headers={"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/600.1.17 (KHTML, like Gecko) Version/7.1 Safari/537.85.10"}, ) + ott = await search_jw(r_json.get("title"), "en_ID") r_json = resp.json().get("result") res_str = "" if judul := r_json.get("title"): - res_str += f"📹 Judul: {judul}\n" + res_str += f"📹 Judul: {judul} [{r_json.get('year')}]\n" if aka := r_json.get("aka"): res_str += f"đŸ“ĸ AKA: {aka}\n\n" else: @@ -312,6 +314,8 @@ async def imdb_id_callback(_, query): res_str += f"🏆 Penghargaan: {GoogleTranslator('auto', 'id').translate(award)}\n\n" else: res_str += "\n" + if ott != "": + res_str += f"\nAvailable On:\n{ott}\n" res_str += f"ÂŠī¸ IMDb by @{BOT_USERNAME}" if trailer := r_json.get("trailer_url"): markup = InlineKeyboardMarkup( @@ -353,9 +357,10 @@ async def imdb_en_callback(bot, query): headers={"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/600.1.17 (KHTML, like Gecko) Version/7.1 Safari/537.85.10"}, ) r_json = resp.json().get("result") + ott = await search_jw(r_json.get("title"), "en_US") res_str = "" if judul := r_json.get("title"): - res_str += f"📹 Title: {judul}\n" + res_str += f"📹 Title: {judul} [{r_json.get('year')}]\n" if aka := r_json.get("aka"): res_str += f"đŸ“ĸ AKA: {aka}\n\n" else: @@ -389,6 +394,8 @@ async def imdb_en_callback(bot, query): res_str += f"🏆 Awards: {award}\n\n" else: res_str += "\n" + if ott != "": + res_str += f"\nAvailable On:\n{ott}\n" res_str += f"ÂŠī¸ IMDb by @{BOT_USERNAME}" if trailer := r_json.get("trailer_url"): markup = InlineKeyboardMarkup( diff --git a/misskaty/plugins/inline_search.py b/misskaty/plugins/inline_search.py index 6dabd5b2..b8408d51 100644 --- a/misskaty/plugins/inline_search.py +++ b/misskaty/plugins/inline_search.py @@ -20,7 +20,7 @@ from pyrogram.types import ( from misskaty import BOT_USERNAME, app, user from misskaty.helper.http import http -from misskaty.helper.tools import GENRES_EMOJI +from misskaty.helper.tools import GENRES_EMOJI, search_jw from misskaty.plugins.misc_tools import get_content from utils import demoji @@ -360,7 +360,7 @@ async def inline_menu(_, inline_query: InlineQuery): description = midb.get("q", "") stars = midb.get("s", "") imdb_url = f"https://imdb.com/title/{midb.get('id')}" - year = f"({midb.get('y')})" if midb.get("y") else "" + year = f"({midb.get('y', '')})" image_url = midb.get("i").get("imageUrl").replace(".jpg", "._V1_UX360.jpg") if midb.get("i") else "https://te.legra.ph/file/e263d10ff4f4426a7c664.jpg" caption = f"đŸŽŦ" caption += f"{title} {year}" @@ -436,6 +436,7 @@ async def imdb_inl(_, query): url = f"https://www.imdb.com/title/{movie}/" resp = await get_content(url) sop = BeautifulSoup(resp, "lxml") + ott = await search_jw(r_json["name"], "en_ID") r_json = json.loads(sop.find("script", attrs={"type": "application/ld+json"}).contents[0]) res_str = "" type = f"{r_json['@type']}" if r_json.get("@type") else "" @@ -520,6 +521,8 @@ async def imdb_inl(_, query): res_str += f"🏆 Penghargaan: {GoogleTranslator('auto', 'id').translate(awards)}\n\n" else: res_str += "\n" + if ott != "": + res_str += f"\nAvailable On:\n{ott}\n" res_str += "ÂŠī¸ IMDb by @MissKatyRoBot" if r_json.get("trailer"): trailer_url = r_json["trailer"]["url"]