This repository has been archived by the owner on Dec 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
my-music
294 lines (247 loc) · 7.17 KB
/
my-music
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/bin/bash
########## VARIABLES ##########
## Helpers
yesReg="^(y|Y)(es)?$"
## Output
bold=$(tput bold)
clear=$(tput clear)
normal=$(tput sgr0)
tab=$(tput cuf 4)
## Database info
user=postgres
password=postgres
dbname=MyMusic
tables="Labels, Artists and Records"
########### PROCEDURES ##########
addArtist ()
{
echo "Adding new Artist"
echo -n "Artist: "
read artist
echo -n "add website? [enter if none]: "
read link
PGPASSWORD=$password psql --echo-all --dbname=$dbname --username=$user --command="INSERT into Artists (name, website) SELECT lower('${artist}'), '${link}' WHERE NOT EXISTS (SELECT * FROM Artists WHERE name = lower('${artist}'));"
clear
}
addLabel ()
{
echo "Adding new Label"
echo -n "Label: "
read label
echo -n "bandcamp? [enter if none]: "
read bandcamp
PGPASSWORD=$password psql --echo-all --dbname=$dbname --username=$user --command="INSERT into Labels (name, bandcamp) SELECT lower('${label}'), '${bandcamp}' WHERE NOT EXISTS (SELECT * FROM Labels WHERE name = lower('${label}'));"
clear
}
addRecord ()
{
echo -n "Artist: "
read artist
artistid=`PGPASSWORD=$password psql --dbname=$dbname --quiet --tuples-only --username=$user --command="SELECT id FROM Artists WHERE name = lower('${artist}');"`
if [[ $artistid == "" ]]; then
addArtist
artistid=`PGPASSWORD=$password psql --dbname=$dbname --quiet --tuples-only --username=$user --command="SELECT id FROM Artists WHERE name = lower('${artist}');"`
fi
echo -n "Label: "
read label
labelid=`PGPASSWORD=$password psql --dbname=$dbname --quiet --tuples-only --username=$user --command="SELECT id FROM Labels WHERE name = lower('${label}');"`
if [[ $labelid == "" ]]; then
addLabel
labelid=`PGPASSWORD=$password psql --dbname=$dbname --quiet --tuples-only --username=$user --command="SELECT id FROM Labels WHERE name = lower('${label}');"`
fi
echo -n "Record title: "
read title
alreadyExists=`PGPASSWORD=$password psql --dbname=$dbname --quiet --tuples-only --username=$user --command="SELECT id FROM Records WHERE title = lower('${title}');"`
if ! [[ $alreadyExists == "" ]]; then
echo "Record already exists [hit enter]"
read
return
fi
echo -n "Record link: "
read link
echo -n "Did you listen? (y|n) "
read listened
if [[ "$listened" =~ $yesReg ]]; then
listened=true
else
listened=false
fi
echo -n "Did you like? (y|n) "
read liked
if [[ "$liked" =~ $yesReg ]]; then
liked=true
else
liked=false
fi
echo -n "Did you purchase? (y|n) "
read purchased
if [[ "$purchased" =~ $yesReg ]]; then
purchased=true
else
purchased=false
fi
echo "please confirm: "
echo "artist: $artist "
echo "label: $label "
echo "title: $title "
echo "link: $link "
echo "listened: $listened "
echo "liked: $liked "
echo "purchased: $purchased"
echo -n "ok? (y|n) "
read confirm
if ! [[ "$confirm" =~ $yesReg ]]; then
echo "Not confirmed. Returning"
return
fi
echo "confirmed"
PGPASSWORD=$password psql --echo-all --dbname=$dbname --username=$user --command="INSERT into Records (title, artist, label, link, listened, liked, purchased) VALUES(lower('${title}'), $artistid, $labelid, trim('${link}'), $listened, $liked, $purchased);"
clear
}
listLiked ()
{
OFFSET=0
LIMIT=20
PGPASSWORD=$password psql --quiet --dbname=$dbname --username=$user --command="SELECT Records.title, Artists.name AS artist, Labels.name AS label FROM Records INNER JOIN Artists ON Records.artist = Artists.id INNER JOIN Labels ON Records.label = Labels.id WHERE liked = true AND purchased = false LIMIT $LIMIT OFFSET $OFFSET;"
while [ true ]; do
echo -n "more --- [b to go back]"
read action
case $action in
b )
return
;;
* )
OFFSET=$(( OFFSET + 20 ))
PGPASSWORD=$password psql --quiet --dbname=$dbname --username=$user --command="SELECT Records.title, Artists.name AS artist, Labels.name AS label FROM Records INNER JOIN Artists ON Records.artist = Artists.id INNER JOIN Labels ON Records.label = Labels.id LIMIT $LIMIT OFFSET $OFFSET;"
esac
done
}
listenToRecord ()
{
recordid=`PGPASSWORD=$password psql --dbname=$dbname --quiet --tuples-only --username=$user --command="SELECT id FROM Records WHERE listened = false AND link != '' ORDER BY random() LIMIT 1;"`
if [ "$recordid" == "" ]; then
echo "No unlistened records"
read
return
fi
recordLink=`PGPASSWORD=$password psql --dbname=$dbname --quiet --tuples-only --username=$user --command="SELECT link FROM Records WHERE id = $recordid;"`
start chrome $recordLink
listened=true
echo -n "Did you like? (y|n) "
read liked
if [[ "$liked" =~ $yesReg ]]; then
liked=true
else
liked=false
fi
echo -n "Did you purchase? (y|n) "
read purchased
if [[ "$purchased" =~ $yesReg ]]; then
purchased=true
else
purchased=false
fi
PGPASSWORD=$password psql --echo-all --dbname=$dbname --username=$user --command="UPDATE Records SET listened = $listened, liked = $liked, purchased = $purchased WHERE id = $recordid;"
}
purchaseRecord ()
{
echo -n "Which record did you purchase? "
read title
recordId=`PGPASSWORD=$password psql --dbname=$dbname --quiet --tuples-only --username=$user --command="SELECT id FROM Records WHERE title = lower('${title}');"`
if ! [[ $recordId != "" ]]; then
echo "Record doesnt exist [hit enter]"
read
return
fi
PGPASSWORD=$password psql --echo-all --dbname=$dbname --username=$user --command="UPDATE Records SET purchased = true WHERE id = $recordId;"
}
createDatabase ()
{
if [ "$1" != "" ]; then
dbname=$1
fi
if [ "$2" != "" ]; then
user=$2
fi
echo "creating music database named ${dbname} with user ${user}"
createdb --echo --owner=$user --username=$user $dbname
echo "creating tables ${tables}"
PGPASSWORD=$password psql --echo-all --dbname=$dbname --file=sql/create_tables.sql --username=$user
}
deleteDatabase ()
{
if [ "$1" != "" ]; then
dbname=$1
fi
if [ "$2" != "" ]; then
user=$2
fi
echo "deleting music database named ${dbname} for user ${user}"
dropdb --echo --username=$user $dbname
}
mainLoop ()
{
clear
while [ true ]; do
echo -n "a (add artist)|l (add label)|r (add record)|liked|listen|purchase| (q to quit)> "
read action
case $action in
a )
addArtist
;;
l )
addLabel
;;
r )
addRecord
;;
liked )
listLiked
;;
listen )
listenToRecord
;;
purchase )
purchaseRecord
;;
q | quit )
exit 0
;;
* )
echo -e "command not recognized\n"
esac
done
exit 0
}
myMusicUsage ()
{
echo -e "${clear}${bold}my-music${normal}\n\n"
echo -e "[[-c dbname user] | [-d dbname user] | [-h] | [-i]]\n\n"
echo "${tab}-c, --create - creates database${normal}"
echo "${tab}-d, --delete - deletes database${normal}. This cannot be undone."
echo "${tab}${tab} dbname [default=MyMusic] - Name of database to create"
echo "${tab}${tab} user [default=postgres] - Name of database owner and user"
echo "${tab}-h, --help - displays command line options${normal}"
echo "${tab}-i, --interactive - interactively add artists, labels, records or listen and purchase."
}
########### Main ##########
case $1 in
-i | --interactive )
mainLoop
;;
-c | --create )
createDatabase $2 $3
;;
-d | --delete )
deleteDatabase $2 $3
;;
-h | --help )
myMusicUsage
exit
;;
* )
myMusicUsage
exit 1
esac
echo "done"
exit 0