def sanity_checks(subtitle_list=[], range_=None): if len(subtitle_list) > 4: raise InvalidRequest(len(subtitle_list)) if range_: if abs(range_[0] - range_[1]) > 7: raise InvalidRequest(range_)
def sanity_checks(subtitle_list=[], range_=None): if len(subtitle_list) > 4: raise InvalidRequest( f"Expected less than 5 quotes, found {len(subtitle_list)}." ) if range_: req_range = abs(range_[0] - range_[1]) if req_range > 7: raise InvalidRequest( f"Expected less than 8 seconds of range, found {req_range}." )
def extract_total_minute(text): """ Extract total duration from a timestamp request string. :param text: comment string :raises exceptions.InvalidRequest """ content = MINUTE_RE.findall(text) if not content: raise InvalidRequest(f"Invalid request: {text}") return content[0]
def extract_total_minute(text): """ Extract total duration from a timestamp request string. :param text: comment string :raises exceptions.InvalidRequest """ content = MINUTE_RE.findall(text) if not content: raise InvalidRequest("Expected {TOTAL DURATION} variable.") return content[0]
def is_parallel(text): """ :param text: complete comment string :raises exceptions.InvalidRequest """ comment = text.replace("!parallel", "") parallels = [" ".join(movie.split()) for movie in comment.split("|")] if len(parallels) > 3: raise InvalidRequest(comment) if 1 < len(parallels) < 4: return parallels
def is_parallel(text): """ :param text: complete comment string :raises exceptions.InvalidRequest """ comment = text.replace("!parallel", "") parallels = [" ".join(movie.split()) for movie in comment.split("|")] if len(parallels) > 4: raise InvalidRequest( "Expected less than 5 separators, found {len(parallels)}.") if 1 < len(parallels) < 5: return parallels
def get_range(content): """ :param content: string from request square bracket """ seconds = [convert_request_content(second.strip()) for second in content.split("-")] if any(isinstance(second, str) for second in seconds): logger.info("String found. Quote request") return content if len(seconds) != 2: raise InvalidRequest(f"Expected 2 timestamps, found {len(seconds)}.") logger.info("Good gif timestamp request") return tuple(seconds)
def dissect_comment(comment): """ :param comment: comment string :raises exceptions.MovieNotFound :raises exceptions.EpisodeNotFound :raises exceptions.OffensiveWord :raises exceptions.IvalidRequest """ split_command = comment.split(" ") requests_command = split_command[0] if requests_command.lower() not in COMMANDS: return split_command.pop(0) final_comment = " ".join(split_command) if requests_command == "!parallel": parallels = is_parallel(final_comment) try: contents = [REQUEST_RE.findall(parallel) for parallel in parallels] if any(len(content) > 1 for content in contents) or len(parallels) > 3: raise InvalidRequest(final_comment) except TypeError: return title, content = "Parallel", ["Parallel"] else: try: title = final_comment.split("[")[0].rstrip() if is_episode(title): search_episode(EPISODE_LIST, title, raise_resting=False) else: search_movie(MOVIE_LIST, title, raise_resting=False) except IndexError: return content = REQUEST_RE.findall(final_comment) if content: [check_offensive_content(text) for text in content] return { "command": requests_command, "title": title, "comment": final_comment, "content": content, }
def is_valid_timestamp_request(request_dict, movie_dict): """ :param comment_dict: request dictionary :param movie_dict: movie/episode dictionary :raises exceptions.InvalidSource :raises exceptions.InvalidRequest """ runtime_movie = convert_request_content(movie_dict["runtime"]) if runtime_movie == movie_dict["runtime"]: raise InvalidRequest(runtime_movie) runtime_request = convert_request_content( extract_total_minute(request_dict["comment"])) if abs(runtime_movie - runtime_request) > 2: raise DifferentSource(f"{runtime_movie}/{runtime_request}") logger.info(f"Valid timestamp request: {runtime_movie}/{runtime_request}")
def direct_request(comment_str, **kwargs): """ :param comment_str: comment/request string to be converted :param comment_dict: comment_dict """ comment_dict = dissect_comment(comment_str) if not comment_dict: raise InvalidRequest(comment_str) return { "user": kwargs.get("user", "Unknown"), "comment": comment_dict.get("comment"), "type": comment_dict.get("command"), "movie": comment_dict.get("title"), "content": comment_dict.get("content"), "id": 0000, "on_demand": True, "verified": kwargs.get("verified", 1), "priority": kwargs.get("priority", 1), }