Site Tools


linux:ubuntu:alexamusictodatabase

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
linux:ubuntu:alexamusictodatabase [2019/11/24 22:33] lunetikklinux:ubuntu:alexamusictodatabase [2021/12/29 00:15] (current) lunetikk
Line 2: Line 2:
 This script is an updated version of my script to [[linux:ubuntu:alexamusictitleonwebsite|display playing music title on website]]. It will write all the artists, tracks and albums to a MySQL database. This script is an updated version of my script to [[linux:ubuntu:alexamusictitleonwebsite|display playing music title on website]]. It will write all the artists, tracks and albums to a MySQL database.
  
-===== Database =====+**Update 2021-12-29:** Amazon redesigned the output via alexa.amazon.de, the string for "album" is now always empty, so can only get the state, artist and title at the moment. If you use Spotify you might want to check out my Spotify script. I also thought about a way to get an album via artist and track out of a musicdatabase (like musicbrainz) but I dont know yet if this will work. 
  
-{{:linux:ubuntu:pasted:20191124-223313.png}}+===== Database =====
  
 +{{:linux:ubuntu:pasted:20191124-223313.png?500}}
  
 <code mysql amazonmusic.sql> <code mysql amazonmusic.sql>
Line 45: Line 46:
 #!/bin/bash #!/bin/bash
  
-#Path to logfile +#Login MySQL
-LOGPATH=/var/mysqlinsert.log +
- +
-#Login for MYSQL+
 LOGIN="local" LOGIN="local"
  
-#Database.Table+#Database MySQL
 DB=amazonmusic DB=amazonmusic
  
 +#Temp- and Webfile
 TMPFILE=/tmp/audio.txt TMPFILE=/tmp/audio.txt
-WEBFILE=/var/www/lunetikkde/audio.php +WEBFILE=/var/www/mysite/audio.php 
-WEBFILE2=/var/www/lunetikkde/audio2.php+WEBFILE2=/var/www/mysite/audio2.php
  
 +#Execute Alexa Remote Control to get the current data and write into $TMPFILE
 /alexa/alexa-remote-control/alexa_remote_control.sh -q > $TMPFILE /alexa/alexa-remote-control/alexa_remote_control.sh -q > $TMPFILE
  
-#OFFLINE=`grep -m1 'message' $TMPFILE | cut -d '"' -f 3 | cut -d ' ' -f 2`+#Get the necessary data from $TMPFILE
 OFFLINE=`grep -m1 'message' $TMPFILE | awk 'BEGIN{FS=" "}{gsub(",",""); print $2}'` OFFLINE=`grep -m1 'message' $TMPFILE | awk 'BEGIN{FS=" "}{gsub(",",""); print $2}'`
 STATE=`grep -m1 'state' $TMPFILE | cut -d '"' -f 4` STATE=`grep -m1 'state' $TMPFILE | cut -d '"' -f 4`
Line 69: Line 69:
 IMG=`grep -m1 'url' $TMPFILE | cut -d '"' -f 4` IMG=`grep -m1 'url' $TMPFILE | cut -d '"' -f 4`
  
- +#If $WEBFILE exists, make a copy of it ($WEBFILE2) and empty $WEBFILE, else create an empty $WEBFILE
-#echo ""$WEBFILE +
 if [ -f $WEBFILE ]; then if [ -f $WEBFILE ]; then
    cp -a $WEBFILE $WEBFILE2    cp -a $WEBFILE $WEBFILE2
Line 79: Line 77:
 fi fi
  
 +#If $OFFLINE isnt null and $STATECUT isnt null, write all the data to $WEBFILE, else you are not listening to music
 if [ "$OFFLINE" != "null" ] && [ "$STATECUT" != "null" ]; then if [ "$OFFLINE" != "null" ] && [ "$STATECUT" != "null" ]; then
    echo "<table style='width:100%'><col style='width:90%'><col style='width:10%'><tr><td>" >> $WEBFILE    echo "<table style='width:100%'><col style='width:90%'><col style='width:10%'><tr><td>" >> $WEBFILE
Line 92: Line 91:
 fi fi
  
-#HASH="$(cmp --silent $WEBFILE $WEBFILE2; echo $?)"  # "$?" gives exit status for each comparison+#The following section compares the $WEBFILEs to make sure you dont write data multiple times for the same track 
 +#Use cmp if it works for you or just use MD5 like I did 
 + 
 +#HASH="$(cmp --silent $WEBFILE $WEBFILE2; echo $?)"  #"$?" gives exit status for each comparison
 #If a FILE is '-' or missing, read standard input. Exit status is 0 if inputs are the same, 1 if different, 2 if trouble. #If a FILE is '-' or missing, read standard input. Exit status is 0 if inputs are the same, 1 if different, 2 if trouble.
 #cmp showed 2 (error) all the time, even in cli, feel free to use if it works for you... #cmp showed 2 (error) all the time, even in cli, feel free to use if it works for you...
  
 +#MD5 Hash of both webfiles to compare
 HASHONE=$(md5sum $WEBFILE | cut -d " " -f1) HASHONE=$(md5sum $WEBFILE | cut -d " " -f1)
 HASHTWO=$(md5sum $WEBFILE2 | cut -d " " -f1) HASHTWO=$(md5sum $WEBFILE2 | cut -d " " -f1)
  
-#if [ "$HASH" -eq 1 ] #use this if you use cmp +#if [ "$HASH" -eq 1 ] #use this if you use cmp, if status is equal to 1 (different), then execute code 
-if [ "$HASHONE" != "$HASHTWO" ] +if [ "$HASHONE" != "$HASHTWO" ] #compare md5 hashes, if different, then execute code 
-then  # if status is equal to 1, then execute code+then  
  
-DBTRACK=$(mysql -D $DB --login-path=$LOGIN -se "SELECT track_id FROM track WHERE track_name = '$TITLE'"+#Look for the current track, artist, album, if not in database, insert, else dont insert 
-DBARTIST=$(mysql -D $DB --login-path=$LOGIN -se "SELECT artist_id FROM artist WHERE artist_name = '$ARTIST'"+DBTRACK=$(mysql --login-path=$LOGIN -D $DB -se "SELECT track_id FROM track WHERE track_name = '$TITLE'"
-DBALBUM=$(mysql -D $DB --login-path=$LOGIN -se "SELECT album_id FROM album WHERE album_name = '$ALBUM'")+DBARTIST=$(mysql --login-path=$LOGIN -D $DB -se "SELECT artist_id FROM artist WHERE artist_name = '$ARTIST'"
 +DBALBUM=$(mysql --login-path=$LOGIN -D $DB -se "SELECT album_id FROM album WHERE album_name = '$ALBUM'")
  
-if [ -z "$DBTITLE" ]+if [ -z "$DBTRACK" ]
 then then
         if [ -z "$DBALBUM" ]         if [ -z "$DBALBUM" ]
Line 113: Line 117:
                 if [ -z "$DBARTIST" ]                 if [ -z "$DBARTIST" ]
                 then                 then
-mysql -D $DB --login-path=$LOGIN  << EOFMYSQL+mysql --login-path=$LOGIN -D $DB << EOFMYSQL
 INSERT INTO artist (artist_name) INSERT INTO artist (artist_name)
 VALUES ('$ARTIST'); VALUES ('$ARTIST');
 EOFMYSQL EOFMYSQL
                 fi                 fi
-mysql -D $DB --login-path=$LOGIN  << EOFMYSQL+mysql --login-path=$LOGIN -D $DB  << EOFMYSQL
 INSERT INTO album (album_name,album_cover,artist_id) INSERT INTO album (album_name,album_cover,artist_id)
 VALUES ('$ALBUM','$IMG',(SELECT artist_id FROM artist WHERE artist_name = '$ARTIST')); VALUES ('$ALBUM','$IMG',(SELECT artist_id FROM artist WHERE artist_name = '$ARTIST'));
 EOFMYSQL EOFMYSQL
         fi         fi
-mysql -D $DB --login-path=$LOGIN  << EOFMYSQL+mysql --login-path=$LOGIN -D $DB << EOFMYSQL
 INSERT INTO track (track_name,album_id,artist_id) INSERT INTO track (track_name,album_id,artist_id)
 VALUES ('$TITLE',(SELECT album_id FROM album WHERE album_name = '$ALBUM'),(SELECT artist_id FROM artist WHERE artist_name = '$ARTIST')); VALUES ('$TITLE',(SELECT album_id FROM album WHERE album_name = '$ALBUM'),(SELECT artist_id FROM artist WHERE artist_name = '$ARTIST'));
Line 129: Line 133:
 fi fi
  
-mysql -D $DB --login-path=$LOGIN  << EOFMYSQL+mysql --login-path=$LOGIN -D $DB << EOFMYSQL
 INSERT INTO played (artist_id,track_id,album_id) INSERT INTO played (artist_id,track_id,album_id)
 VALUES ((SELECT artist_id FROM artist WHERE artist_name = '$ARTIST'),(SELECT track_id FROM track WHERE track_name = '$TITLE'),(SELECT album_id FROM album WHERE album_name = '$ALBUM')); VALUES ((SELECT artist_id FROM artist WHERE artist_name = '$ARTIST'),(SELECT track_id FROM track WHERE track_name = '$TITLE'),(SELECT album_id FROM album WHERE album_name = '$ALBUM'));
 EOFMYSQL EOFMYSQL
 +
  
 fi fi
linux/ubuntu/alexamusictodatabase.1574631206.txt.gz · Last modified: 2019/11/24 22:33 by lunetikk