summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorniliara-edu <nil.jimeno@estudiant.fjaverianas.com>2024-10-02 23:21:43 +0200
committerniliara-edu <nil.jimeno@estudiant.fjaverianas.com>2024-10-02 23:21:43 +0200
commit7df09f8b146ae7fb091787770fe5681381164f0e (patch)
tree0310d8dad1d0c6198d68d13b22030db3c5e8b28f
parent0972fa6eab8c9111311f082ba8abfdc6b4a40945 (diff)
fix database setup
-rw-r--r--.gitignore2
-rw-r--r--scripts/create_db.sh6
-rw-r--r--scripts/create_db.sql1
-rw-r--r--scripts/create_user.sql4
-rw-r--r--scripts/populate.sh12
-rw-r--r--scripts/populate/api.py5
-rw-r--r--scripts/populate/database.py18
-rw-r--r--scripts/populate/parser.py17
-rw-r--r--scripts/populate/structures.py2
9 files changed, 43 insertions, 24 deletions
diff --git a/.gitignore b/.gitignore
index 9adab75..93694a3 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,3 +3,5 @@ target/
Cargo.lock
**/*.rs.bk
.env
+lyrics_cache/
+covers/
diff --git a/scripts/create_db.sh b/scripts/create_db.sh
index c0e759c..4f4a483 100644
--- a/scripts/create_db.sh
+++ b/scripts/create_db.sh
@@ -1,6 +1,6 @@
#!/bin/bash
echo "choose a password for the database:"
-read input
+read -s input
echo "use xampp's version of mariadb? [y/n]"
read answer
@@ -19,9 +19,7 @@ fi
echo "creating database..."
$sudo $mariadb -u root <<EOF
-$(cat scripts/create_user.sql)
+CREATE OR REPLACE USER 'balalaika_user'@'%' IDENTIFIED BY '$input';
EOF
-echo '$input'
-
$sudo $mariadb -u root < ./scripts/create_db.sql
diff --git a/scripts/create_db.sql b/scripts/create_db.sql
index f803714..405fa96 100644
--- a/scripts/create_db.sql
+++ b/scripts/create_db.sql
@@ -17,6 +17,7 @@ CREATE TABLE album (
name varchar(255),
cover varchar(510),
artist_id int,
+ release_date date,
PRIMARY KEY (id),
FOREIGN KEY (artist_id) REFERENCES artist(id)
diff --git a/scripts/create_user.sql b/scripts/create_user.sql
index bc9472b..3d78dc8 100644
--- a/scripts/create_user.sql
+++ b/scripts/create_user.sql
@@ -1,3 +1 @@
-DROP USER IF EXISTS 'balalaika_user'@'%';
-DROP USER IF EXISTS 'balalaika_user'@'localhost';
-CREATE USER 'balalaika_user'@'%' IDENTIFIED BY '$input';
+CREATE OR REPLACE USER 'balalaika_user'@'%' IDENTIFIED BY '$input';
diff --git a/scripts/populate.sh b/scripts/populate.sh
index d8ccf8d..0f1ac0c 100644
--- a/scripts/populate.sh
+++ b/scripts/populate.sh
@@ -3,11 +3,19 @@ echo activate virtual environment? \(source ~/.venv/bin/activate\) [y/n]
read answer
if [[ $answer == 'y' ]] then
- source ~/.venv/bin/activate
+ source ~/.config/venv/bin/activate
fi
-python3 scripts/populate/main.py
+mkdir -p lyrics_cache
+cd lyrics_cache
+
+python3 ../scripts/populate/main.py
+
+cd ..
+mv lyrics_cache/covers ./
+rm -rf lyrics_cache
if [[ $answer == 'y' ]] then
deactivate
fi
+
diff --git a/scripts/populate/api.py b/scripts/populate/api.py
index 5cc24d3..9133c25 100644
--- a/scripts/populate/api.py
+++ b/scripts/populate/api.py
@@ -14,8 +14,9 @@ def download_albums(albums):
def get_album_json(artist_name, album_name):
- if os.path.isfile(parser.getLink(album_name)):
+ link = parser.get_link(album_name)
+ if os.path.isfile(link):
return
album = genius.search_album(album_name, artist_name)
- album.save_lyrics()
+ album.save_lyrics(f"{link}")
diff --git a/scripts/populate/database.py b/scripts/populate/database.py
index 2319812..eaccfd2 100644
--- a/scripts/populate/database.py
+++ b/scripts/populate/database.py
@@ -16,19 +16,17 @@ connector = mysql.connector.connect(
cursor = connector.cursor()
-
-
def process_albums(album_list):
[process_album(album, album_id)
for album_id, album in enumerate(album_list, 1)]
def process_album(album, album_id):
- upload_album(album)
+ upload_album(album, album_id)
[upload_song(song, album_id) for song in album.songs]
-def upload_album(album):
+def upload_album(album, album_id):
album.name = album.name.lower()
cursor.execute("""
INSERT INTO album (
@@ -40,9 +38,19 @@ def upload_album(album):
""", {
'name': album.name,
'cover': album.cover,
- 'artist_id': album.artist
+ 'artist_id': album.artist,
})
+ if album.release:
+ cursor.execute("""
+ UPDATE album
+ SET release_date = (%(release)s)
+ WHERE id = %(album_id)s;
+ """, {
+ 'release': album.release,
+ 'album_id': album_id,
+ })
+
def upload_song(song, album_id):
cursor.execute("""
diff --git a/scripts/populate/parser.py b/scripts/populate/parser.py
index cbb51b5..fdce1c9 100644
--- a/scripts/populate/parser.py
+++ b/scripts/populate/parser.py
@@ -5,7 +5,7 @@ import os
def process_json_file(name, album_id, artist_id):
- link = getLink(name)
+ link = get_link(name)
file_json = open(link, "r")
album_json = file_json.read()
@@ -14,8 +14,8 @@ def process_json_file(name, album_id, artist_id):
return process_json(album_json, album_id, artist_id)
-def getLink(name):
- return "Lyrics_"+name.replace(" ", "")+".json"
+def get_link(name):
+ return name.replace(" ", "")+".json"
def process_json(album_json, album_id, artist_id):
@@ -27,10 +27,13 @@ def process_json(album_json, album_id, artist_id):
new_cover = get_cover_link(artist_id, album_id)
download_cover(off_cover, new_cover, artist_id)
- year = data["release_date_components"]["year"]
- month = data["release_date_components"]["year"]
- day = data["release_date_components"]["year"]
- release = [year, month, day]
+ if data["release_date_components"]:
+ year = data["release_date_components"]["year"]
+ month = data["release_date_components"]["month"]
+ day = data["release_date_components"]["day"]
+ release = f"{year}-{month}-{day}"
+ else:
+ release = None
songs = [analyze_song(song) for song in data["tracks"]]
return structures.album(album_name, new_cover, songs, artist_id, release)
diff --git a/scripts/populate/structures.py b/scripts/populate/structures.py
index 31feb1b..b6cd8cd 100644
--- a/scripts/populate/structures.py
+++ b/scripts/populate/structures.py
@@ -5,7 +5,7 @@ class song:
class album:
- def __init__(self, name, cover, songs, artist_id, release=None):
+ def __init__(self, name, cover, songs, artist_id, release = None):
self.name = name
self.cover = cover
self.songs = songs