Browse code

jamendo-dl.sh - v0.0 - Creation

Fred authored on30/12/2014 23:49:28
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,295 @@
1
+#!/bin/bash
2
+# Title: jamendo-dl.sh
3
+# Version: 0.0
4
+# Author: Frédéric CHEVALIER <fcheval@txbiomedgenetics.org>
5
+# Created in: 2014-12-29
6
+# Modified in:
7
+# Licence : GPL v3
8
+
9
+
10
+
11
+#======#
12
+# Aims #
13
+#======#
14
+
15
+aim="Download albums from Jamendo.com"
16
+
17
+
18
+
19
+#==========#
20
+# Versions #
21
+#==========#
22
+
23
+# v0.0 - 2014-12-29: creation
24
+
25
+
26
+
27
+#===========#
28
+# Functions #
29
+#===========#
30
+
31
+# Usage message
32
+function usage {
33
+    echo -e "
34
+    \e[32m ${0##*/} \e[00m -i|--id value -f|--fmt value -l|--list file -z|--kzip -c|--cat -h|--help
35
+
36
+Aim: $aim
37
+
38
+Options:
39
+    -i, --id        id number of an album (eg., a1234) or an artist to download all his albums (eg., 5678).
40
+    -f, --fmt       format of music files (mp3, ogg, flac) [default: ogg]
41
+    -l, --list      text file with a list of ids to download (accept comments using #)
42
+    -k, --kzip      keep the zip files downloaded
43
+    -c, --cat       force regeneration of the Jamendo catalog (automatic if the
44
+                        catalog is older than 7 days) (useful when using artist id only)
45
+    -h, --help      this message
46
+   
47
+How to:
48
+    - To get album id, go on Jamendo website, choose an album, look at the address bar of the web browser
49
+        (eg., https://www.jamendo.com/fr/list/a122411/circles) and copy the id beginning by a (a122411)
50
+    - To get album id, go on Jamendo website, choose an album, look at the address bar of the web browser
51
+        (eg., https://www.jamendo.com/fr/artist/342854/alexander-franke) and copy the id with numbers only (342854)
52
+    "
53
+}
54
+
55
+
56
+# Info message
57
+function info {
58
+    if [[ -t 1 ]]
59
+    then
60
+        echo -e "\e[32mInfo:\e[00m $1"
61
+    else
62
+        echo -e "Info: $1"
63
+    fi
64
+}
65
+
66
+
67
+# Warning message
68
+function warning {
69
+    if [[ -t 1 ]]
70
+    then
71
+        echo -e "\e[33mWarning:\e[00m $1"
72
+    else
73
+        echo -e "Warning: $1"
74
+    fi
75
+}
76
+
77
+
78
+# Error message
79
+function error {
80
+    if [[ -t 1 ]]
81
+    then
82
+        echo -e "\e[31mError:\e[00m $1"
83
+        exit $2
84
+    else
85
+        echo -e "Error: $1"
86
+        exit $2
87
+    fi
88
+}
89
+
90
+
91
+# Dependency test
92
+function test_dep {
93
+    which $1 &> /dev/null
94
+    if [[ $? != 0 ]]
95
+    then
96
+        error "Package $1 is needed. Exiting..." 1
97
+    fi
98
+}
99
+
100
+
101
+# Album download
102
+# usage: album_dl $myid $myformat
103
+function album_dl {
104
+
105
+    # Name parsing
106
+    album_name=$(wget --spider http://storage-new.newjamendo.com/download/$1/$2/ 2>&1 | grep -m 1 "albums" | sed -n "s/.*albums\/\(.*\).zip.*/\1/p")
107
+    
108
+    if [[ ! $(echo "$album_name" | grep .) ]]
109
+    then
110
+		warning "The id \"$1\" does not correspond to an album. Skipping..."
111
+	else
112
+		# Download
113
+		info "Downloading of \"${album_name}\"..."
114
+		wget -c -O "${album_name}.zip" "http://storage-new.newjamendo.com/download/$1/$2/" &> /dev/null
115
+		if [[ $? != 0 ]]
116
+		then
117
+			warning "An error occurred during downloading. Skipping..."
118
+		fi
119
+
120
+		# Unzipping
121
+		info "Unzipping album..."
122
+		keep_tmp=0
123
+		if [[ ! -d "$album_name" ]]
124
+		then
125
+			unzip -d "$album_name" "${album_name}.zip" &> /dev/null
126
+			if [[ $? != 0 ]]
127
+			then
128
+				error "An error occurred during unzipping. Skipping..." 1
129
+			fi
130
+		else
131
+			warning "Destination directory already existing. Keep zip file. Skipping..."
132
+			keep_tmp=1
133
+		fi
134
+
135
+		# Removing zip file if nothing specified
136
+		if [[ -z $keep && $keep_tmp == 0 ]]
137
+		then
138
+			info "Deleting zip file..."
139
+			rm "${album_name}.zip"
140
+		fi
141
+	fi
142
+
143
+    echo ""
144
+}
145
+
146
+
147
+# Download of complet album list from a given artist
148
+# usage: all_albums_dl $myid $myformat
149
+function all_albums_dl {
150
+
151
+    # Download the Jamendo catalog if needed
152
+    mycat_file="dbdump_artistalbumtrack.xml.gz"
153
+    if [[ -n $mycat || ! -e "/tmp/$mycat_file" || $(find /tmp/ -name "$mycat_file" -type f -ctime +7) ]]
154
+    then
155
+        mycat="/tmp/$mycat_file"
156
+        info "Download of the Jamendo catalog. May take a while...\n"
157
+        wget -O "$mycat" "http://imgjam.com/data/dbdump_artistalbumtrack.xml.gz" &> /dev/null
158
+    else
159
+        mycat="/tmp/$mycat_file"
160
+        info "Jamendo catalog already there and not older than 7 days...\n"
161
+    fi
162
+
163
+    # Parsing the album ids
164
+    mylist_tmp=$(zcat "$mycat" | grep "<artist><id>"$1"</id>" | grep -o "<album><id>[0-9]*</id>")
165
+    if [[ ! $(echo "$mylist_tmp" | grep .) ]]
166
+    then
167
+		warning "The id \"$1\" does not correspond to any artist. Skipping...\n"
168
+	fi
169
+
170
+    # Downloading each album
171
+    for i in $mylist_tmp
172
+    do
173
+        myid_tmp="a$(echo $i | sed -n "s/<album><id>\(.*\)<\/id>/\1/p")"
174
+        album_dl "$myid_tmp" "$2"
175
+    done
176
+
177
+}
178
+
179
+
180
+# Test id
181
+# usage: test_id $myid $myformat
182
+function test_id {
183
+	if [[ $(echo "$1" | grep "^[0-9]") ]]
184
+    then
185
+        all_albums_dl "$1" "$2"
186
+    elif [[ $(echo "$1" | grep "^a") ]]
187
+    then
188
+        album_dl "$1" "$2"
189
+    elif [[ ! $(echo "$1" | grep "^[0-9]\|^a") ]]
190
+    then
191
+		warning "The id \"$1\" is not well formated. Skipping...\n"
192
+		continue
193
+    fi
194
+}
195
+
196
+
197
+
198
+#==============#
199
+# Dependencies #
200
+#==============#
201
+
202
+test_dep wget
203
+test_dep unzip
204
+
205
+
206
+
207
+#===========#
208
+# Variables #
209
+#===========#
210
+
211
+# Load variables
212
+while [[ $# -gt 0 ]]
213
+do
214
+    case $1 in
215
+        -i|--id     ) myid="$2" ; shift 2 ;;
216
+        -f|--fmt    ) myformat="$2"  
217
+                        if [[ "$myformat" == mp3 ]]
218
+                        then
219
+                            myformat=mp32
220
+                        elif [[ "$myformat" == ogg ]]
221
+                        then
222
+                            myformat=ogg2
223
+                        fi                                
224
+                        shift 2 ;;
225
+        -l|--list   ) mylist="$2" ; shift 2 ;;
226
+        -k|--kzip   ) keep=1 ; shift ;;
227
+        -c|--cat    ) mycat=1 ; shift ;;
228
+        -h|--help   ) usage ; exit 0 ;;
229
+        *           ) error "Invalid option: $1\n$(usage)" 1 ;;
230
+    esac
231
+done
232
+
233
+
234
+# Check the existence of obligatory options
235
+if [[ -z "$myid" && -z "$mylist" ]]
236
+then
237
+    error "The options -i or -l are required. Exiting...\n$(usage)" 1
238
+fi
239
+
240
+if [[ -n "$myid" && -n "$mylist" ]]
241
+then
242
+    error "The options -i and -l cannot be used at the same time. Exiting...\n$(usage)" 1
243
+fi
244
+
245
+if [[ -z "$myformat" ]]
246
+then
247
+    myformat="ogg2"
248
+elif [[ "$myformat" != "mp32" && "$myformat" != "ogg2" && "$myformat" != "flac" ]]
249
+then
250
+    error "The format can only be mp3, ogg or flac. Exiting..." 1
251
+fi
252
+
253
+if [[ $(echo "$myid" | grep "^a") && -n $all ]]
254
+then
255
+    error "The options -a cannot be used when downloading a single album. Exiting...\n$(usage)" 1
256
+fi
257
+
258
+
259
+
260
+#============#
261
+# Processing #
262
+#============#
263
+
264
+# Test file type of the list
265
+if [[ -n "$mylist" && ! -e "$mylist" ]]
266
+then
267
+	error "The file \"$mylist\" does not exist. Exiting..." 1
268
+elif [[ -n "$mylist" && ! $(file -b --mime-type "$mylist" | grep text) ]]
269
+then
270
+	error "The list file \"$mylist\" is not a text file. Exiting..." 1
271
+fi
272
+
273
+
274
+# Download albums
275
+if [[ -n "$myid" ]]
276
+then
277
+	test_id "$myid" "$myformat"
278
+elif [[ -n "$mylist" ]]
279
+then
280
+    while read myid
281
+    do
282
+		myid=$(echo "$myid" | sed "s/#.*$// ; s/ * //")
283
+		echo $myid
284
+		if [[ ! $(echo "$myid" | grep .) ]]
285
+		then
286
+			continue
287
+		fi
288
+				
289
+		test_id "$myid" "$myformat"
290
+       
291
+    done < "$mylist"
292
+fi
293
+
294
+
295
+exit 0