def get_predictions(): videos = [] novos_videos_json = "novos_videos.json" if not os.path.exists(novos_videos_json): run_backend.update_db() last_update = os.path.getmtime(novos_videos_json) * 1e9 # if time.time_ns() - last_update > (720*3600*1e9): # aprox. 1 mes # run_backend.update_db() with open("novos_videos.json", 'r') as data_file: for line in data_file: line_json = json.loads(line) videos.append(line_json) predictions = [] for video in videos: # print(video) # print(video['video_id']) predictions.append((video['video_id'], video['title'], float(video['score']))) predictions = sorted(predictions, key=lambda x: x[2], reverse=True)[:30] predictions_formatted = [] for e in predictions: # print(e) predictions_formatted.append( "<tr><th><a href=\"{link}\">{title}</a></th><th>{score}</th></tr>".format(title=e[1], link=e[0], score=e[2])) return '\n'.join(predictions_formatted), last_update
def get_predictions(): videos = [] novos_videos_json = "data/novos_videos.json" if not os.path.exists(novos_videos_json): run_backend.update_db() last_update = os.path.getmtime(novos_videos_json) * 1e9 #if time.time_ns() - last_update > (720*3600*1e9): #when the range is > than aprox. 1 month # run_backend.update_db() with open(novos_videos_json, 'r') as data_files: for line in data_files: line_json = json.loads(line) videos.append(line_json) predictions = [] for video in videos: predictions.append( (video['video_id'], video['title'], float(video['score']))) #Getting only the 30 recomendations with highest score predictions = sorted(predictions, key=lambda x: x[2], reverse=True)[:30] #Creating html code for each video recommended predictions_formatted = [] for e in predictions: predictions_formatted.append( "<tr><th><a href=\"{link}\">{title}<a/></th><th>{score}<\th><\tr>". format(title=e[1], link=e[0], score=e[2])) return '\n'.join(predictions_formatted), last_update
def get_predictions(): videos = [] new_videos_json = 'new_videos.json' if not os.path.exists(new_videos_json): run_backend.update_db() with open(new_videos_json, 'r') as data_file: for line in data_file: line_json = json.loads(line) videos.append(line_json) predictions = [] for video in videos: print(video) predictions.append( (video['video_id'], video['title'], float(video['score']))) predictions = sorted(predictions, key=lambda x: x[2], reverse=True)[:30] predictions_formatted = [] for prediction in predictions: print(prediction) predictions_formatted.append( '<tr><th><a href=\'{link}\'>{title}</a></th><th>{score}</th></tr>'. format(link=prediction[0], title=prediction[1], score=prediction[2])) return '\n'.join(predictions_formatted)
def get_predictions(): new_videos_csv = "new_videos.csv" if not os.path.exists(new_videos_csv): print("inicio update db") run_backend.update_db(queries, limit_page) last_update = os.path.getmtime(new_videos_csv) * 1e9 predictions = [] new_videos = pd.read_csv("new_videos.csv", sep=";") new_videos = new_videos.drop_duplicates() for row in new_videos.iterrows(): predictions.append((row[1]["video_id"], row[1]["title"], float(row[1]["score"]), row[1]["thumb"])) predictions = sorted(predictions, key=lambda x: x[2], reverse=True)[:30] predictions_formatted = [] carrousel_formatted = [] for n, e in enumerate(predictions): if n == 0: carrousel_formatted.append("""<div class="carousel-item active"> <iframe width="600" height="337" class="iframe-video" src=\"{embed}\" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> <div> <p>Score: <b>{score}</b></p> </div> </div>""".format(embed=e[0], score=e[2])) elif n !=0 and n <= 4: carrousel_formatted.append("""<div class="carousel-item"> <iframe width="600" height="337" class="iframe-video" src=\"{embed}\" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> <div> <p>Score: <b>{score}</b></p> </div> </div>""".format(embed=e[0], score=e[2])) predictions_formatted.append("""<tr> <th><a href=\"{link}\">{title}</a></th> <th>{score}</th> </tr>""".format(title=e[1], link=e[0], score=e[2])) return "\n".join(predictions_formatted), "\n".join(carrousel_formatted), last_update
def get_predictions(): cars = [] new_cars_json = "new_cars.json" if not os.path.exists(new_cars_json): run_backend.update_db() last_update = os.path.getmtime(new_cars_json)*1e9 #if time.time_ns() - last_update > (60*60*24*1e9): #aprox 1 day #run_backend.update_db() #last_update = datetime.datetime.today().strftime('%Y-%m-%d') with open("new_cars.json", "r") as data_file: for line in data_file: line_json = json.loads(line) cars.append(line_json) predictions = [] for car in cars: predictions.append( {"car_link":car['car_id'], "car":car['car'], "score": float(car['score']) } ) predictions = sorted(predictions, key = lambda x: x['score'], reverse = True)[:30] return predictions, last_update
def get_predictions(): events = [] # Using a file as DB in this first version run_backend.update_db() with open("new_events.json", 'r') as data_file: for line in data_file: line_json = json.loads(line) events.append(line_json) predictions = [] for event in events: predictions.append((event['link'], event['name'], float(event['prediction']), event['platform'])) # reverse=True: descending order predictions = sorted(predictions, key=lambda x: x[2], reverse=True)[:50] predictions_formatted = [] for event in predictions: predictions_formatted.append("<tr><th><a href=\"{link}\">{name}</a></th><th>{prediction}, {rounded}</th><th>{platform}</th></tr>".format(link=event[0], \ name=event[1], prediction=event[2], rounded=round(event[2]), platform=event[3])) return '\n'.join(predictions_formatted)
def get_predictions(): videos = [] novos_videos_json = "novos_videos.json" if not os.path.exists(novos_videos_json): run_backend.update_db() last_update = os.path.getmtime(novos_videos_json) * 1e9 #if time.time_ns() - last_update > (720*3600*1e9): # aprox. 1 mes # run_backend.update_db() with open("novos_videos.json", 'r') as data_file: for line in data_file: line_json = json.loads(line) videos.append(line_json) predictions = [] for video in videos: # depois que cria a lista de tuples predictions.append( (video['video_id'], video['title'], float(video['score']))) # aplico a função sorted, usar a função no key, vai pegar o ultimo elemento(score) e vai ordenar de trás pra frente, do maior pro menor essa lista, e vou pegar o 30 primeiros predictions = sorted(predictions, key=lambda x: x[2], reverse=True)[:30] # agora vou iterar, so que ppr essa lista. E nessa lista vou colcoar o código HTML pra cada um dos videos que estão nessa lista de predictions, temos um HTML muito simples que criar uma tag com hiperlink pra cada video, e coloca nessas th e tr que são as tags pra organizar a tabela, e coloca a score tbm. Isso é o codigo de cada um dos videos que temos na pagina do app. predictions_formatted = [] for e in predictions: #print(e) predictions_formatted.append( "<tr><th><a href=\"{link}\">{title}</a></th><th>{score}</th></tr>". format(title=e[1], link=e[0], score=e[2])) # no fim eu preciso dar um 'join' nessa lista e colocar uma linha, eu coloco cada item dessa lista numa nova linha pra ele formatar corretamente a tabela. É possivel ver isso mais claro no codigo fonte da pagina do app. # Daqui o get_prediction vai pro main_page e o resto ta explicado la. return '\n'.join(predictions_formatted), last_update
def get_predictions(): links = [] novos_links_json = "novos_links.json" ## Se o json não existir, buscamos novos links com a função update_db() if not os.path.exists(novos_links_json): run_backend.update_db() last_update_epch = os.path.getmtime(novos_links_json) last_update = time.ctime(last_update_epch) with open("novos_links.json", 'r') as data_file: for line in data_file: line_json = json.loads(line) links.append(line_json) predictions = [] for link in links: if link in predictions: continue else: predictions.append( (link['link_id'], link['titulo'], float(link['score']))) #Ordena do mais interessante ao menos interessante, apresentando apenas 30 predictions = sorted(predictions, key=lambda x: x[2], reverse=True)[:10] predictions_formatted = [] for e in predictions: predictions_formatted.append(""" <tr><th>{title}</th><th>{score}</th><th><a href=\"{link}\"><svg class="bi bi-arrow-right-square-fill" width="1em" height="1em" viewBox="0 0 16 16" fill="currentColor" xmlns="http://www.w3.org/2000/svg"> <path fill-rule="evenodd" d="M2 0a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V2a2 2 0 0 0-2-2H2zm5.646 10.646a.5.5 0 0 0 .708.708l3-3a.5.5 0 0 0 0-.708l-3-3a.5.5 0 1 0-.708.708L9.793 7.5H5a.5.5 0 0 0 0 1h4.793l-2.147 2.146z"/> </svg></a></th></tr> """.format(title=e[1], link=e[0], score=e[2])) predictions_formatted = list(OrderedDict.fromkeys(predictions_formatted)) return '\n'.join(predictions_formatted), last_update
import run_backend import sqlite3 as sql if __name__ == "__main__": with sql.connect(run_backend.db_name) as conn: c = conn.cursor() # Create table c.execute('''CREATE TABLE videos (title text, video_id text, score real, update_time integer)''' ) conn.commit() run_backend.update_db()
import run_backend queries = ['Data+Science', 'Machine+Learning'] limit_page = 2 run_backend.update_db(queries, limit_page)
import run_backend run_backend.update_db("novos_videos.json")