Skip to content

Commit

Permalink
skip listener working
Browse files Browse the repository at this point in the history
  • Loading branch information
jbdlb committed Oct 23, 2022
1 parent 3eef546 commit 0e013f9
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 22 deletions.
7 changes: 4 additions & 3 deletions backend/src/spotify_api/api_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@ def get_current_song(self):
self._update_scope(scope="user-read-currently-playing")
current_song = self.sp.currently_playing()
id_ = current_song["item"]["id"]
return self.get_features(id_)
except TypeError:
print("No song is currently playing.")
return self.get_features(id_)
return None


def save_song_features(self, features=pd.Series, skip_state="UNKNOWN", skip_probability=0):
"""
Expand Down Expand Up @@ -140,9 +142,8 @@ def get_next_song(self, sp, playlist_id):
# get currently playing song
current_song = sp.currently_playing()
current_song_id = current_song['item']['id']

idx_next_song = all_track_id.index(current_song_id) + 1
return all_track_id[idx_next_song], all_track_name[idx_next_song]
return all_track_id[idx_next_song]

def get_playlist_id(self):
self._update_scope(scope="user-read-currently-playing")
Expand Down
43 changes: 25 additions & 18 deletions backend/src/spotify_api/skip_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import numpy as np
from pathlib import Path
import time
from datetime import datetime


class SKIP_LISTENER:
Expand All @@ -18,49 +19,55 @@ def __init__(self):
self.current_song = None
self.previous_song = None
self.end_time = 0
self.all_skiped = []
self.all_non_skiped = []
self.end_time
self.all_skipped = []
self.all_non_skipped = []
Path(settings.DATA_PATH, "player_data").mkdir(parents=True, exist_ok = True)

def listen(self):
self.current_song = self.interface.get_current_song()
self.previous_song = self.interface.get_current_song()
self.calc_end_time()
self.update_end_time()
while True:
if time.time() < self.endtime:
if time.time() < self.end_time:
if self.previous_song["id"] != self.current_song["id"]:
self.all_skiped.append(self.previous_song)
self.all_skipped.append(self.previous_song)
self.save_all_skipped()
self.interface.save_song_features(self.previous_song,
"SKIPPEP")
print(f"music skipped {self.previous_song['id']}")
self.previous_song = self.current_song
self.update_end_time()
else:
self.previous_song = self.current_song
else:
self.previous_song
self.all_non_skiped.append(self.previous_song)
self.previous_song = self.current_song
self.current_song = self.interface.get_current_song()
self.update_end_time()
self.all_non_skipped.append(self.previous_song)
self.previous_song["date_saved"] = str(datetime.now())
self.save_all_non_skipped()
print("not_skipped")
self.current_song = self.interface.get_current_song()
self.calc_end_time()
time.sleep(1)
self.current_song = self.interface.get_current_song()

time.sleep(5)

def save_all_non_skipped(self):
self.all_non_skipped = self.all_non_skipped[-50:]
pd.to_csv(self.all_non_skipped, Path(settings.DATA_PATH, "player_data", "non_skipped.csv"))
pd.DataFrame(self.all_skipped).to_csv(Path(settings.DATA_PATH, "player_data",
"non_skipped.csv"))

def save_all_skipped(self):
self.all_skipped = self.all_skipped[-50:]
pd.to_csv(self.all_skipped, Path(settings.DATA_PATH, "player_data",
"skipped.csv"))
pd.DataFrame(self.all_skipped).to_csv(Path(settings.DATA_PATH, "player_data",
"skipped.csv"))

def calc_end_time(self):
def update_end_time(self):
try:
start_time = time.time()
length_of_the_song = self.current_song['duration_ms'] / 1000
self.endtime = start_time + length_of_the_song
self.end_time = start_time + length_of_the_song
except TypeError:
print("no song currently playing")
self.endtime = None
self.end_time = None

if __name__ == "__main__":
listener = SKIP_LISTENER()
Expand Down
7 changes: 6 additions & 1 deletion backend/src/spotify_api/spotify_actioner.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import spotipy
from backend.src.config import settings

from backend.src.spotify_api.api_interface import SPOTIFY_API_INTERFACE

class SpotifyActionnner:
def __init__(self):
Expand All @@ -12,7 +12,9 @@ def __init__(self):
client_secret=settings.SPOTIPY_CLIENT_SECRET,
redirect_uri=settings.SPOTIPY_REDIRECT_URI,
)
self.interface= SPOTIFY_API_INTERFACE()
self.sp = spotipy.Spotify(auth=self.token)
self.automatically_skip=[]

def _update_scope(self, scope: str):
self.token = spotipy.util.prompt_for_user_token(
Expand All @@ -27,4 +29,7 @@ def _update_scope(self, scope: str):

def skip_current_song(self) -> None:
self._update_scope(scope="user-modify-playback-state")
song = self.interface.get_current_song()
self.sp.next_track()

self.automatically_skip.append(song["id"])
3 changes: 3 additions & 0 deletions data/player_data/non_skipped.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
,danceability,energy,key,loudness,mode,speechiness,acousticness,instrumentalness,liveness,valence,tempo,type,id,uri,track_href,analysis_url,duration_ms,time_signature,skip_state,date_saved,skip_probability
0,0.682,0.0987,1,-22.723,1,0.612,0.133,0,0.105,0.396,100.247,audio_features,5ZxX55HFfi4aofGTxMp58x,spotify:track:5ZxX55HFfi4aofGTxMp58x,https://api.spotify.com/v1/tracks/5ZxX55HFfi4aofGTxMp58x,https://api.spotify.com/v1/audio-analysis/5ZxX55HFfi4aofGTxMp58x,33107,5,SKIPPEP,2022-10-23 11:09:07.142544,0
1,0.74,0.266,1,-17.222,1,0.947,0.986,0,0.508,0.963,70.633,audio_features,0EgkVzDidmq4MP57jZFeik,spotify:track:0EgkVzDidmq4MP57jZFeik,https://api.spotify.com/v1/tracks/0EgkVzDidmq4MP57jZFeik,https://api.spotify.com/v1/audio-analysis/0EgkVzDidmq4MP57jZFeik,22747,3,SKIPPEP,2022-10-23 11:09:09.500457,0
4 changes: 4 additions & 0 deletions data/player_data/skipped.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
,danceability,energy,key,loudness,mode,speechiness,acousticness,instrumentalness,liveness,valence,tempo,type,id,uri,track_href,analysis_url,duration_ms,time_signature,skip_state,date_saved,skip_probability
0,0.682,0.0987,1,-22.723,1,0.612,0.133,0,0.105,0.396,100.247,audio_features,5ZxX55HFfi4aofGTxMp58x,spotify:track:5ZxX55HFfi4aofGTxMp58x,https://api.spotify.com/v1/tracks/5ZxX55HFfi4aofGTxMp58x,https://api.spotify.com/v1/audio-analysis/5ZxX55HFfi4aofGTxMp58x,33107,5,SKIPPEP,2022-10-23 11:09:07.142544,0.0
1,0.74,0.266,1,-17.222,1,0.947,0.986,0,0.508,0.963,70.633,audio_features,0EgkVzDidmq4MP57jZFeik,spotify:track:0EgkVzDidmq4MP57jZFeik,https://api.spotify.com/v1/tracks/0EgkVzDidmq4MP57jZFeik,https://api.spotify.com/v1/audio-analysis/0EgkVzDidmq4MP57jZFeik,22747,3,SKIPPEP,2022-10-23 11:09:09.500457,0.0
2,0.682,0.0987,1,-22.723,1,0.612,0.133,0,0.105,0.396,100.247,audio_features,5ZxX55HFfi4aofGTxMp58x,spotify:track:5ZxX55HFfi4aofGTxMp58x,https://api.spotify.com/v1/tracks/5ZxX55HFfi4aofGTxMp58x,https://api.spotify.com/v1/audio-analysis/5ZxX55HFfi4aofGTxMp58x,33107,5,,,

0 comments on commit 0e013f9

Please sign in to comment.