import logging from datetime import datetime from os import path from urllib.parse import urlencode import pandas from ui_automation_framework.utils import logger as cl from app.api_client import ApiClient log = cl.Logger(logging.DEBUG) apollo_env = "https://proxy.apollo.stream" delphi_env = "https://stage-delphi-api.delphiplatform.io" delphi_client_id = "" delphi_client_secret = "" apollo_client_id = "" apollo_client_secret = "" apollo_token_url = "" class APIOverride(ApiClient): def __init__(self): self.api_apollo_client = ApiClient(host_endpoint=apollo_env) self.api_delphi_client = ApiClient(host_endpoint=delphi_env) def get_apollo_response(self, url): return self.api_apollo_client.get_response(endpoint=url, token=self.apollo_authorize(), user="", env=apollo_env)["body"] def get_delphi_response(self, url): return self.api_delphi_client.get_response(endpoint=url, token=self.delphi_authorize(delphi_client_id, delphi_client_secret), user="", env=delphi_env)["body"] def test_apollo_api_connection(self): response = self.get_apollo_response("/sonytracks/analyze?playlisturi=spotify%3Aplaylist%3A37i9dQZF1DXcBWIGoYBM5M") df = pandas.DataFrame({"Response": response}) df.reset_index().to_excel("apollo_api_response.xlsx") def test_delphi_api_connection(self): response = self.get_delphi_response("/v3/public/playlists/spotify_37i9dQZF1DXcBWIGoYBM5M") df = pandas.DataFrame({"Response": response}) df.reset_index().to_excel("delphi_response.xlsx") def get_latest_date(self): date = self.get_apollo_response("/dsp-api/consumer_analytics/streams-latest-date?vendors=all") return date def get_spotify_track_current_playlists(self, isrc, date): response = self.get_apollo_response(f"/gate-api/playlists/spotify/by-track/?isrc={isrc}&limit=2000&offset=0&date={date}") return response def get_spotify_track_past_playlists(self, isrc): response = self.get_apollo_response(f"/gate-api/playlists/spotify/by-track/history/?isrc={isrc}&limit=2000&excludeCurrentPlaylists=true") return response def get_apple_track_current_playlists(self, isrc): response = self.get_apollo_response(f"/applemusic/playlistswithtrack?isrc={isrc}&limit=1000") return response def get_apple_track_past_playlists(self, isrc): response = self.get_apollo_response(f"/applemusic/playlists/tracklisthistory/track?isrc={isrc}&limit=500") return response def delphi_authorize(self, client_id, client_secret): data = {"client_id": client_id, "client_secret": client_secret, "grant_type": "client_credentials"} headers = {"content-type": "application/x-www-form-urlencoded"} session_token = self.session.post(url=f"{delphi_env}/v3/oauth/token", data=urlencode(data), headers=headers) self.logger.info(f"delphi token response: {session_token.json()}") access_token = session_token.json().get("access_token") self.logger.info(access_token) if access_token is None: raise ValueError("Can not get the access token!") return access_token def apollo_authorize(self): today = str(datetime.today().date()) file_name = f"token_{today}.txt" if path.exists(file_name): file = open(file_name) access_token = file.readlines()[0] else: data = {"audience": "apollo|api", "grant_type": "client_credentials", "client_id": apollo_client_id, "client_secret": apollo_client_secret} headers = {"content-type": "application/x-www-form-urlencoded"} session_token = self.session.post(url=apollo_token_url, data=urlencode(data), headers=headers) self.logger.info(f"apollo token response: {session_token.json()}") access_token = session_token.json().get("access_token") f = open(file_name, "a") f.write(access_token) f.close() self.logger.info(access_token) if access_token is None: raise ValueError("Can not get the access token!") self.logger.info(f"apollo token now: {access_token}") return access_token