-
Notifications
You must be signed in to change notification settings - Fork 84
Opt-in caching to speed up search of redundant tracks #137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import json | ||
from spotify_to_ytmusic.settings import CACHE_DIR | ||
|
||
|
||
class CacheManager: | ||
def __init__(self): | ||
self.cache_dir = CACHE_DIR | ||
self.cache_dir.mkdir(parents=True, exist_ok=True) | ||
self.cache_file = self.cache_dir / "lookup.json" | ||
|
||
def load_lookup_table(self): | ||
try: | ||
with self.cache_file.open("r", encoding="utf-8") as f: | ||
return json.load(f) | ||
except FileNotFoundError: | ||
return {} | ||
|
||
def save_to_lookup_table(self, table): | ||
with self.cache_file.open("w", encoding="utf-8") as f: | ||
json.dump(table, f, ensure_ascii=False) | ||
|
||
def remove_cache_file(self): | ||
if self.cache_file.is_file(): | ||
self.cache_file.unlink() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import unittest | ||
from pathlib import Path | ||
from platformdirs import user_cache_dir | ||
from spotify_to_ytmusic.utils.cache_manager import CacheManager | ||
|
||
|
||
class TestCacheManager(unittest.TestCase): | ||
@classmethod | ||
def setUpClass(cls): | ||
cls.cache_manager = CacheManager() | ||
cls.test_data = { | ||
"Jordan Burns Weekend": "4ga4xy7omAE", | ||
"Robert DeLong Lights LŪN Did It To Myself - LŪN Remix": "M7jON1NmyoY", | ||
} | ||
|
||
def setUp(self): | ||
self.cache_manager.remove_cache_file() | ||
|
||
def test_save_and_load_lookup_table(self): | ||
"""Test that data is correctly saved and loaded from cache.""" | ||
self.cache_manager.save_to_lookup_table(self.test_data) | ||
|
||
loaded_data = self.cache_manager.load_lookup_table() | ||
self.assertEqual(loaded_data, self.test_data) | ||
|
||
def test_load_empty_lookup_table(self): | ||
"""Test that loading a non-existing cache returns an empty dictionary.""" | ||
self.assertEqual(self.cache_manager.load_lookup_table(), {}) | ||
|
||
def test_remove_cache_file(self): | ||
"""Test that the cache file is properly deleted.""" | ||
self.cache_manager.save_to_lookup_table(self.test_data) | ||
|
||
self.assertTrue(self.cache_manager.cache_file.exists()) | ||
|
||
self.cache_manager.remove_cache_file() | ||
self.assertFalse(self.cache_manager.cache_file.exists()) | ||
|
||
def test_cache_file_location(self): | ||
"""Test that the cache file is created in the correct platformdirs location.""" | ||
expected_path = ( | ||
Path(user_cache_dir(appname="spotify_to_ytmusic", appauthor=False)) | ||
/ "lookup.json" | ||
) | ||
self.assertEqual(self.cache_manager.cache_file, expected_path) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.