from datetime import timedelta, date from flask import request from api import models, api_errors from api.resources import AuthenticatedResource from api.resources.charts import SpotifyOlap from tracker import db from tracker.bangers.slope7 import youtube_slope7_matrix from tracker.soundcloud_analyzer import get_results class YouTubeAnalyzer(AuthenticatedResource): default_filters = dict( minPlaysYesterday=1000, timePeriod=1, strength=20, noiseReduction=0.6, ) def post(self): # filters = {**self.default_filters, **(request.get_json().get('filters') or {})} # # period = filters['timePeriod'] # if period == 0: # slope_arg_name = 'min_slope15' # rsq_arg_name = 'min_rsq15' # elif period == 1: # slope_arg_name = 'min_slope22' # rsq_arg_name = 'min_rsq22' # elif period == 2: # slope_arg_name = 'min_slope29' # rsq_arg_name = 'min_rsq29' # else: # raise api_errors.BadRequestError(f"Unknown timePeriod '{period}'") # # kwargs = { # slope_arg_name: filters['strength'], # rsq_arg_name: filters['noiseReduction'], # 'min_plays_yesterday': filters['minPlaysYesterday'] # } all_rows = list(db.Session.execute(''' select video_ytid, published, current_date - published, started_at, total_plays, max_daily_plays, min_daily_plays, avg_daily_plays, last_plays, plays7, slope7, rsq7, yint7, count7, plays14, slope14, rsq14, yint14, count14, plays21, slope21, rsq21, yint21, count21, plays28, slope28, rsq28, yint28, count28, lang_code, audio_language, text_language, has_music_topic, is_major_label, maybe_english, duration from mv_yt_video_regressions r where ( (slope7 > slope21 and slope21 > 10) or (slope7 > slope14 and slope14 > 8) ) and rsq21 > 0.5 and last_plays < 500000 and has_music_topic and maybe_english and not is_major_label and lang_code = 0 and duration < 720 and ended_at >= current_date - 2 and published > current_date - 365 order by slope7 desc ''')) return { "matrix": all_rows, "schema": [ "videoYtid", "published", "release_days_ago", "started_at", "total_plays", "max_daily_plays", "min_daily_plays", "avg_daily_plays", "plays_yesterday", "plays7", "slope7", "rsq7", "yint7", "count7", "plays14", "slope14", "rsq14", "yint14", "count14", "plays21", "slope21", "rsq21", "yint21", "count21", "plays28", "slope28", "rsq28", "yint28", "count28", "lang_code", "audio_language", "text_language", "has_music_topic", "is_major_label", "maybe_english", "duration" ], } class YouTubeVidByYtid(AuthenticatedResource): def get(self): ytids = [y.strip() for y in (request.args.get("ytids") or "").split(",")] vids = list(models.youtube_videos(ytids)) return { "results": vids }