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
.DS_Store
_gen
venv

View File

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

2
assets/requirements.txt Normal file
View File

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