def get_imdb(title): search_url = "https://api.themoviedb.org/3/search/multi?api_key={apikey}&query={query}" id_url = "https://api.themoviedb.org/3/{media_type}/{id}?api_key=" search = get(search_url.format(apikey=imdbkey, query=title), json=True) if search.data is None or "results" not in search.data: return None, None if len(search.data["results"]) == 0: return None, None else: info = get(id_url.format(**search.data["results"][0]) + imdbkey, json=True) if search.data["results"][0]["media_type"] == "movie": response, overview = formatresponse(info.data, is_movie=True) return response, overview else: response, overview = formatresponse(info.data, is_movie=False)
def get_tv(self, short=False): req = get("https://0xfdb.xyz/nowplaying/test.html") soup = bs4(req.data, 'html.parser') title = soup.find(id="nowplaying").get_text().strip() info, overview = get_imdb(title) if info: self.sendmsg(f"Now Playing: {info}") if short: self.sendmsg(overview) else: self.sendmsg(title)
def autourl(self, msg: str): urlexpression = "http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+" possible = re.findall(urlexpression, msg) req = get(possible[0], allow_redirects=True, timeout=10) domain = re.findall("https?:\/\/(.+?)\/", req.url)[0] if req.status == 200: if soup := bs4(req.data, "html.parser"): try: title = soup.title.string.strip() except AttributeError as error: return None else: return "[ {} ] - {}".format(title, domain)
def get_food2fork(self, search: str) -> str: url = "https://forkify-api.herokuapp.com/api/search?q=" + search request = get(url, json=True) if request.status_code == 200: try: total = len(request.data) num = random.randint(0, total) msg = "({}/{}) {} {}".format(num, total, request.data[num]["title"], request.data[num]["source_url"]) except (IndexError, KeyError) as error: msg = error return msg
def lookup(self, videoid: str) -> dict: searchurl = ( "https://www.googleapis.com/youtube/v3/videos?" "part=contentDetails,snippet&id={vid}&fields=" "items(contentDetails%2Fduration%2Csnippet(channelTitle%2Ctitle))" "&key={key}" ) url = searchurl.format(vid=videoid, key=self.settings["api_key"]) ytjson = get(url, json=True) if ytjson.data.get("error", False): return {} # videoid = ytjson.data["items"][0]["id"]["videoId"] title = ytjson.data["items"][0]["snippet"]["title"] channel = ytjson.data["items"][0]["snippet"]["channelTitle"] _duration = ytjson.data["items"][0]["contentDetails"]["duration"] duration = _duration.lstrip("PT").lower() return {"title": title, "channel": channel, "duration": duration}
def on_pubmsg(self, c, event): # TODO pull prefixes from config prefix = ";" msg = Message(message=event.arguments[0], user=self.channel.getuser(event.source.nick)) # TEMP stick in a module after events are wired if "http" in msg.message: try: urlexpression = "http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+" possible = re.findall(urlexpression, msg.message) req = get(possible[0]) if req.status == 200: soup = bs4(req.data, "html.parser") if soup is not None: try: title = soup.title.string except AttributeError as error: pass else: self.sendmsg(title.strip()) except: pass elif msg.message.startswith(prefix): command = Command(prefix=prefix, data=msg) # TODO move these if command.name == "reload" or command.name == "load": if m := self.cm.import_module(command.message, self): self.cm.add_cog(m, command.message, self) self.sendmsg(f"{command.name}ed {command.message}") else: self.sendmsg(f"failed to {command.name} {command.message}") elif command.name == "unload": if self.cm.unload(command.message): self.sendmsg(f"unloaded {command.message}") else: self.sendmsg(f"Could not unload {command.message}") elif command.name == "loaded": available = ", ".join(list(self.cm.modules.keys())) loaded = ", ".join(list(self.cm.cogs.keys())) self.sendmsg(f"Loaded: {loaded}") self.sendmsg(f"Available: {available}") else: self.cm.do_command(command)
def run(self, bot, data): ## TODO: handle results that are too long fun = None if data.group(1): fun = data.group(1) elif data.group(2): fun = data.group(2)[2:] if fun: ## if we are searching for too small a string, complain if len(fun) < 2: return bot.say("Search term too broad, please refine it") fun = web.quote(fun.encode('utf-8')) res = web.get("http://xelerus.de/doc_function_raw.php?search=%s" % fun) rows = res.splitlines() if len(rows) > 1: funcs = [] for row in rows: id, name, syntax = row.split("|") if name.endswith("_deprecated"): continue if name.lower() == fun: ## we have an exact match, only show this return bot.say("%s | More info: http://xelerus.de/index.php?s=functions&function=%s" % (syntax, id)) funcs.append(name) names = ", ".join(funcs) if len(names) > 512: return bot.say("Too many results. Please refine your search") return bot.say(names) elif len(rows) == 1: id, name, syntax = rows[0].split("|") return bot.say("%s | More info: http://xelerus.de/index.php?s=functions&function=%s" % (syntax, id)) else: return bot.say("No results found")
def run(self, c: Command): req = get(url="https://api.chucknorris.io/jokes/random", json=True) if req.status == 200: self.sendmsg(req.data["value"])
def get_music(self): req = get("https://taro.0xfdb.xyz/radio.html") soup = bs4(req.data, 'html.parser') title = soup.find(id="NowPlaying").get_text().strip() self.sendmsg(title)
def run(self, c: Command): req = get(url="https://api.kanye.rest/", json=True) if req.status == 200: self.sendmsg(req.data["quote"])