Skip to content

Commit

Permalink
Merge branch 'develop' into production
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurVardevanyan committed Nov 6, 2022
2 parents 61595fe + ecf011b commit 747220b
Show file tree
Hide file tree
Showing 8 changed files with 620 additions and 96 deletions.
166 changes: 144 additions & 22 deletions monitoringBackend/database.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
from datetime import datetime, timezone
from datetime import datetime
from django.db import connection
import sys
from random import randint
Expand All @@ -11,7 +10,16 @@
log = logging.getLogger(__name__)


def scanWorkers(workerID):
def scanWorkers(workerID: int):
"""
Boots the Spotify Monitoring Backend.
Assigns this Instance an UID into the Database
Parameters:
int: Current Worker ID
Returns:
int: Worker Count
"""
utc_time = datetime.now()
currentEpoch = int(utc_time.astimezone().timestamp())
models.Workers.objects.filter(worker=str(workerID)).update(
Expand All @@ -27,7 +35,15 @@ def scanWorkers(workerID):


def createWorker():

"""
Boots the Spotify Monitoring Backend.
Assigns this Instance an UID into the Database
Parameters:
None
Returns:
int: Current Worker ID
"""
workerID = randint(10**(9-1), (10**9)-1)
utc_time = datetime.now()
currentEpoch = int(utc_time.astimezone().timestamp())
Expand All @@ -36,11 +52,19 @@ def createWorker():
models.Workers(worker=workerID, lastUpdated=currentEpoch,
creationTime=currentTime, updatedTime=currentTime).save()

count = scanWorkers(workerID)
return workerID, count
return workerID


def user_status(user: str, detailed: int = 0):
"""
Add Artist to Artist List
def user_status(user, detailed=0):
Parameters:
user (str) : User ID
detailed (int) : Whether to Return Detailed output or not.
Returns:
Tuple | Literal[0]: Depends on if Detailed Output or not.
"""
with connection.cursor() as cursor:
users = "SELECT * from users where user ='" + user + "'"
cursor.execute(users)
Expand All @@ -54,7 +78,16 @@ def user_status(user, detailed=0):
return status


def add_artists(spotify, cursor):
def add_artists(spotify: dict, cursor: connection.cursor):
"""
Add Artist to Artist List
Parameters:
cursor (cursor): Database Connection
spotify (dict) : Dictionary Containing Song Details
Returns:
int: unused return
"""
artists = "SELECT * from artists"
cursor.execute(artists)
artists = []
Expand All @@ -78,10 +111,19 @@ def add_artists(spotify, cursor):
iter.get("name")
)
cursor.execute(add_artist, data_artist)
return 0


def add_song_artists(spotify, cursor):
def add_song_artists(spotify: dict, cursor: connection.cursor):
"""
Add Song / Artist to Song / Artist List
Parameters:
cursor (cursor): Database Connection
spotify (dict) : Dictionary Containing Song Details
Returns:
int: unused return
"""
for iter in spotify.get("item").get("artists"):
add_song_artist = ("INSERT IGNORE INTO songArtists"
"(songID,artistID)"
Expand All @@ -91,9 +133,21 @@ def add_song_artists(spotify, cursor):
iter.get("id")
)
cursor.execute(add_song_artist, data_song_artist)
return 0


def add_song_count(user: str, spotify: dict, cursor: connection.cursor, count: int = 1):
"""
Add Song / Artist to Song / Artist List
def add_song_count(user, spotify, cursor, count=1):
Parameters:
user (str) : User ID
spotify (dict) : Dictionary Containing Song Details
cursor (cursor): Database Connection
count (int) : PlayCount is set to 0 when inputting unplayed songs from playlist.
Returns:
int: unused return
"""
playCount = "SELECT `playCount` from `playCount` WHERE songID = '" + \
spotify.get("item").get("id") + "' and user = '" + user + "'"
cursor.execute(playCount)
Expand All @@ -116,9 +170,19 @@ def add_song_count(user, spotify, cursor, count=1):
add_song = ("UPDATE playCount SET playCount = '" + str(playCount) +
"' WHERE songID = '" + spotify.get("item").get("id") + "' and user = '" + user + "'")
cursor.execute(add_song)
return 0


def add_song(spotify: dict, cursor: connection.cursor):
"""
Add Song to Song List
def add_song(spotify, cursor):
Parameters:
cursor (cursor): Database Connection
spotify (dict) : Dictionary Containing Song Details
Returns:
int: unused return
"""
add_song = ("INSERT IGNORE INTO songs"
"(id,name,trackLength)"
"VALUES (%s, %s, %s)")
Expand All @@ -128,11 +192,21 @@ def add_song(spotify, cursor):
spotify.get("item").get("duration_ms")
)
cursor.execute(add_song, data_song)
add_song_artists(spotify, cursor) # Function
add_song_artists(spotify, cursor)
return 0


def listening_history(user, spotify, cursor):
def listening_history(user: str, spotify: dict, cursor: connection.cursor):
"""
Add Song to Listening History
Parameters:
user (str) : User ID
cursor (cursor): Database Connection
spotify (dict) : Dictionary Containing Song Details
Returns:
bool: Whether Song is Duplicate or Not.
"""
add_play = ("INSERT IGNORE INTO listeningHistory"
"(user, timestamp,timePlayed, songID)"
"VALUES (%s, %s, %s, %s)")
Expand All @@ -146,11 +220,19 @@ def listening_history(user, spotify, cursor):

if (int(cursor.rowcount) == 0):
logging.warning("Duplicate History Song: " + str(data_play[:-1]))
return 0
return 1
return False
return True


def get_playlists(user):
def get_playlists(user: str):
"""
Get List of Playlists from Associated User
Parameters:
user (str) : User ID
Returns:
list: List of Playlists
"""
with connection.cursor() as cursor:
query = "SELECT playlists.playlistID, name, playlists.lastUpdated from playlists INNER JOIN playlistsUsers ON playlistsUsers.playlistID = playlists.playlistID where user = '"+user+"'"
cursor.execute(query)
Expand All @@ -160,7 +242,15 @@ def get_playlists(user):
return playlists


def add_playlist(user, playlist):
def add_playlist(playlist: str):
"""
Create Playlist In Database
Parameters:
playlist (str) : Which Playlist to Insert Song Into
Returns:
int: unused return
"""
with connection.cursor() as cursor:
utc_time = datetime.utcnow()
lastUpdated = utc_time.strftime("%Y-%m-%d %H:%M:%S")
Expand All @@ -172,9 +262,20 @@ def add_playlist(user, playlist):
addPlaylist = " UPDATE playlists SET lastUpdated = '"+lastUpdated+"' WHERE playlistID = '" + \
playlist+"'"
cursor.execute(addPlaylist)
return 0


def add_playlist_songs(cursor: connection.cursor, song: str, playlist: str, status: str):
"""
Input Songs from Playlist into all PlayList Fields
def add_playlist_songs(cursor, song, playlist, status):
Parameters:
cursor (cursor): Database Connection
playlist (str) : Which Playlist to Insert Song Into
status (str) : local/playable/unplayable
Returns:
int: unused return
"""
addPlaylist = ("INSERT IGNORE INTO playlistSongs"
"(playlistID, songID, songStatus)"
"VALUES (%s, %s, %s)")
Expand All @@ -187,18 +288,39 @@ def add_playlist_songs(cursor, song, playlist, status):

if (int(cursor.rowcount) == 0):
logging.warning("Duplicate Playlist Song: " + str(dataPlaylist))
return 0


def database_input(user, spotify):
def database_input(user: str, spotify: dict):
"""
Input Songs into Database
Parameters:
user (str): User ID
spotify (dict): Dictionary Containing Song Details
Returns:
bool: Dictionary Containing Song Details
"""
with connection.cursor() as cursor:
add_artists(spotify, cursor)
add_song(spotify, cursor)
if(listening_history(user, spotify, cursor)):
if(listening_history(user, spotify, cursor) == True):
add_song_count(user, spotify, cursor)
return spotify


def playlist_input(user, spotify, playlist, status):
def playlist_input(user: str, spotify: dict, playlist: str, status: str):
"""
Input Songs from Playlist into all Database Fields
Parameters:
user (str) : User ID
spotify (dict) : Dictionary Containing Song Details
playlist (str) : Which Playlist to Insert Song Into
status (str) : local/playable/unplayable
Returns:
int: unused return
"""
with connection.cursor() as cursor:
spotify["item"] = spotify.get("track")
if(spotify.get("item").get("is_local")):
Expand All @@ -211,4 +333,4 @@ def playlist_input(user, spotify, playlist, status):
add_song(spotify, cursor)
add_song_count(user, spotify, cursor, 0)
add_playlist_songs(cursor, spotify, playlist, status)
return 1
return 0
13 changes: 11 additions & 2 deletions monitoringBackend/playlistSongs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@
log = logging.getLogger(__name__)


def main(user, playlist):
def main(user: str, playlist: str):
"""
Playlist Song Controller
Parameters:
user (str): User ID
playlist (str) : Which Playlist to Insert Song Into
Returns:
bool: unused return
"""
logging.info("Checking for Playlist Songs: " +
str(user) + " " + str(playlist))
playlist = playlist[0]
Expand All @@ -25,7 +34,7 @@ def main(user, playlist):
playlistSections = []
loop = True
response = requests.get(url, headers=header).json()
database.add_playlist(user, playlist)
database.add_playlist(playlist)
playlistSections.append(response.get("items"))
if response.get("next") == None:
loop = False
Expand Down
49 changes: 40 additions & 9 deletions monitoringBackend/scripts.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
from django.db import connection
import json
from datetime import datetime, timezone
import logging
import sys
sys.path.append("..")

log = logging.getLogger(__name__)


def songIdUpdater(user):
history = getHistory(connection, user)
def songIdUpdater(user: str):
"""
Song ID Updater Helper Function
Parameters:
user (str): User to Scan
Returns:
list: List of Changed Song IDs
"""
history = getHistory(user)
history = duplicateFinder(history)
return databaseUpdate(history, connection, user)
return databaseUpdate(history, user)


def getHistory(user: str):
"""
Get Song History of User
def getHistory(connection, user):
Parameters:
user (str): User to Scan
Returns:
list: List of Changed Song IDs
"""
with connection.cursor() as cursor:
query = 'SELECT played1.timestamp, songs.ID, songs.name, playCount.playCount, user\
FROM songs\
Expand Down Expand Up @@ -53,7 +67,15 @@ def getHistory(connection, user):
return songHistory


def duplicateFinder(history):
def duplicateFinder(history: list):
"""
Find Songs with Duplicate IDs
Parameters:
history (list): Song History of User
Returns:
set: List of Song with Duplicate IDs
"""
history.sort(key=lambda i: i[5])
newHistory = []
for i in reversed(range(0, len(history))):
Expand All @@ -78,7 +100,16 @@ def duplicateFinder(history):
return newHistory2


def databaseUpdate(history, connection, user):
def databaseUpdate(history: set, user: str):
"""
Get Song History of User
Parameters:
set: List of Song with Duplicate IDs
user (str): User to Scan
Returns:
list: List of updated songs.
"""
trimmedHistory = []
for song in history:
newPlayCount = 0
Expand Down Expand Up @@ -111,7 +142,7 @@ def databaseUpdate(history, connection, user):

def main():
# print(songIdUpdater(""))
return 1
return 0


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit 747220b

Please sign in to comment.