Skip to content
This repository has been archived by the owner on Feb 9, 2024. It is now read-only.

Commit

Permalink
Rework the index_dir and use db_files instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
mayhem committed Jan 10, 2024
1 parent f866b0d commit 9b31507
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 84 deletions.
49 changes: 25 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ source .virtualenv/bin/activate
pip install -r requirements.txt
```

### Setting up config.py

While it isn't strictly necessary to setup config.py, it makes using the resolver easier:

```
cp config.py.sample config.py
```

Then edit config.py and set the location of where you're going to store your resolver database file
into DATABASE_FILE. If you plan to use a Subsonic API, the fill out the Subsonic section as well.

If you decide not to use the config.py file, make sure to pass the path to the DB file with -d to each
command. All further examples in this file assume you added the config file and will therefore omit
the -d option.

## Scanning your collection

Note: Soon we will eliminate the requirement to do a filesystem scan before also doing a subsonic
Expand All @@ -50,14 +65,14 @@ scan (if you plan to use subsonic). For now, do the file system scan, then the s
Then prepare the index and scan a music collection. mp3, m4a, wma, OggVorbis, OggOpus and flac files are supported.

```
./resolve.py create music_index
./resolve.py scan music_index <path to mp3/flac files>
./resolve.py create
./resolve.py scan <path to mp3/flac files>
```

If you remove from tracks from your collection, use cleanup to remove refereces to those tracks:

```
./resolve.py cleanup music_index
./resolve.py cleanup
```

### Scan a Subsonic collection
Expand All @@ -71,7 +86,7 @@ cp config.py.sample config.py
Then edit the file and add your subsonic configuration.

```
./resolve.py subsonic music_index
./resolve.py subsonic
```

This will match your collection to the remove subsonic API collection.
Expand All @@ -94,7 +109,7 @@ curl "https://api.listenbrainz.org/1/playlist/<playlist MBID>" > test.jspf
Finally, resolve the playlist to local files:

```
./resolve.py playlist music_index input.jspf output.m3u
./resolve.py playlist input.jspf output.m3u
```

Then open the m3u playlist with a local tool.
Expand Down Expand Up @@ -124,21 +139,7 @@ to download more data for your MusicBrainz tagged music collection.
First, download tag and popularity data:

```
./resolve.py metadata music_index
```

Then, copy config.py.sample to config.py and then edit config.py:

```
cp config.py.sample config.py
edit config.py
```

Fill out the values for your subsonic server API and save the file.
Finally, match your collection against the subsonic collection:

```
./resolve.py subsonic music_index
./resolve.py metadata
```

### Playlist generation
Expand Down Expand Up @@ -167,7 +168,7 @@ isn't very suited for the prompt that was given.
#### Artist Element

```
./resolve.py lb-radio music_index easy 'artist:(taylor swift, drake)'
./resolve.py lb-radio easy 'artist:(taylor swift, drake)'
```

Generates a playlist with music from Taylor Swift and artists similar
Expand All @@ -177,14 +178,14 @@ to her and Drake, and artists similar to him.
#### Tag Element

```
./resolve.py lb-radio music_index easy 'tag:(downtempo, trip hop)'
./resolve.py lb-radio easy 'tag:(downtempo, trip hop)'
```

This will generate a playlist on easy mode for recordings that are
tagged with "downtempo" AND "trip hop".

```
./resolve.py lb-radio music_index medium 'tag:(downtempo, trip hop)::or'
./resolve.py lb-radio medium 'tag:(downtempo, trip hop)::or'
```

This will generate a playlist on medium mode for recordings that are
Expand All @@ -194,7 +195,7 @@ at the end of the prompt.
You can include more than on tag query in a prompt:

```
./resolve.py lb-radio music_index medium 'tag:(downtempo, trip hop)::or tag:(punk, ska)'
./resolve.py lb-radio medium 'tag:(downtempo, trip hop)::or tag:(punk, ska)'
```

#### Stats, Collections, Playlists and Rec
Expand Down
3 changes: 3 additions & 0 deletions config.py.sample
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Where to find the database file
DATABASE_FILE = ""

# To connect to a subsonic API
SUBSONIC_HOST = "" # include http:// or https://
SUBSONIC_USER = ""
Expand Down
2 changes: 0 additions & 2 deletions lb_content_resolver/content_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,6 @@ def resolve_playlist(self, match_threshold, recordings=None, jspf_playlist=None)
if recordings is None and jspf_playlist is None:
raise ValueError("Either recordings or jspf_playlist must be passed.")

print("\nResolve recordings to local files or subsonic ids")

artist_recording_data = []
if jspf_playlist is not None:
if len(jspf_playlist["playlist"]["track"]) == 0:
Expand Down
24 changes: 10 additions & 14 deletions lb_content_resolver/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,16 @@ class Database:
'''
Keep a database with metadata for a collection of local music files.
'''
def __init__(self, index_dir):
self.index_dir = index_dir
self.db_file = os.path.join(index_dir, "lb_resolve.db")
def __init__(self, db_file):
self.db_file = db_file
self.fuzzy_index = None

def create(self):
"""
Create the index directory for the data. Currently it contains only
the sqlite dir, but in the future we may serialize the fuzzy index here as well.
Create the database. Can be run again to create tables that have been recently added to the code,
but don't exist in the DB yet.
"""

if not os.path.exists(self.index_dir):
try:
os.mkdir(self.index_dir)
except OSError as err:
print("Could not create index directory: %s (%s)" % (self.index_dir, err))
return

setup_db(self.db_file)
db.connect()
db.create_tables([Recording, RecordingMetadata, Tag, RecordingTag, RecordingSubsonic, UnresolvedRecording])
Expand Down Expand Up @@ -84,7 +76,7 @@ def scan(self, music_dir):
with tqdm(total=self.track_count_estimate) as self.progress_bar:
self.traverse("")

self.close_db()
self.close()

print("Checked %s tracks:" % self.total)
print(" %5d tracks not changed since last run" % self.not_changed)
Expand Down Expand Up @@ -285,12 +277,16 @@ def database_cleanup(self, dry_run):
print("RM %s" % recording.file_path)
recording_ids.append(recording.id)

if not recording_ids:
print("No cleanup needed, all recordings found")
return

if not dry_run:
placeholders = ",".join(("?", ) * len(recording_ids))
db.execute_sql("""DELETE FROM recording WHERE recording.id IN (%s)""" % placeholders, tuple(recording_ids))
print("Stale references removed")
else:
print("--delete not specified, no refeences removed")
print("--delete not specified, no refences removed")

def metadata_sanity_check(self, include_subsonic=False):
"""
Expand Down
7 changes: 5 additions & 2 deletions lb_content_resolver/fuzzy_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ def search(self, query_data):

output = []
for i, result in enumerate(results):
output.append({ "confidence": fabs(result[1][0]),
"recording_id": result[0][0] })
if len(result[0]):
output.append({ "confidence": fabs(result[1][0]),
"recording_id": result[0][0] })
else:
output.append({ "confidence": 0.0, "recording_id": 0 })

return output
Loading

0 comments on commit 9b31507

Please sign in to comment.