added 'snapshot_id' to decide whether process a playlist or not
This commit is contained in:
@@ -32,6 +32,7 @@ class Playlist(db.Model):
|
|||||||
jellyfin_id = db.Column(db.String(120), nullable=True)
|
jellyfin_id = db.Column(db.String(120), nullable=True)
|
||||||
last_updated = db.Column(db.DateTime )
|
last_updated = db.Column(db.DateTime )
|
||||||
last_changed = db.Column(db.DateTime )
|
last_changed = db.Column(db.DateTime )
|
||||||
|
snapshot_id = db.Column(db.String(120), nullable=True)
|
||||||
# Many-to-Many relationship with JellyfinUser
|
# Many-to-Many relationship with JellyfinUser
|
||||||
users = db.relationship('JellyfinUser', secondary=user_playlists, back_populates='playlists')
|
users = db.relationship('JellyfinUser', secondary=user_playlists, back_populates='playlists')
|
||||||
|
|
||||||
|
|||||||
10
app/tasks.py
10
app/tasks.py
@@ -203,14 +203,20 @@ def check_for_playlist_updates(self):
|
|||||||
processed_playlists = 0
|
processed_playlists = 0
|
||||||
|
|
||||||
for playlist in playlists:
|
for playlist in playlists:
|
||||||
app.logger.info(f'Checking updates for playlist: {playlist.name}')
|
|
||||||
playlist.last_updated = datetime.now( timezone.utc)
|
playlist.last_updated = datetime.now( timezone.utc)
|
||||||
|
sp_playlist = sp.playlist(playlist.spotify_playlist_id)
|
||||||
|
|
||||||
|
app.logger.info(f'Checking updates for playlist: {playlist.name}, s_snapshot = {sp_playlist['snapshot_id']}')
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
if sp_playlist['snapshot_id'] == playlist.snapshot_id:
|
||||||
|
app.logger.info(f'playlist: {playlist.name} , no changes detected, snapshot_id {sp_playlist['snapshot_id']}')
|
||||||
|
continue
|
||||||
try:
|
try:
|
||||||
#region Check for updates
|
#region Check for updates
|
||||||
# Fetch all playlist data from Spotify
|
# Fetch all playlist data from Spotify
|
||||||
spotify_tracks = {}
|
spotify_tracks = {}
|
||||||
offset = 0
|
offset = 0
|
||||||
|
playlist.snapshot_id = sp_playlist['snapshot_id']
|
||||||
while True:
|
while True:
|
||||||
playlist_data = sp.playlist_items(playlist.spotify_playlist_id, offset=offset, limit=100)
|
playlist_data = sp.playlist_items(playlist.spotify_playlist_id, offset=offset, limit=100)
|
||||||
items = playlist_data['items']
|
items = playlist_data['items']
|
||||||
@@ -267,7 +273,7 @@ def check_for_playlist_updates(self):
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Update Playlist Items and Metadata
|
#region Update Playlist Items and Metadata
|
||||||
functions.update_playlist_metadata(playlist, sp.playlist(playlist.spotify_playlist_id))
|
functions.update_playlist_metadata(playlist, sp_playlist)
|
||||||
ordered_tracks = db.session.execute(
|
ordered_tracks = db.session.execute(
|
||||||
db.select(Track, playlist_tracks.c.track_order)
|
db.select(Track, playlist_tracks.c.track_order)
|
||||||
.join(playlist_tracks, playlist_tracks.c.track_id == Track.id)
|
.join(playlist_tracks, playlist_tracks.c.track_id == Track.id)
|
||||||
|
|||||||
32
migrations/versions/d4fef99d5d3c_add_snapshot_id.py
Normal file
32
migrations/versions/d4fef99d5d3c_add_snapshot_id.py
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
"""Add snapshot_id
|
||||||
|
|
||||||
|
Revision ID: d4fef99d5d3c
|
||||||
|
Revises: 8fcd403f0c45
|
||||||
|
Create Date: 2024-11-22 13:00:05.192423
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = 'd4fef99d5d3c'
|
||||||
|
down_revision = '8fcd403f0c45'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
with op.batch_alter_table('playlist', schema=None) as batch_op:
|
||||||
|
batch_op.add_column(sa.Column('snapshot_id', sa.String(length=120), nullable=True))
|
||||||
|
|
||||||
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
with op.batch_alter_table('playlist', schema=None) as batch_op:
|
||||||
|
batch_op.drop_column('snapshot_id')
|
||||||
|
|
||||||
|
# ### end Alembic commands ###
|
||||||
Reference in New Issue
Block a user