Skip to content

Commit 8ec5051

Browse files
committed
Update genre_update.py
1 parent 5cacdfb commit 8ec5051

1 file changed

Lines changed: 39 additions & 5 deletions

File tree

docs/examples/genre_update.py

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import ampache
2+
import os
23
import re
34
import sys
45
import time
56

7+
from datetime import datetime
68
from mutagen import File
79

810

@@ -31,7 +33,7 @@ def update_music_metadata(ampache_connection, update_list):
3133
#print(album_songs)
3234
change = False
3335
for song in album_songs['song']:
34-
if "filename" in song:
36+
if "filename" in song and not song["filename"].endswith(".wav") and os.path.isfile(song["filename"]):
3537
#print(song['filename'])
3638
music_file = f"{song['filename']}"
3739

@@ -47,7 +49,7 @@ def update_music_metadata(ampache_connection, update_list):
4749
# update from tags to reflect the new changes
4850
if change:
4951
print(f"{id} updated {music_file}\n {existing_genres} => {genres}\n")
50-
else:
52+
elif not song["filename"].endswith(".wav"):
5153
print(f"{id} NO CHANGE {music_file}")
5254
print(genres)
5355
print()
@@ -59,7 +61,7 @@ def update_music_metadata(ampache_connection, update_list):
5961
#print(album_songs)
6062
change = False
6163
for song in album_songs['song']:
62-
if "filename" in song:
64+
if "filename" in song and os.path.isfile(song['filename']):
6365
#print(song['filename'])
6466
music_file = f"{song['filename']}"
6567

@@ -89,7 +91,7 @@ def update_music_metadata(ampache_connection, update_list):
8991
def get_external_genres(ampache_connection, album_id):
9092
genres = []
9193
genres_tmp = ampache_connection.get_external_metadata(album_id, 'album')
92-
if not genres_tmp == False and "plugin" in genres_tmp:
94+
if genres_tmp and "plugin" in genres_tmp:
9395
for plugin in genres_tmp['plugin']:
9496
if "genre" in genres_tmp['plugin'][plugin]:
9597
plugin_genres = genres_tmp['plugin'][plugin]['genre']
@@ -118,7 +120,10 @@ def get_external_genres(ampache_connection, album_id):
118120
else:
119121
plugin_genres = list(plugin_genres.values())
120122
genres.extend(filter_genres(plugin_genres))
121-
return genres if genres else False
123+
if genres:
124+
genres = replace_lowercase_duplicates(genres)
125+
return genres
126+
return False
122127

123128

124129
def filter_genres(genres: list):
@@ -163,6 +168,7 @@ def filter_genres(genres: list):
163168
'AlternRock': ['Alternative Rock'],
164169
'Altrnative Rock': ['Alternative Rock'],
165170
'Ambient Electronic': ['Electronic','Ambient'],
171+
'Aor': ['AOR'],
166172
'Art-Pop': ['Art Pop'],
167173
'Art-Rock': ['Art Rock'],
168174
'Atmospheric Death': ['Atmospheric Death Metal'],
@@ -210,6 +216,7 @@ def filter_genres(genres: list):
210216
'Dreampop': ['Dream Pop'],
211217
'Drum & Bass': ['Drum n Bass'],
212218
'Drum and Bass': ['Drum n Bass'],
219+
'Ebm': ['EBM'],
213220
'Electro Industrial': ['Electronic','Rock','Industrial'],
214221
'Electro Pop': ['Electropop'],
215222
'Electronica & Dance': ['Dance','Electronica'],
@@ -253,7 +260,9 @@ def filter_genres(genres: list):
253260
'Heavy-Metal': ['Heavy Metal'],
254261
'Hip-hop': ['Hip Hop'],
255262
'Idie-Pop': ['Indie Pop'],
263+
'Idm': ['IDM'],
256264
'Imdustrial': ['Industrial'],
265+
'Indie': ['Indie Rock'],
257266
'Indie / Folk Rock / Singer-Songwriter': ['Folk Rock','Indie Rock'],
258267
'Indie / Pop-Rock': ['Indie Rock','Pop Rock'],
259268
'Indie Pop / Electronic': ['Electronic','Indie Pop'],
@@ -324,6 +333,7 @@ def filter_genres(genres: list):
324333
'Raggae': ['Reggae'],
325334
'Retro Wave': ['Retrowave'],
326335
'rimental Black Metal': ['Experimental Black Metal'],
336+
'Rio': ['RIO'],
327337
'Rock \'N\' Roll': ['Rock & Roll'],
328338
'Rock indé': ['Indie Rock'],
329339
'Rock progressif': ['Prog Rock'],
@@ -378,6 +388,16 @@ def filter_genres(genres: list):
378388

379389
def validate_discogs_genre(genre):
380390
discogs_valid = [
391+
'Alt-Pop',
392+
'Bootleg',
393+
'Club/Dance',
394+
'Metal',
395+
'Neue Deutsche Harte',
396+
'Podcast',
397+
'Punk Rock',
398+
'Thrash Metal',
399+
'Visual Kei',
400+
'Vocaloid',
381401
'Rock',
382402
'Electronic',
383403
'Pop',
@@ -1063,6 +1083,20 @@ def validate_discogs_genre(genre):
10631083
return None
10641084

10651085

1086+
def replace_lowercase_duplicates(input_list):
1087+
seen = {}
1088+
result = []
1089+
for item in input_list:
1090+
lower_item = item.lower()
1091+
if lower_item not in seen:
1092+
seen[lower_item] = item # Track the first occurrence
1093+
result.append(item)
1094+
else:
1095+
# Replace with the first occurrence (preserve original casing)
1096+
result.append(seen[lower_item])
1097+
return result
1098+
1099+
10661100
# Open Ampache library
10671101
ampache_connection = ampache.API()
10681102

0 commit comments

Comments
 (0)