def add_track_ids(db: DbStore, export_path=EXPORT_PATH): data = parse_file(export_path) print("Loaded {} plays fro GDPR. Removing short plays (<30s)...".format(len(data))) data = remove_short_plays(data) print("Now got {} plays. Removing tracks already in database...".format(len(data))) # FIXME # data = remove_recent(data, db.get_first_record()) print("Got {} plays to insert into database".format(len(data))) creds = get_credentials() count = len(data) failures = [] plays = [] cache = {} for i, item in enumerate(data): main_artist = item["artistName"].split(",")[0] cache_key = "{}:{}".format(item["trackName"], item["artistName"]) print("[{}%] ".format(int(100 * i / count)), end="") if cache_key in cache: print("Cache hit! ", end="") play = cache[cache_key] # shift into format for play else: play = get_track_play(db, creds, item["trackName"], main_artist) play = { "track": { "duration_ms": None, "popularity": None, "name": play["track_name"], "id": play["track_id"], "album": { "name": play["album_name"], "id": play["album_id"] }, "artists": [ { "name": play["main_artist_name"], "id": play["main_artist_id"], } ] } } # strip().replace(" ", "T") + "Z" if play: play["played_at"] = item["time"].isoformat() print("got {} by {} with id {}".format(item["trackName"], item["artistName"], play)) cache[cache_key] = play db.add_play(play) else: failures.append(item) print("FAIL FAIL FAIL") print(failures) with open("import.json", "w+", encoding="utf-8") as f: f.write(json.dumps(plays, default=json_serial))
def insert(tracks, db: DbStore): # Get last track listened to stored in db # This is to ensure we don't duplicate items in database latest_track_time = db.most_recent_played_at() logging.info( "Retrieved tracks from Spotify, filtering out ones played up to {}". format(latest_track_time)) if latest_track_time: tracks = remove_tracks_before_inc(tracks, latest_track_time) logging.info("Inserting {} tracks".format(len(tracks))) for track in tracks: logging.info("Adding track {}".format(util.track_to_string(track))) db.add_play(track)