Skip to content

Commit

Permalink
Merge branch 'monitor-artist-playlist' into beta
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalec committed Nov 10, 2022
2 parents a8e03b2 + edf090f commit 76b6d07
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 26 deletions.
4 changes: 2 additions & 2 deletions deemon/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env python3
from deemon.utils import startup

__version__ = '2.18b2'
__dbversion__ = '3.6'
__version__ = '2.18b3'
__dbversion__ = '3.7'

appdata = startup.get_appdata_dir()
startup.init_appdata_dir(appdata)
5 changes: 3 additions & 2 deletions deemon/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,13 @@ def download_command(artist, artist_id, album_id, url, file, bitrate,
@click.option('-I', '--import', 'im', metavar="PATH", help="Monitor artists/IDs from file or directory")
@click.option('-i', '--artist-id', is_flag=True, help="Monitor artist by ID")
@click.option('-p', '--playlist', is_flag=True, help='Monitor Deezer playlist by URL')
@click.option('--include-artists', is_flag=True, help='Also monitor artists from playlist')
@click.option('-u', '--url', is_flag=True, help='Monitor artist by URL')
@click.option('-R', '--remove', is_flag=True, help='Stop monitoring an artist')
@click.option('-s', '--search', 'search_flag', is_flag=True, help='Show similar artist results to choose from')
@click.option('-T', '--time-machine', type=str, metavar="YYYY-MM-DD", help="Refresh newly added artists on this date")
@click.option('-t', '--record-type', metavar="TYPE", type=str, help='Specify record types to download')
def monitor_command(artist, im, playlist, bitrate, record_type, alerts, artist_id,
def monitor_command(artist, im, playlist, include_artists, bitrate, record_type, alerts, artist_id,
dl, remove, url, download_path, search_flag, time_machine):
"""
Monitor artist for new releases by ID, URL or name.
Expand Down Expand Up @@ -256,7 +257,7 @@ def monitor_command(artist, im, playlist, bitrate, record_type, alerts, artist_i
if im:
monitor.importer(im)
elif playlist:
monitor.playlists(playlist_id)
monitor.playlists(playlist_id, include_artists)
elif artist_id:
monitor.artist_ids(dataprocessor.csv_to_list(artist))
elif artist:
Expand Down
22 changes: 17 additions & 5 deletions deemon/cmd/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ def build_artist_query(self, api_result: list):
self.db.commit()
return True

def build_playlist_query(self, api_result: list):
def build_playlist_query(self, api_result: list, include_artists: bool):

if include_artists:
include_artists = '1'

existing = self.db.get_all_monitored_playlist_ids() or []
playlists_to_add = []
pbar = tqdm(api_result, total=len(api_result), desc="Setting up playlists for monitoring...", ascii=" #",
Expand All @@ -134,8 +138,16 @@ def build_playlist_query(self, api_result: list):
if playlist['id'] in existing:
logger.info(f" Already monitoring {playlist['title']}, skipping...")
else:
playlist.update({'bitrate': self.bitrate, 'alerts': self.alerts, 'download_path': self.download_path,
'profile_id': config.profile_id(), 'trans_id': config.transaction_id()})
playlist.update(
{
'bitrate': self.bitrate,
'alerts': self.alerts,
'download_path': self.download_path,
'profile_id': config.profile_id(),
'trans_id': config.transaction_id(),
'monitor_artists': include_artists
}
)
playlists_to_add.append(playlist)
if len(playlists_to_add):
logger.debug("New playlists have been monitored. Saving changes to the database...")
Expand Down Expand Up @@ -214,7 +226,7 @@ def importer(self, import_path: str):
return

# @performance.timeit
def playlists(self, playlists: list):
def playlists(self, playlists: list, include_artists: bool):
if self.remove:
return self.purge_playlists(ids=playlists)
ids = [int(x) for x in playlists]
Expand All @@ -225,7 +237,7 @@ def playlists(self, playlists: list):
desc=f"Fetching playlist data for {len(ids):,} playlist(s), please wait...",
ascii=" #", bar_format=ui.TQDM_FORMAT))

if self.build_playlist_query(api_result):
if self.build_playlist_query(api_result, include_artists):
self.call_refresh()
else:
print("")
Expand Down
17 changes: 16 additions & 1 deletion deemon/cmd/refresh.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import re
import time
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime, timedelta

Expand Down Expand Up @@ -248,12 +249,19 @@ def run(self, artists: list = None, playlists: list = None):
for payload in payload_container:
self.prep_payload(payload)

playlist_monitor_artists = []
for payload in api_result['playlists']:
if len(payload):
self.seen = self.db.get_playlist_tracks(payload['id'])
payload['tracks'] = self.remove_existing_releases(payload, self.seen)
self.filter_playlist_releases(payload)

if payload['monitor_artists']:
logger.debug(f"Artists from this playlist ({payload['id']}) are to be monitored!")
for track in payload['tracks']:
playlist_monitor_artists.append(track['artist_id'])
playlist_monitor_artists = list(set(playlist_monitor_artists))

if self.skip_download:
logger.info(f" [!] You have opted to skip downloads, clearing {len(self.queue_list):,} item(s) from queue...")
self.queue_list.clear()
Expand All @@ -263,7 +271,6 @@ def run(self, artists: list = None, playlists: list = None):
dl = Download()
dl.download_queue(self.queue_list)


if len(self.new_playlist_releases) or len(self.new_releases):
if len(self.new_playlist_releases):
logger.debug("Updating playlist releases in database...")
Expand All @@ -284,6 +291,14 @@ def run(self, artists: list = None, playlists: list = None):
notification = notifier.Notify(self.new_releases_alert)
notification.send()

if playlist_monitor_artists:
print("")
logger.info(":: New artists to monitor, stand by...")
time.sleep(2)
from deemon.cmd.monitor import Monitor
monitor = Monitor()
monitor.artist_ids(playlist_monitor_artists)

def db_stats(self):
artists = len(self.db.get_all_monitored_artist_ids())
playlists = len(self.db.get_all_monitored_playlist_ids())
Expand Down
54 changes: 38 additions & 16 deletions deemon/core/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ def create_new_database(self):
"'profile_id' INTEGER DEFAULT 1,"
"'download_path' TEXT,"
"'refreshed' INTEGER DEFAULT 0,"
"'trans_id' INTEGER)")
"'trans_id' INTEGER,"
"'monitor_artists' INTEGER DEFAULT 0)")

self.query("CREATE TABLE playlist_tracks ("
"'track_id' INTEGER,"
Expand Down Expand Up @@ -169,19 +170,19 @@ def do_upgrade(self):

if current_ver < parse_version("3.5.2"):
self.query("CREATE TABLE releases_tmp ("
"'artist_id' INTEGER,"
"'artist_name' TEXT,"
"'album_id' INTEGER,"
"'album_name' TEXT,"
"'album_release' TEXT,"
"'album_added' INTEGER,"
"'explicit' INTEGER,"
"'label' TEXT,"
"'record_type' INTEGER,"
"'profile_id' INTEGER DEFAULT 1,"
"'future_release' INTEGER DEFAULT 0,"
"'trans_id' INTEGER,"
"unique(album_id, profile_id))")
"'artist_id' INTEGER,"
"'artist_name' TEXT,"
"'album_id' INTEGER,"
"'album_name' TEXT,"
"'album_release' TEXT,"
"'album_added' INTEGER,"
"'explicit' INTEGER,"
"'label' TEXT,"
"'record_type' INTEGER,"
"'profile_id' INTEGER DEFAULT 1,"
"'future_release' INTEGER DEFAULT 0,"
"'trans_id' INTEGER,"
"unique(album_id, profile_id))")
self.query("INSERT OR REPLACE INTO releases_tmp(artist_id, artist_name, "
"album_id, album_name, album_release, album_added, "
"explicit, label, record_type, profile_id, "
Expand All @@ -193,12 +194,33 @@ def do_upgrade(self):
self.query("ALTER TABLE releases_tmp RENAME TO releases")
self.query("INSERT OR REPLACE INTO 'deemon' ('property', 'value') VALUES ('version', '3.5.2')")
self.commit()
logger.debug(f"Database upgraded to version 3.5.2")

if current_ver < parse_version("3.6"):
# album_release_ts REMOVED
pass

if current_ver < parse_version("3.7"):
self.query("CREATE TABLE playlists_tmp ("
"'id' INTEGER UNIQUE,"
"'title' TEXT,"
"'url' TEXT,"
"'bitrate' TEXT,"
"'alerts' INTEGER,"
"'profile_id' INTEGER DEFAULT 1,"
"'download_path' TEXT,"
"'refreshed' INTEGER DEFAULT 0,"
"'trans_id' INTEGER,"
"'monitor_artists' INTEGER DEFAULT 0)")
self.query("INSERT OR REPLACE INTO playlists_tmp (id, title, url, bitrate, "
"alerts, profile_id, download_path, refreshed, trans_id) SELECT "
"id, title, url, bitrate, alerts, profile_id, download_path, "
"refreshed, trans_id FROM playlists")
self.query("DROP TABLE playlists")
self.query("ALTER TABLE playlists_tmp RENAME TO playlists")
self.query("INSERT OR REPLACE INTO 'deemon' ('property', 'value') VALUES ('version', '3.7')")
self.commit()
logger.debug(f"Database upgraded to version 3.7")

def query(self, query, values=None):
if values is None:
values = {}
Expand Down Expand Up @@ -530,7 +552,7 @@ def fast_monitor(self, values):

def fast_monitor_playlist(self, values):
self.cursor.executemany(
"INSERT OR REPLACE INTO playlists (id, title, url, bitrate, alerts, profile_id, download_path, trans_id) VALUES (:id, :title, :link, :bitrate, :alerts, :profile_id, :download_path, :trans_id)",
"INSERT OR REPLACE INTO playlists (id, title, url, bitrate, alerts, profile_id, download_path, trans_id, monitor_artists) VALUES (:id, :title, :link, :bitrate, :alerts, :profile_id, :download_path, :trans_id, :monitor_artists)",
values)

def insert_multiple(self, table, values):
Expand Down

0 comments on commit 76b6d07

Please sign in to comment.