Skip to content

Commit

Permalink
implementing rescan buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
blastbeng committed Oct 4, 2024
1 parent 84d013d commit b3943d1
Show file tree
Hide file tree
Showing 15 changed files with 349 additions and 130 deletions.
14 changes: 8 additions & 6 deletions spotisub/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
utils.print_logo(database.VERSION)

logging.basicConfig(
format='%(asctime)s %(levelname)-8s %(message)s',
level=int(
os.environ.get(
constants.LOG_LEVEL,
constants.LOG_LEVEL_DEFAULT_VALUE)),
datefmt='%Y-%m-%d %H:%M:%S')
handlers=[
logging.FileHandler(os.path.abspath(os.curdir) + "/cache/spotisub.log", mode="w"),
logging.StreamHandler()
],
format='%(asctime)s %(levelname)-8s %(message)s',
level=int(os.environ.get("LOG_LEVEL")),
datefmt='%Y-%m-%d %H:%M:%S')


log = logging.getLogger('werkzeug')
log.setLevel(int(os.environ.get(constants.LOG_LEVEL,
Expand Down
36 changes: 17 additions & 19 deletions spotisub/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,17 +578,17 @@ def insert_playlist_relation(conn, subsonic_song_id,
old_relation = select_playlist_relation(conn, subsonic_song_id,
subsonic_artist_id, spotify_song_uuid, pl_info_uuid)
if old_relation is None:
new_uuid = str(uuid.uuid4().hex)
stmt = insert(
dbms.subsonic_spotify_relation).values(
uuid=new_uuid,
uuid=str(uuid.uuid4().hex),
subsonic_song_id=subsonic_song_id,
subsonic_artist_id=subsonic_artist_id,
spotify_song_uuid=spotify_song_uuid,
playlist_info_uuid=pl_info_uuid)
stmt.compile()
conn.execute(stmt)
return select_playlist_relation_by_uuid(new_uuid, conn_ext=conn)
return select_playlist_relation(conn, subsonic_song_id,
subsonic_artist_id, spotify_song_uuid, pl_info_uuid)
else:
stmt = update(
dbms.subsonic_spotify_relation).where(
Expand All @@ -599,7 +599,7 @@ def insert_playlist_relation(conn, subsonic_song_id,
spotify_song_uuid=spotify_song_uuid)
stmt.compile()
conn.execute(stmt)
return select_playlist_relation_by_uuid(old_relation.uuid, conn_ext=conn)
return select_playlist_relation_by_uuid(old_relation.uuid)

def select_playlist_info_by_subsonic_id(subsonic_playlist_uuid, conn_ext=None):
"""select spotify artists by uuid"""
Expand Down Expand Up @@ -657,10 +657,10 @@ def select_playlist_relation(conn, subsonic_song_id,
return value


def select_playlist_relation_by_uuid(uuid, conn_ext=None):
def select_playlist_relation_by_uuid(uuid):
value = None
"""select playlist relation"""
with conn_ext if conn_ext is not None else dbms.db_engine.connect() as conn:
with dbms.db_engine.connect() as conn:
stmt = select(dbms.subsonic_spotify_relation.c.uuid,
dbms.subsonic_spotify_relation.c.subsonic_song_id,
dbms.subsonic_spotify_relation.c.subsonic_artist_id,
Expand All @@ -676,8 +676,6 @@ def select_playlist_relation_by_uuid(uuid, conn_ext=None):
for row in records:
value = row
cursor.close()
if conn_ext is None:
conn.close()

return value

Expand Down Expand Up @@ -713,20 +711,20 @@ def select_all_songs(conn_ext=None, missing_only=False, page=None,
'spotify_artist_ignored'),
dbms.spotify_song.c.tms_insert)
stmt = stmt.join(
dbms.playlist_info,
dbms.playlist_info.c.uuid == dbms.subsonic_spotify_relation.c.playlist_info_uuid)
dbms.subsonic_spotify_relation,
dbms.subsonic_spotify_relation.c.playlist_info_uuid == dbms.playlist_info.c.uuid, isouter=True)
stmt = stmt.join(
dbms.spotify_song,
dbms.subsonic_spotify_relation.c.spotify_song_uuid == dbms.spotify_song.c.uuid)
dbms.subsonic_spotify_relation.c.spotify_song_uuid == dbms.spotify_song.c.uuid, isouter=True)
stmt = stmt.join(
dbms.spotify_album,
dbms.spotify_song.c.album_uuid == dbms.spotify_album.c.uuid)
dbms.spotify_song.c.album_uuid == dbms.spotify_album.c.uuid, isouter=True)
stmt = stmt.join(
dbms.spotify_song_artist_relation,
dbms.spotify_song.c.uuid == dbms.spotify_song_artist_relation.c.song_uuid)
dbms.spotify_song.c.uuid == dbms.spotify_song_artist_relation.c.song_uuid, isouter=True)
stmt = stmt.join(
dbms.spotify_artist,
dbms.spotify_song_artist_relation.c.artist_uuid == dbms.spotify_artist.c.uuid)
dbms.spotify_song_artist_relation.c.artist_uuid == dbms.spotify_artist.c.uuid, isouter=True)
if missing_only:
stmt = stmt.where(
dbms.subsonic_spotify_relation.c.subsonic_song_id == None,
Expand Down Expand Up @@ -786,11 +784,11 @@ def count_songs(conn, missing_only=False, search=None, song_uuid=None, playlist_
group_concat(spotify_artist.name) AS spotify_artist_names,
group_concat(spotify_artist.uuid) AS spotify_artist_uuids,
spotify_song.tms_insert FROM playlist_info
JOIN subsonic_spotify_relation ON playlist_info.uuid = subsonic_spotify_relation.playlist_info_uuid
JOIN spotify_song ON subsonic_spotify_relation.spotify_song_uuid = spotify_song.uuid
JOIN spotify_album ON spotify_song.album_uuid = spotify_album.uuid
JOIN spotify_song_artist_relation ON spotify_song.uuid = spotify_song_artist_relation.song_uuid
JOIN spotify_artist ON spotify_song_artist_relation.artist_uuid = spotify_artist.uuid """
LEFT OUTER JOIN subsonic_spotify_relation ON playlist_info.uuid = subsonic_spotify_relation.playlist_info_uuid
LEFT OUTER JOIN spotify_song ON subsonic_spotify_relation.spotify_song_uuid = spotify_song.uuid
LEFT OUTER JOIN spotify_album ON spotify_song.album_uuid = spotify_album.uuid
LEFT OUTER JOIN spotify_song_artist_relation ON spotify_song.uuid = spotify_song_artist_relation.song_uuid
LEFT OUTER JOIN spotify_artist ON spotify_song_artist_relation.artist_uuid = spotify_artist.uuid """
where = ""
if missing_only:
where = where + """ subsonic_spotify_relation.subsonic_song_id is null
Expand Down
22 changes: 11 additions & 11 deletions spotisub/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ def artist_top_tracks(uuid):

def artist_top_tracks_run(uuid):
"""artist top tracks"""
playlist_info_db = select_playlist_info_by_uuid(uuid)
playlist_info_db = database.select_playlist_info_by_uuid(uuid)
if playlist_info_db is not None and playlist_info_db.uuid is not None:
artist = get_artist(playlist_info.import_arg)
artist = get_artist(playlist_info_db.import_arg)
if artist is not None and "uri" in artist and artist["uri"] is not None:
playlist_name = playlist_info_db.name + " - Top Tracks"
playlist_info = {}
Expand Down Expand Up @@ -239,9 +239,9 @@ def show_recommendations_for_artist(uuid):
def show_recommendations_for_artist_run(uuid):
"""show recommendations for artist"""

playlist_info_db = select_playlist_info_by_uuid(uuid)
playlist_info_db = database.select_playlist_info_by_uuid(uuid)
if playlist_info_db is not None and playlist_info_db.uuid is not None:
artist = get_artist(playlist_info.import_arg)
artist = get_artist(playlist_info_db.import_arg)
if artist is not None and "uri" in artist and artist["uri"] is not None:
playlist_name = playlist_info_db.name + " - Recommendations"
playlist_info = {}
Expand Down Expand Up @@ -291,7 +291,7 @@ def my_recommendations(uuid):

def my_recommendations_run(uuid):
"""my recommendations"""
playlist_info_db = select_playlist_info_by_uuid(uuid)
playlist_info_db = database.select_playlist_info_by_uuid(uuid)
if playlist_info_db is not None and playlist_info_db.uuid is not None:
playlist_num = int(playlist_info_db.import_arg)
sp = spotipy_helper.get_spotipy_client()
Expand Down Expand Up @@ -359,7 +359,7 @@ def get_user_saved_tracks(uuid):

def get_user_saved_tracks_run(uuid):
"""get user saved tracks run"""
playlist_info_db = select_playlist_info_by_uuid(uuid)
playlist_info_db = database.select_playlist_info_by_uuid(uuid)
if playlist_info_db is not None and playlist_info_db.uuid is not None:
playlist_info = {}
playlist_info["uuid"] = playlist_info_db.uuid
Expand All @@ -382,7 +382,7 @@ def get_user_playlists(uuid):

def get_user_playlists_run(uuid, offset=0):
"""get user playlists"""
playlist_info_db = select_playlist_info_by_uuid(uuid)
playlist_info_db = database.select_playlist_info_by_uuid(uuid)
if playlist_info_db is not None and playlist_info_db.uuid is not None:

sp = spotipy_helper.get_spotipy_client()
Expand All @@ -391,8 +391,8 @@ def get_user_playlists_run(uuid, offset=0):
limit=50, offset=offset)

for item in playlist_result['items']:
if item['name'] is not None and item['name'].strip() != '' and (playlist_info_db.name is None or (
playlist_info_db.name is not None and item['name'].lower().strip() == playlist_info_db.name.lower().strip())):
if item['name'] is not None and item['name'].strip() != '' and (playlist_info_db.import_arg is None or (
playlist_info_db.import_arg is not None and item['name'].lower().strip() == playlist_info_db.import_arg.lower().strip())):
playlist_info = {}
playlist_info["uuid"] = playlist_info_db.uuid
playlist_info["name"] = item['name'].strip()
Expand All @@ -407,7 +407,7 @@ def get_user_playlists_run(uuid, offset=0):


if len(playlist_result['items']) != 0:
get_user_playlists(playlist_name, offset=offset + 50)
get_user_playlists_run(uuid, offset=offset + 50)

if os.environ.get(constants.PLAYLIST_GEN_SCHED, constants.PLAYLIST_GEN_SCHED_DEFAULT_VALUE) == "0":
scheduler.remove_job(id=constants.JOB_UP_ID)
Expand All @@ -416,7 +416,7 @@ def get_user_playlists_run(uuid, offset=0):
if len(playlists) > 0 and os.environ.get(constants.PLAYLIST_GEN_SCHED, constants.PLAYLIST_GEN_SCHED_DEFAULT_VALUE) != "0":
item = random.choice(playlists)
scheduler.modify_job(
args=item["name"],
args=[item.uuid],
id=constants.JOB_UP_ID
)
else:
Expand Down
5 changes: 0 additions & 5 deletions spotisub/helpers/subsonic_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,11 +560,6 @@ def select_all_playlists(spotipy_helper, page=None,
spotify_playlist = get_spotify_object_from_cache(spotipy_helper.get_spotipy_client(), playlist["spotify_playlist_uri"])
if spotify_playlist is not None and "images" in spotify_playlist and spotify_playlist["images"] is not None and len(spotify_playlist["images"]) > 0:
playlist["image"] = spotify_playlist["images"][0]["url"]

if playlist["type"] == constants.JOB_MR_ID:
playlist["subsonic_playlist_name"] = "My Recommendations " + playlist["import_arg"]
else:
playlist["subsonic_playlist_name"] = playlist["import_arg"]

for plid in ids:
playlist_search, has_been_deleted = get_playlist_from_cache(
Expand Down
23 changes: 23 additions & 0 deletions spotisub/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import threading
import json
import math
from time import sleep
from time import strftime
from requests import ConnectionError
from requests import ReadTimeout
Expand Down Expand Up @@ -341,6 +342,28 @@ def tasks():
return render_template('tasks.html',
title=title)

@spotisub.route('/logs')
@login_required
def logs():
title = 'Logs'
return render_template('logs.html',
title=title)

@spotisub.route('/streamlog')
@login_required
def streamlog():
def generate():
with open(os.path.abspath(os.curdir) + '/cache/spotisub.log') as f:
while True:
yield f.read()
sleep(1)

r = Response(response=generate(), status=200, mimetype="text/plain")
r.headers["Content-Type"] = "text/plain; charset=utf-8"
r.headers["Accept"] = "text/plain"
return r


@spotisub.route('/poll_playlist/<string:uuid>/')
@login_required
def poll_playlist(uuid=None):
Expand Down
34 changes: 33 additions & 1 deletion spotisub/static/css/light.css
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,16 @@ a.article-active:hover {
-moz-animation:spin 4s linear infinite;
animation:spin 4s linear infinite;
}
.svg-fa-spin-loading {
overflow: visible;
box-sizing: content-box;
margin-top:0px;
margin-left:0px;
font-size:30px;
-webkit-animation:spin 4s linear infinite;
-moz-animation:spin 4s linear infinite;
animation:spin 4s linear infinite;
}
@-moz-keyframes spin {
100% { -moz-transform: rotate(360deg); }
}
Expand Down Expand Up @@ -656,7 +666,7 @@ progress[value]::-webkit-progress-value {
line-height: 20px;
position: absolute;
max-height: 352px;
width: 200px;
width: 231px;
will-change: transform;
top: 108px;
right: 20px;
Expand All @@ -665,6 +675,28 @@ progress[value]::-webkit-progress-value {
border: 1px solid;
border-color: black;
}

.MenuContent-menuContent-rescan {
z-index: 2000;
display: flex;
flex-direction: column;
background-color: #6d7fcc;
line-height: 20px;
position: absolute;
max-height: 352px;
width: 400px;
will-change: transform;
top: 108px;
left: 270px;
color: white;

border: 1px solid;
border-color: black;
}

.span-rescan{
padding: 10px;
}

.Scroller-vertical.Scroller-autoScroll {
overflow-y: auto;
Expand Down
13 changes: 13 additions & 0 deletions spotisub/static/js/logs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function pollLogsJob(url){
let xhr = new XMLHttpRequest();

xhr.open("GET", url, true);

xhr.onreadystatechange = function () {
var output = document.getElementById('output-log');
if (this.readyState == 4 && this.status == 200) {
output.textContent = xhr.responseText;
}
}
xhr.send();
}
26 changes: 25 additions & 1 deletion spotisub/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,28 @@ function callUrl(url){
}
}
xhr.send();
}
}



window.addEventListener('click', function(e){
if (e.target.id !== 'show-toolbar-popup') {
var element1 = document.getElementById("toolbar-root-sort");
if (element1 != null && element1 !== 'undefined' && !element1.contains(e.target) && !element1.classList.contains("nodisplay") ) {
element1.classList.add("nodisplay");
}
var element2 = document.getElementById("toolbar-root-view");
if (element2 != null && element2 !== 'undefined' && !element2.contains(e.target) && !element2.classList.contains("nodisplay") ) {
element2.classList.add("nodisplay");
}
var element3 = document.getElementById("toolbar-root-filter");
if (element3 != null && element3 !== 'undefined' && !element3.contains(e.target) && !element3.classList.contains("nodisplay") ) {
element3.classList.add("nodisplay");
}
var element4 = document.getElementById("toolbar-root-rescan");
if (element4 != null && element3 !== 'undefined' && !element3.contains(e.target) && !element4.classList.contains("nodisplay") ) {
element4.classList.add("nodisplay");
}

}
});
Loading

0 comments on commit b3943d1

Please sign in to comment.