def handle_reaction(reaction, user): """ If the user clicks an emoji on one of our search result messages, this triggers the request by firing off a fake message to the request command """ if not reaction.message.embeds: return content = reaction.message.embeds[0].description # we only care about reactions from whoever performed the search if not f"<@{user.id}>" in content and not f"<@!{user.id}>" in content: return # I didn't want to store any state in the bot so we are awkwardly parsing all the state # we need out of the message selected_line = re.search(f"{reaction} ([^\n]*)", content) if not selected_line: return radarr_regex = r'\[([^\]]+)\]\(' + re.escape(tmdb.movie_url) + r'(\d+)\)' if radarr_result := re.search(radarr_regex, selected_line.group(0)): tmdb_id = "tmdb:" + radarr_result.group(2) logger.info(f"{user} selected movie {tmdb_id}") return request.handle_message(tmdb_id, None, title=radarr_result.group(1))
def req_movie_request(tmdbId): logger.info(f"in req_movie_request: tmdbId = {tmdbId}") look = req_movie_lookup(tmdbId=tmdbId) logger.info(f"in req_movie_request: look = {look}") try: lookup = look["json"][0] except IndexError: return { "code": 600, "json": [{ "message": f"Could not find movie with id {tmdbId}" }] } if look['code'] >= 400: return look body = { 'title': lookup['title'], 'tmdbId': tmdbId, 'qualityProfileId': 1, 'titleSlug': lookup['titleSlug'], 'images': lookup['images'], 'year': lookup['year'], "rootFolderPath": Config.RADARR_ROOT_FOLDER_PATH, "monitored": True, 'addOptions': { "searchForMovie": True } } URI = "movie" return api.req_item(conn, URI, body)
def req_movie_lookup(name=None, tmdbId=0, imdbIdStr=None, imdbId=0): if name is None and tmdbId == 0 and imdbIdStr is None and imdbId == 0: return URI = "movie/lookup" if name: params = {"term": name} elif tmdbId > 0: URI = URI + "/tmdb" params = {"tmdbId": f"{tmdbId}"} else: URI = URI + "/imdb" if imdbIdStr is not None: params = {"imdbId": imdbIdStr} else: params = {"imdbId": f"tt{imdbId}"} logger.info(f"URI: {URI}, params: {params}") return api.req_item_lookup(conn, URI, params)
def req_item(conn, URI, body): resp = req(conn, URI, method="POST", body=body) logger.info(resp) logger.info(resp.json()) #i messed this up if resp: if isinstance(resp.json(), dict): json = [resp.json()] elif isinstance(resp.json(), list): json = resp.json() else: json = [{}] return {"code": resp.status_code, "json": json} try: if resp.json(): if isinstance(resp.json(), dict): return {"code": resp.status_code, "json": [resp.json()]} else: return {"code": resp.status_code, "json": resp.json()} return {"code": resp.status_code, "json": None} except: return {"code": None, "json": None}
request by firing off a fake message to the request command """ if not reaction.message.embeds: return content = reaction.message.embeds[0].description # we only care about reactions from whoever performed the search if not f"<@{user.id}>" in content and not f"<@!{user.id}>" in content: return # I didn't want to store any state in the bot so we are awkwardly parsing all the state # we need out of the message selected_line = re.search(f"{reaction} ([^\n]*)", content) if not selected_line: return radarr_regex = r'\[([^\]]+)\]\(' + re.escape(tmdb.movie_url) + r'(\d+)\)' if radarr_result := re.search(radarr_regex, selected_line.group(0)): tmdb_id = "tmdb:" + radarr_result.group(2) logger.info(f"{user} selected movie {tmdb_id}") return request.handle_message(tmdb_id, None, title=radarr_result.group(1)) sonarr_regex = r'\[([^\]]+)\]\(' + re.escape(tmdb.tv_url) + r'(\d+)\)' if sonarr_result := re.search(sonarr_regex, selected_line.group(0)): tvdb = "tmdbtv:" + sonarr_result.group(2) logger.info(f"{user} selected series {tvdb}") return request.handle_message(tvdb, None, title=sonarr_result.group(1))