From 036e981fe2a7bc824b53ebf3f58e777ab439db5b Mon Sep 17 00:00:00 2001
From: Luca Bilke <bilke@tralios.de>
Date: Fri, 30 Jun 2023 12:55:57 +0200
Subject: [PATCH] fix extensions, compatibility for python < 3.11

---
 .gitignore                |  4 +---
 TODO.md                   |  3 +++
 conf.toml                 | 11 +++++++----
 requirements.txt          |  1 +
 tidal-scraper/download.py | 17 ++++++++++++-----
 tidal-scraper/helper.py   | 13 +++++++++----
 tidal-scraper/run.py      |  2 +-
 7 files changed, 34 insertions(+), 17 deletions(-)
 mode change 100644 => 100755 tidal-scraper/run.py

diff --git a/.gitignore b/.gitignore
index 9c03f71..39b1e22 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1 @@
-auth.json
-
-tidal-scraper/__pycache__
\ No newline at end of file
+tidal-scraper/__pycache__
diff --git a/TODO.md b/TODO.md
index 1138ed9..1e75b99 100644
--- a/TODO.md
+++ b/TODO.md
@@ -2,3 +2,6 @@
 - [ ] installer should create state and config homes if not existing
 - [ ] proper SIGTERM handling
 - [ ] decrypt and write in chunks
+- [ ] test error logger
+
+- [ ] Switch to tomllib once ubuntu updates their python package to 3.11 (I haven't switched yet to avoid github issues being made)
diff --git a/conf.toml b/conf.toml
index fab14cb..5815b6a 100644
--- a/conf.toml
+++ b/conf.toml
@@ -7,14 +7,17 @@ quality = "lossless"
 user_id =
 
 dest_dir = "./downloads/"
-# The following templates are passed an artist, album and track object.
+
+# These templates are passed their respective tidalapi objects
 # Possible attributes can be found here: https://tidalapi.netlify.app/api.html
-# The artist is derived from the album a track is in rather than the track itself.
-album_dir = "{artist.name}/{album.name}/"
+album_dir = "{album.artist.name}/{album.name}/"
 playlist_dir = "{playlist.name}/"
-track_name = "{track.track_num}: {track.name}"
+# Rather than receiving an artist, the track receives both "albumartist" and a "trackartist"
+track_name = "{track.track_num}: {track.artist.name} - {track.name}"
 
 # One of 160, 320, 480, 640, 750, 1080
 playlist_image_size = 1080
 # One of 80, 160, 320, 640, 1280
 album_image_size = 1280
+
+debug = false
diff --git a/requirements.txt b/requirements.txt
index 162be56..7ef0196 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -2,3 +2,4 @@ git+https://github.com/tamland/python-tidal
 pycrypto
 tqdm
 mutagen
+toml
diff --git a/tidal-scraper/download.py b/tidal-scraper/download.py
index 4e59683..51eedb8 100644
--- a/tidal-scraper/download.py
+++ b/tidal-scraper/download.py
@@ -60,10 +60,14 @@ def download_track(track: tidalapi.Track, dest: str) -> None:
     album = track.album
     assert album
     print(f"Starting {album.artist.name} - {track.name}")
-    dest += clean_template(conf["track_name"], track=track)
+    dest += clean_template(
+        conf["track_name"],
+        track=track,
+    )
     http_failures = 0
     while http_failures <= 3:
         try:
+            print("running")
             stream = track.stream()
             manifest = json.loads(b64decode(stream.manifest))
             if conf["debug"]:
@@ -76,8 +80,10 @@ def download_track(track: tidalapi.Track, dest: str) -> None:
                 else:
                     dest += ".m4a"
             else:
-                for ext in (x for x in extensions if x != ".mp4"):
-                    dest += ext
+                for ext in extensions:
+                    if ext in url:
+                        dest += ext
+                        break
             if os.path.exists(dest) and conf["skip_downloaded"]:
                 print(f"Skipping track")
                 return
@@ -105,7 +111,9 @@ def download_track(track: tidalapi.Track, dest: str) -> None:
             break
         except requests.HTTPError:
             http_failures += 1
-        except:
+        except KeyboardInterrupt as e:
+            raise e
+        except Exception as e:
             log_error(
                 "Failure while downloading {artist} - {track}",
                 artist=album.artist.name,
@@ -129,7 +137,6 @@ def download_album(album: tidalapi.Album) -> None:
     dest = clean_template(
         conf["dest_dir"] + "/" + conf["album_dir"],
         album=album,
-        artist=album.artist,
     )
     os.makedirs(os.path.dirname(dest), exist_ok=True)
     download_cover(album, dest, conf["album_image_size"])
diff --git a/tidal-scraper/helper.py b/tidal-scraper/helper.py
index 0cc1f28..eac4ba9 100644
--- a/tidal-scraper/helper.py
+++ b/tidal-scraper/helper.py
@@ -1,6 +1,8 @@
 import re
 import os
-import tomllib
+import toml
+# TODO: wait for python to update to 3.11 for ubuntu users
+# import tomllib
 import sys
 import traceback
 
@@ -19,8 +21,10 @@ if not conf_dir:
 conf_dir += "/tidal-scraper"
 state_dir += "/tidal-scraper"
 
-with open(conf_dir + "/conf.toml", "rb") as f:
-    conf = tomllib.load(f)
+with open(conf_dir + "/conf.toml", "r") as f:
+    conf = toml.load(f)
+# with open(conf_dir + "/conf.toml", "rb") as f:
+    # conf = tomllib.load(f)
 
 
 def clean_template(path: str, **kwargs) -> str:
@@ -33,5 +37,6 @@ def clean_template(path: str, **kwargs) -> str:
 def log_error(template: str, **kwargs):
     with open(conf["error_log"], "a") as f:
         msg = template.format(**kwargs)
-        f.write(msg + "\n\n\n")
+        f.write(msg + "\n")
         traceback.format_exception(*sys.exc_info())
+        f.write("\n\n")
diff --git a/tidal-scraper/run.py b/tidal-scraper/run.py
old mode 100644
new mode 100755
index 791e1d7..cb0fffc
--- a/tidal-scraper/run.py
+++ b/tidal-scraper/run.py
@@ -1,4 +1,4 @@
-#!/bin/env python
+#!/bin/env python3
 from download import download_album
 from state import State
 from helper import conf