fix: populate script

This commit is contained in:
GitBluub
2023-11-15 13:32:02 +01:00
committed by Clément Le Bihan
parent ece93f79b2
commit ee56a53b40
3 changed files with 17 additions and 4 deletions

1
.gitignore vendored
View File

@@ -16,3 +16,4 @@ node_modules/
.data .data
.DS_Store .DS_Store
_gen _gen
venv

View File

@@ -8,6 +8,7 @@ from mido import MidiFile
from configparser import ConfigParser from configparser import ConfigParser
url = os.environ.get("API_URL") url = os.environ.get("API_URL")
auth_headers = {}
def getOrCreateAlbum(name, artistId): def getOrCreateAlbum(name, artistId):
if not name: if not name:
@@ -15,7 +16,7 @@ def getOrCreateAlbum(name, artistId):
res = requests.post(f"{url}/album", json={ res = requests.post(f"{url}/album", json={
"name": name, "name": name,
"artist": artistId, "artist": artistId,
}) },headers=auth_headers)
out = res.json() out = res.json()
print(out) print(out)
return out["id"] return out["id"]
@@ -25,7 +26,7 @@ def getOrCreateGenre(names):
for name in names.split(","): for name in names.split(","):
res = requests.post(f"{url}/genre", json={ res = requests.post(f"{url}/genre", json={
"name": name, "name": name,
}) },headers=auth_headers)
out = res.json() out = res.json()
print(out) print(out)
ids += [out["id"]] ids += [out["id"]]
@@ -35,7 +36,7 @@ def getOrCreateGenre(names):
def getOrCreateArtist(name): def getOrCreateArtist(name):
res = requests.post(f"{url}/artist", json={ res = requests.post(f"{url}/artist", json={
"name": name, "name": name,
}) },headers=auth_headers)
out = res.json() out = res.json()
print(out) print(out)
return out["id"] return out["id"]
@@ -49,6 +50,7 @@ def populateFile(path, midi, mxl):
difficulties["length"] = round((mid.length), 2) difficulties["length"] = round((mid.length), 2)
artistId = getOrCreateArtist(metadata["Artist"]) artistId = getOrCreateArtist(metadata["Artist"])
print(f"Populating {metadata['Name']}") print(f"Populating {metadata['Name']}")
print(auth_headers)
res = requests.post(f"{url}/song", json={ res = requests.post(f"{url}/song", json={
"name": metadata["Name"], "name": metadata["Name"],
"midiPath": f"/assets/{midi}", "midiPath": f"/assets/{midi}",
@@ -58,18 +60,26 @@ def populateFile(path, midi, mxl):
"album": getOrCreateAlbum(metadata["Album"], artistId), "album": getOrCreateAlbum(metadata["Album"], artistId),
"genre": getOrCreateGenre(metadata["Genre"]), "genre": getOrCreateGenre(metadata["Genre"]),
"illustrationPath": f"/assets/{os.path.commonpath([midi, mxl])}/illustration.png" "illustrationPath": f"/assets/{os.path.commonpath([midi, mxl])}/illustration.png"
}) }, headers=auth_headers)
print(res.json()) print(res.json())
def main(): def main():
global url global url
if url == None: if url == None:
url = "http://localhost:3000" url = "http://localhost:3000"
print("Connecting as guest")
res = requests.post(f"{url}/auth/guest")
token = (res.json())["access_token"]
global auth_headers
auth_headers["Authorization"] = f"Bearer {token}"
print("Searching for files...") print("Searching for files...")
for file in glob.glob("**/*.ini", recursive=True): for file in glob.glob("**/*.ini", recursive=True):
print(f"File found: {file}") print(f"File found: {file}")
path = os.path.splitext(file)[0] path = os.path.splitext(file)[0]
populateFile(file, path + ".midi", path + ".mxl") populateFile(file, path + ".midi", path + ".mxl")
print("Deleting guest")
requests.delete(f"{url}/auth/me", headers=auth_headers)
if __name__ == "__main__": if __name__ == "__main__":
exit(main()) exit(main())

2
assets/requirements.txt Normal file
View File

@@ -0,0 +1,2 @@
mido
requests