def anime(username=None, **kwargs): if username == None: username = input("Username> ") USER = Users.find_user_object(username) anime_name = input("Anime> ") if anime_name.lower() in ['quit', 'q', 'quit()']: return try: anime = searcher.find_by_id(int(anime_name)) except: anime_name = searcher.search_by_name(anime_name)[0][0] anime = searcher.find_by_name(anime_name) # print(anime_name, anime) rec_scores = processor.MAL_native_recommendation( username, raw=True, skip_watched=False)[anime['mal_id']] if len(rec_scores) == 0: avg_score = 1 else: avg_score = sum(rec_scores) / len(rec_scores) entry = processor.score(anime, USER, avg_score) return pretty(anime, USER, entry)
def recommendations(): # Check if an ID was provided as part of the URL. # If ID is provided, assign it to a variable. # If no ID is provided, display an error in the browser. if 'name' in request.args: name = request.args['name'] else: return "Error: No name field provided. Please specify an name." # print(kwargs) kwargs = {} for key in request.args: if key != 'name': kwargs[key] = request.args[key] s_time = time.time() kwargs = convert(kwargs) recs = processor.enhanced_recommendations(name, kwargs)[:10] print("Done") USER = Users.find_user_object(name) data = "".join( [pretty_html(searcher.find_by_id(x[0]), USER, x[1]) for x in recs]) USER.dump() time_stamp = round(time.time() - s_time, 1) return render_template('recommendation.html', name=name, parameters=kwargs, time=time_stamp, recommendation_anime=data)
def add_key(entry): mal_id, scores = entry anime = searcher.find_by_id(mal_id) # print(anime) if anime == None: anime = add_new_anime(mal_id) if anime == None: print("INVALID ID: {}".format(mal_id)) return (mal_id, (0, )) refresh_animes.append(anime) if has_prequel(anime) and not USER.watch_prequel(anime): return (mal_id, (0, )) if searcher.resolve_kwargs(anime, kwargs): genres = [x['name'] for x in anime['genre']] avg_multiplier = sum(scores) / len(scores) if avg_multiplier != 1: avg_multiplier *= (1 / USER.average_difference) my_score = score(anime, USER, avg_multiplier) # print(avg_multiplier, anime['score'], USER.genre_rating(genres), USER.studio_rating(anime), ENHANCED_RECOMMENDATIONS[key][0]) return (mal_id, my_score) # print(avg_multiplier, anime['score'], USER.genre_rating(genres), USER.studio_rating(anime), ENHANCED_RECOMMENDATIONS[key][0]) return (mal_id, (0, ))
def generate(self): json_data = {} json_data['raw_genres'] = defaultdict(list) json_data['normalized_genres'] = defaultdict(list) json_data['normalized_studio'] = defaultdict(list) json_data['normalized_staff'] = defaultdict(list) for anime in self.user_data: score = anime['score'] if score == 0: continue status = anime['status'] mal_id = anime['anime_id'] # print(type(mal_id)) anime = find_by_id(mal_id) if anime == None: print(mal_id) continue # print(anime) for genre in anime['genre']: json_data['raw_genres'][genre['name']].append(score) json_data['normalized_genres'][genre['name']].append( round(score / anime['score'], 2)) # input() self.json_data = json_data return self
def confirm_valid(self): refresh_animes = [] for anime in self.user_data: if find_by_id(anime['anime_id']) == None: refresh_animes.append(Scrape(anime['anime_id'])) refresh_animes = [x for x in refresh_animes if x != None] if len(refresh_animes) != 0: searcher.refresh(refresh_animes)
def rec_html(username="******", count=10, **kwargs): USER = Users.find_user_object(username) htmls = [] recommendations = processor.enhanced_recommendations(username, kwargs)[:count] for entry in recommendations: anime = searcher.find_by_id(entry[0]) htmls.append(pretty_html(anime, USER, entry[1])) USER.dump() return htmls
def __init__(self, username): self.username = username self.file_name = "Users/{}.json".format(username) self.user_data = user_scrape(self.username) self.IDs = [x['anime_id'] for x in self.user_data] self.complete_IDs = [ x['anime_id'] for x in self.user_data if str(x['status']) in ['1', '2'] ] self.average_difference = [ x['score'] / find_by_id(x['anime_id'])['score'] for x in self.user_data if x != None and find_by_id(x['anime_id']) != None and x['score'] != 0 ] self.average_difference = sum(self.average_difference) / len( self.average_difference) print(round(self.average_difference, 2)) self.recommendations = None self.confirm_valid()
def rec(username="******", count=10, **kwargs): USER = Users.find_user_object(username) print( "---------------------------------------------------------------------------" ) recommendations = processor.enhanced_recommendations(username, kwargs)[:count] for entry in recommendations: anime = searcher.find_by_id(entry[0]) print(pretty(anime, USER, entry[1])) print( "---------------------------------------------------------------------------" ) USER.dump()
def MAL_native_recommendation(username='******', raw=False, skip_watched=True): refresh_animes = [] USER = Users.find_user_object(username) if USER.recommendations != None: return USER.recommendations USER_DATA = USER.user_data RECOMMENDATIONS = defaultdict(list) IDs = Users.find_user_object(username).complete_IDs for user_anime in USER_DATA: if user_anime['status'] != 2: continue # print(user_anime) mal_id = user_anime['anime_id'] user_score = user_anime['score'] # Confirming we got the Anime anime = searcher.find_by_id(mal_id) if anime == None: anime = add_new_anime(mal_id) if anime == None: print("INVALID ID") continue # Queue this to add to database refresh_animes.append(anime) score_diff = round( user_score / anime['score'] / USER.average_difference, 2) for rec in anime.get( 'user_recommendations', []) if anime.get('user_recommendations', []) != None else []: if rec['mal_id'] in IDs and skip_watched: continue RECOMMENDATIONS[rec['mal_id']] += [score_diff] * int( rec['count']) + ([1] * (1 + int(int(rec['count']) / 4))) # for key in list(RECOMMENDATIONS.keys()): # RECOMMENDATIONS[key] = sum(RECOMMENDATIONS[key]) / len(RECOMMENDATIONS[key]) * (1 + (len(RECOMMENDATIONS[key]) / 500)) USER.recommendations = RECOMMENDATIONS if len(refresh_animes) != 0: searcher.refresh(refresh_animes) if raw: return RECOMMENDATIONS return sorted(RECOMMENDATIONS.items(), key=lambda x: sum(x[1]) / len(x[1]), reverse=True)
def calculate_combos(self): combos = set() for anime in self.user_data: score = anime['score'] if score == 0: continue status = anime['status'] mal_id = anime['anime_id'] # print(mal_id) anime = find_by_id(mal_id) if anime == None: continue # GENRES genres = [x['name'] for x in anime['genre'] if x != None] for genre_group in [ combo for x in range(1, len(genres) + 1) for combo in itertools.combinations(genres, x) ]: str_genre_group = str("|".join(sorted(genre_group))) self.json_data['normalized_genres'][ str_genre_group] = self.json_data['normalized_genres'].get( str_genre_group, []) + [round(score / anime['score'], 2)] # STUDIO studios = [x['name'] for x in anime['studio']] licensors = [x['name'] for x in anime['licensor']] producers = [x['name'] for x in anime['producer']] All = studios + licensors + producers for studio in All: self.json_data['normalized_studio'][ studio] = self.json_data['normalized_studio'].get( studio, []) + [round(score / anime['score'], 2)] # STAFF staff = [x['name'] for x in anime.get('staff', [])] for s in staff: self.json_data['normalized_staff'][ s] = self.json_data['normalized_staff'].get( s, []) + [round(score / anime['score'], 2)]
def raw(): # Check if an ID was provided as part of the URL. # If ID is provided, assign it to a variable. # If no ID is provided, display an error in the browser. if 'name' in request.args: name = request.args['name'] else: return "Error: No name field provided. Please specify an name." # print(kwargs) kwargs = {} for key in request.args: if key != 'name': kwargs[key] = request.args[key] kwargs = convert(kwargs) recs = processor.enhanced_recommendations(name, kwargs)[:10] USER = Users.find_user_object(name) USER.dump() return jsonify( [json_output(searcher.find_by_id(x[0]), USER, x[1]) for x in recs])
def add_new_anime(ID): if searcher.find_by_id(ID) != None: print("Already have {} in the system!".format(ID)) return None anime = AnimeScraper.Scrape(ID) return anime