Skip to content

Commit

Permalink
Merge pull request #265 from kozaka-tv/164-playlist-should-be-a-speci…
Browse files Browse the repository at this point in the history
…al-dictionary-with-all-the-information-included-from-rspl

164 PR: Playlist should be a special dictionary with all the information included from RSPL
  • Loading branch information
kozaka-tv authored Jun 30, 2024
2 parents d5a86b1 + 82aad6a commit 1903db6
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 50 deletions.
5 changes: 3 additions & 2 deletions config/config_data.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import dataclasses
import os

from utils import rs_playlist, string_utils
from utils import string_utils
from utils.exceptions import ConfigError
from utils.rs_playlist_util import get_viewers

SECTION_ROCK_SNIFFER = "RockSniffer"
SECTION_SETLIST_LOGGER = "SetlistLogger"
Expand Down Expand Up @@ -115,7 +116,7 @@ def validate_and_get_phpsessid(conf, twitch_channel):
phpsessid_set = conf.get_set(SECTION_SONG_LOADER, "phpsessid")

for phpsessid in phpsessid_set:
viewers = rs_playlist.get_viewers(twitch_channel, phpsessid)
viewers = get_viewers(twitch_channel, phpsessid)
if viewers.get("result") != "Error":
return phpsessid

Expand Down
63 changes: 63 additions & 0 deletions modules/song_loader/rs_playlist_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from dataclasses import dataclass
from typing import List, Dict, Optional


# pylint: disable=too-many-instance-attributes
# The structure of this data class is defined in RSPL, so we have to disable the pylint check.

@dataclass
class Viewer:
username: str
twitch_id: int
badges: List[str]
present: bool
inactive: bool
inactive_time: int


@dataclass
class DlcSet:
id: int
cdlc_id: int
artist: str
artist_id: int
title: str
album: str
tuning: int
parts: Optional[str]
dd: bool
official: int
creator: str
estimated_length: int
trr_url: Optional[str]
updated: int
downloads: int
tuning_name: str
paths: int
paths_string: str


@dataclass
class PlaylistItem:
id: int
vip: bool
position: int
string: str
tags: List[str]
request_timestamp: int
viewer: Viewer
dlc_set: List[DlcSet]


@dataclass
class ChannelTag:
name: str
icon: str
color: str
user: bool


@dataclass
class RsPlaylist:
playlist: List[PlaylistItem]
channel_tags: Dict[str, ChannelTag]
98 changes: 54 additions & 44 deletions modules/song_loader/song_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@
import os
from time import time

from dacite import from_dict

from config.config_data import ConfigData
from definitions import PSARC_INFO_FILE_CACHE_DIR, TMP_DIR
from modules.database.db_manager import DBManager
from modules.song_loader.rs_playlist_data import RsPlaylist
from modules.song_loader.song_data import SongData
from modules.song_loader.song_loader_helper import playlist_does_not_changed, check_cdlc_archive_dir, \
check_rocksmith_cdlc_dir, update_tags_in_song_data, is_official, log_new_songs_found
from utils import file_utils, rs_playlist, psarc_reader
from utils import file_utils, psarc_reader
from utils.collection_utils import is_not_empty, is_empty, repr_in_multi_line
from utils.exceptions import BadDirectoryError
from utils.exceptions import RSPLNotLoggedInError
from utils.rs_playlist import get_playlist, user_is_not_logged_in
from utils.rs_playlist_util import get_playlist, user_is_not_logged_in, set_tag_to_download, set_tag_loaded
from utils.string_utils import time_float_to_string

HEARTBEAT = 5
Expand All @@ -26,9 +29,12 @@ def __init__(self, config_data: ConfigData, songs):
if self.enabled:
self.twitch_channel = config_data.song_loader.twitch_channel
self.phpsessid = config_data.song_loader.phpsessid

self.rsplaylist = None
self.rsplaylist_json = None
self.rsplaylist_updated = True
self.rspl_tags = config_data.song_loader.rspl_tags

self.cdlc_archive_dir = check_cdlc_archive_dir(config_data.song_loader.cdlc_archive_dir)
self.destination_dir = config_data.song_loader.destination_dir
self.download_dirs = config_data.file_manager.download_dirs
Expand Down Expand Up @@ -80,7 +86,7 @@ def run(self):

self.__update_songs_from_rs_dir()

if self.playlist_has_been_changed():
if self.__playlist_has_been_changed():
log.info("Playlist has been changed, update songs!")

self.__find_existing_song_filenames_from_db_according_to_the_requests()
Expand Down Expand Up @@ -108,33 +114,37 @@ def __update_songs_from_rs_dir(self):
# def __update_songs_from_tmp_dir(self):
# self.__store_and_return_all_the_new_song_datas(self.destination_dir, self.songs.songs_in_tmp)

def playlist_has_been_changed(self):
def __playlist_has_been_changed(self):
new_playlist = get_playlist(self.twitch_channel, self.phpsessid)

self.__stop_if_user_is_not_logged_in_on_rspl_page(new_playlist)

if self.rsplaylist is None:
self.rsplaylist = new_playlist
if self.rsplaylist_json is None:
self.__update_playlist_with(new_playlist)
log.info("Initial load of the playlist is done...")
return True

if playlist_does_not_changed(self.rsplaylist, new_playlist):
if playlist_does_not_changed(self.rsplaylist_json, new_playlist):
return False

log.info("Playlist has been changed, lets update!")

self.rsplaylist = new_playlist
self.__update_playlist_with(new_playlist)

return True

def __update_playlist_with(self, new_playlist):
self.rsplaylist_json = new_playlist
self.rsplaylist = from_dict(data_class=RsPlaylist, data=new_playlist)

def __stop_if_user_is_not_logged_in_on_rspl_page(self, new_playlist):
not_logged_in = user_is_not_logged_in(new_playlist)

if not_logged_in is None:
log.debug("Playlist is empty! Can not tell, that the user is logged in or not!")

elif not_logged_in:
self.rsplaylist = None
self.rsplaylist_json = None
self.last_run = time()
log.error("User is not logged in! Please log in on RSPL!")
raise RSPLNotLoggedInError
Expand Down Expand Up @@ -273,21 +283,21 @@ def __move_requested_cdlc_files_from_archive_to_rs(self):
self.songs.moved_from_archive.update(
{filename: self.songs.songs_from_archive_has_to_be_moved.pop(filename)})
actually_loaded_songs.add(filename)
rs_playlist.set_tag_loaded(self.twitch_channel,
self.phpsessid,
song_data.rspl_request_id,
self.rspl_tags)
set_tag_loaded(self.twitch_channel,
self.phpsessid,
song_data.rspl_request_id,
self.rspl_tags)
else:
log.error("Could not move file! Song exists in DB, but there is no file in archive: %s",
song_to_move)
self.songs.missing_from_archive.update(
{filename: self.songs.songs_from_archive_has_to_be_moved.pop(filename)})

if self.__has_no_tag_to_download(song_data):
rs_playlist.set_tag_to_download(self.twitch_channel,
self.phpsessid,
song_data.rspl_request_id,
self.rspl_tags)
set_tag_to_download(self.twitch_channel,
self.phpsessid,
song_data.rspl_request_id,
self.rspl_tags)

if is_not_empty(self.songs.moved_from_archive):
log.warning("---- Files newly moved and will be parsed: %s", str(actually_loaded_songs))
Expand All @@ -303,45 +313,45 @@ def __has_no_tag_loaded(self, song_data):
def __has_no_tag_to_download(self, song_data):
return self.rspl_tags.tag_to_download not in song_data.tags

def __do_not_has_the_tag_to_download(self, sr):
return self.rspl_tags.tag_to_download not in sr["tags"]
def __do_not_has_the_tag_to_download(self, playlist_item):
return self.rspl_tags.tag_to_download not in playlist_item.tags

def __find_existing_song_filenames_from_db_according_to_the_requests(self):
for sr in self.rsplaylist["playlist"]:
rspl_request_id = sr['id']
for dlc_set in sr["dlc_set"]:
rspl_song_id = dlc_set['id']
cdlc_id = dlc_set["cdlc_id"]
artist = dlc_set["artist"]
title = dlc_set["title"]
official = dlc_set["official"]
rspl_position = str(sr["position"])

playlist = self.rsplaylist.playlist
for playlist_item in playlist:
for dlc_set_item in playlist_item.dlc_set:
cdlc_id = dlc_set_item.cdlc_id
artist = dlc_set_item.artist
title = dlc_set_item.title

official = dlc_set_item.official
if is_official(official):
log.info("Skipping ODLC request with cdlc_id=%s - %s - %s", cdlc_id, artist, title)
continue

songs_in_the_db = self.db_manager.search_song_by_artist_and_title(artist, title)

rspl_position = str(playlist_item.position)

if is_empty(songs_in_the_db):
if self.__do_not_has_the_tag_to_download(sr):
if self.__do_not_has_the_tag_to_download(playlist_item):
log.warning("User must download the song: rspl_position=%s cdlc_id=%s - %s - %s",
rspl_position, cdlc_id, artist, title)
rs_playlist.set_tag_to_download(self.twitch_channel,
self.phpsessid,
rspl_request_id,
self.rspl_tags)
set_tag_to_download(self.twitch_channel,
self.phpsessid,
playlist_item.id,
self.rspl_tags)

log.info("Tag is set! rspl_position=%s cdlc_id=%s - %s - %s",
rspl_position, cdlc_id, artist, title)
continue

for song_filename in songs_in_the_db:
song_data = SongData(rspl_request_id, cdlc_id, rspl_song_id, artist, title, song_filename)
song_data = SongData(playlist_item.id, cdlc_id, dlc_set_item.id, artist, title, song_filename)
song_data.rspl_official = official
song_data.rspl_position = rspl_position

update_tags_in_song_data(song_data, sr)
update_tags_in_song_data(song_data, playlist_item)

song_data.rspl_official = official
song_data.rspl_position = rspl_position
Expand All @@ -364,10 +374,10 @@ def __calculate_songs_need_to_be_moved_from_archive_to_under_rs(self):
if filename in filenames_from_rs_dir:
log.debug("Already loaded song: %s", song_data)
if self.__has_no_tag_loaded(song_data):
rs_playlist.set_tag_loaded(self.twitch_channel,
self.phpsessid,
song_data.rspl_request_id,
self.rspl_tags)
set_tag_loaded(self.twitch_channel,
self.phpsessid,
song_data.rspl_request_id,
self.rspl_tags)

elif filename in difference:
self.songs.songs_from_archive_has_to_be_moved.update({filename: song_data})
Expand All @@ -377,8 +387,8 @@ def __calculate_songs_need_to_be_moved_from_archive_to_under_rs(self):
repr_in_multi_line(self.songs.songs_from_archive_has_to_be_moved))

def __set_tag_loaded(self, song_data):
rs_playlist.set_tag_loaded(self.twitch_channel,
self.phpsessid,
song_data.rspl_request_id,
self.rspl_tags)
set_tag_loaded(self.twitch_channel,
self.phpsessid,
song_data.rspl_request_id,
self.rspl_tags)
song_data.tags.add(self.rspl_tags.tag_loaded)
4 changes: 2 additions & 2 deletions modules/song_loader/song_loader_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ def playlist_does_not_changed(old_playlist, new_playlist):
return False


def update_tags_in_song_data(song_data, sr):
def update_tags_in_song_data(song_data, playlist_item):
song_data.tags.clear()
for tag in sr['tags']:
for tag in playlist_item.tags:
song_data.tags.add(tag)


Expand Down
5 changes: 3 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ twine==5.1.0
ordered-set
zstandard==0.22.0
pycryptodome==3.20.0
dacite

pytest==8.2.1
pylint==3.2.2
pytest==8.2.2
pylint==3.2.3

fuzzywuzzy~=0.18.0
watchdog==4.0.1
File renamed without changes.

0 comments on commit 1903db6

Please sign in to comment.