def get_goat_movies(genres: hug.types.text, api_key: hug.types.text, hug_timer=5): """ Get a list of the most upvoted (GOAT) movies. Input: gg_id, genres, api_key URL: http://HOST:PORT/get_single_training_movie?genres=none&api_key=API_KEY """ if (check_api_token(api_key) == True): # API KEY VALID if ((genres == ' ') | (genres == '%20')): result = db.movie.find().sort([('goat_upvotes', -1)])[:10] # print("a") return build_goat_json(result, hug_timer) else: print("b") genres.replace('%20', ' ') # Browser pushes ' ', NodeJS pushes '%20' if (' ' in genres): genres = genres.split(' ') # Splitting the genre string into a list of genres! genre_list_check = isinstance(genres, list) # Check if the genres variable is a list (or a single genre string) print("is list? {}".format(genre_list_check)) print("genres: {}".format(genres)) if (genre_list_check == True): # Multiple genres detected! Count the quantity of movies w/ all of these genres! movie_count = db.movie.find({"genre": {'$all': genres}}).sort([('goat_upvotes', -1)]).count() else: # Single genre detected! Count how many movies there are w/ this genre movie_count = db.movie.find({"genre": genres}).sort([('goat_upvotes', -1)]).count() print("movie count: {}".format(movie_count)) if (movie_count > 0): print("greater than 0") # Results found! if (movie_count >= 10): # More than 10 movies found? Let's limit top-k to 10! k_limit = 10 else: # Less than 10 movies found? Let's reduce the top-k limit! k_limit = movie_count if (genre_list_check == True): # Multiple genre 'all' search result = db.movie.find({"genre": {'$all': genres}}).sort([('goat_upvotes', -1)])[:k_limit] else: # Single genre search result = db.movie.find({"genre": genres}).sort([('goat_upvotes', -1)])[:k_limit] return build_goat_json(result, hug_timer) else: # No results, provide json result for nodejs to detect! return {'success': False, 'valid_key': True, 'took': float(hug_timer)} else: # API KEY INVALID! return {'success': False, 'valid_key': False, 'took': float(hug_timer)}
def get_single_training_movie(gg_id: hug.types.text, genres: hug.types.text, actors: hug.types.text, api_key: hug.types.text, hug_timer=5): """ Get a single movie for the training bot section. Retrieves a list of movies the user has previously voted for, used as a filter! URL: http://HOST:PORT/get_single_training_movie?gg_id=anonymous_google_id&genres=none&actors=none&api_key=API_KEY """ if (check_api_token(api_key) == True): # API KEY VALID user_id = get_user_id_by_gg_id(gg_id) # Get the user's movie rating user ID (incremental) if user_id is not None: imdb_list = get_voted_movie_by_user_id(user_id) # Produce a list of the user's previously rated movies. if ((genres == ' ') | (genres == '%20')): remaining_count = db.movie.find({'imdbID': {'$nin': imdb_list}}).count() result = db.movie.find({'imdbID': {'$nin': imdb_list}})[0] # Filter out the list of previously voted for movies, sorted by imdb vote data (rating, quantity votes), takes top 1. return build_training_response(result, hug_timer, remaining_count) else: genres.replace('%20', ' ') # Browser pushes ' ', NodeJS pushes '%20' if (' ' in genres): #blah! genres = genres.split(' ') #print(genres) genre_list_check = isinstance(genres, list) if (genre_list_check == True): remaining_count = db.movie.find({'imdbID': {'$nin': imdb_list}, "genre": {'$all': genres}}).count() else: remaining_count = db.movie.find({'imdbID': {'$nin': imdb_list}, "genre": genres}).count() if (remaining_count > 0): # Results found! Return 1 result to the user. if (genre_list_check == True): result = db.movie.find({'imdbID': {'$nin': imdb_list}, "genre": {'$all': genres}})[0] else: result = db.movie.find({'imdbID': {'$nin': imdb_list}, "genre": genres})[0] return build_training_response(result, hug_timer, remaining_count) else: # No results, provide json result for nodejs to detect! return {'success': False, 'valid_key': True, 'took': float(hug_timer)} else: # Invalid GG_ID return {'success': False, 'valid_key': True, 'took': float(hug_timer)} else: # API KEY INVALID! return {'success': False, 'valid_key': False, 'took': float(hug_timer)}
def formatData(date: hug.types.text, time: hug.types.text, location1: hug.types.text, location2: hug.types.text, hug_timer=3): """Changing the data types""" print(date) dateString = date[0:4] + "/" + date[4:6] + "/" + date[6:8] timeString = time[0:2] + ":" + time[3:6] location1String = location1.replace(".", ":") location2String = location2.replace(".", ":") return runAstroScript(dateString, timeString, location1String, location2String)
def fixed_simplify(formula: hug.types.text): return sp.simplify(formula.replace(' ', '+'))
def synthesize_data(query: hug.types.text, method: hug.types.text): if query_ok(query): parsed = sqlparse.parse(query)[0] order_found = False order_clauses = [] limit_found = False if parsed.get_type() == 'SELECT': for t in parsed.tokens: if (t.is_whitespace): continue if (t.is_keyword and t.normalized == 'ORDER'): order_found = True continue if order_found: if t.is_keyword and t.normalized != 'BY': break elif isinstance(t, (sqlparse.sql.Identifier, sqlparse.sql.IdentifierList)): order_clauses.append(str(t)) for t in parsed.tokens: if (t.is_keyword and t.normalized == 'LIMIT'): limit_found = True # replace order by clauses with random() # as order by doesn't do anything once synthesis occurs # remove all semicolons - should have at most 1... semicolon_found = ';' in query fixed_query = query.replace(';', '') if order_found: i = query.rfind(order_clauses[0]) fixed_query = fixed_query[:i] + 'random()' + fixed_query[ i + len(order_clauses[0]):] for o in order_clauses[1:]: i = fixed_query.rfind(o) fixed_query = fixed_query[:i] + fixed_query[i + len(o):] # some cleanup fixed_query = re.sub('random\(\),', 'random()', fixed_query, flags=re.M) fixed_query = re.sub('^\s+,', '', fixed_query, flags=re.M) # if no order by statement present, add it else: if limit_found: i = fixed_query.lower().rfind('limit') fixed_query = fixed_query[: i] + "\norder by random()\n" + fixed_query[ i:] else: fixed_query += '\norder by random()' if semicolon_found: fixed_query += ';' try: if method is not None: for m in kfpd.synthesis_methods: if method.lower() == m.lower(): kfpd.plugin = globals()[m + 'Plugin']() df = kfpd.read_sql(fixed_query, db_conn) # if any order by clauses were present, re-apply them if len(order_clauses) > 0: sort_by = [] asc_flags = [] orig_columns = df.columns df.columns = df.columns.str.lower() for o in order_clauses: sub_o = o.split(',') # If you don’t specify the ASC or DESC keyword, SQLite uses ASC or ascending order by default. for s in sub_o: if s.lower().find(' desc') != -1: asc_flags.append(False) else: asc_flags.append(True) sort_by.append( re.sub('\s+asc|\s+desc', '', s, flags=re.IGNORECASE).strip().lower()) df.sort_values(sort_by, ascending=asc_flags, inplace=True) df.columns = orig_columns df_html = (df.style.hide_index().set_table_attributes( "class='table table-hover'").set_uuid('_').render()) # pandas generated html has a lot of stuff we don't want returned # chuck it! df_html = re.sub(' id="T__row\d+_col\d+"', '', df_html) df_html = re.sub(' class="data row\d+ col\d+" ', '', df_html) return { 'message': 'success', 'query': query, 'executed_query': fixed_query, 'response': df_html, 'csv': df.to_csv(index=False) } except Exception as e: print('web-service.synthesize_data() caught exception', str(e)) return {'message': 'error', 'query': query, 'response': str(e)} else: return { 'message': 'error', 'query': query, 'response': 'Invalid query provided.' }