-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
build.sh
executable file
·1514 lines (1439 loc) · 46.8 KB
/
build.sh
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
#
#SPDX-License-Identifier: GPL-3.0-or-later
#myMPD (c) 2021-2022 Juergen Mang <mail@jcgames.de>
#https://github.com/jcorporation/radiodb
set -uo pipefail
#print out commands
[ -z "${DEBUG+x}" ] || set -x
PUBLISH_DIR="docs/db"
SOURCES_DIR="sources"
TRASH_DIR="trash"
PICS_DIR="${PUBLISH_DIR}/pics"
PLS_DIR="${PUBLISH_DIR}/webradios"
INDEXFILE="${PUBLISH_DIR}/index/webradios.min.json"
INDEXFILE_COMBINED="${PUBLISH_DIR}/index/webradiodb-combined.min.json"
INDEXFILE_JS="${PUBLISH_DIR}/index/webradiodb-combined.min.js"
STATUSFILE="${PUBLISH_DIR}/index/status.min.json"
LANGFILE="${PUBLISH_DIR}/index/languages.min.json"
COUNTRYFILE="${PUBLISH_DIR}/index/countries.min.json"
STATEFILE="${PUBLISH_DIR}/index/states.min.json"
REGIONFILE="${PUBLISH_DIR}/index/regions.min.json"
GENREFILE="${PUBLISH_DIR}/index/genres.min.json"
CODECFILE="${PUBLISH_DIR}/index/codecs.min.json"
BITRATEFILE="${PUBLISH_DIR}/index/bitrates.min.json"
MOODE_PICS_DIR="${SOURCES_DIR}/moode-pics"
MOODE_PLS_DIR="${SOURCES_DIR}/moode-webradios"
MYMPD_PICS_DIR="${SOURCES_DIR}/mympd-pics"
MYMPD_PLS_DIR="${SOURCES_DIR}/mympd-webradios"
source ./mappings/country_map.sh
source ./mappings/genre_map.sh
source ./mappings/language_map.sh
source ./mappings/m3ufields_map.sh
source ./mappings/region_map.sh
# Checks if string starts with http:// or https://
is_uri() {
local CHECK_URI="$1"
[ "${CHECK_URI:0:7}" = "http://" ] && return 0
[ "${CHECK_URI:0:8}" = "https://" ] && return 0
return 1
}
# Checks if string is an unsigned number
is_uint() {
case "$1" in
"" | *[!0-9]*)
return 1
;;
esac
return 0
}
# Upper cases the complete string
ucstring() {
local var="$*"
printf '%s' "${var^^}"
}
# Upper cases the first character of each word
ucwords() {
local var="$*"
#shellcheck disable=SC2206
array=( $var )
#shellcheck disable=SC2124
var="${array[@]^}"
printf '%s' "$var"
}
# Trims whitespaces from start and end of a string
trim() {
local var="$*"
# remove leading whitespace characters
var="${var#"${var%%[![:space:]]*}"}"
# remove trailing whitespace characters
var="${var%"${var##*[![:space:]]}"}"
printf '%s' "$var"
}
# Trims the path from a filename
trim_path() {
local var="$1"
printf '%s' "${var##*/}"
}
# Trims the extension from a filename
trim_ext() {
local var="$1"
local ext="$2"
printf '%s' "${var%%.${ext}*}"
}
# Generates a myMPD compatible m3u filename, by replacings special chars with underscore
gen_m3u_name() {
sed -E -e 's/[<>/.:?&$%!#\\|;=]/_/g' <<< "$1"
}
# Gets a m3u field value
get_m3u_field() {
local M3U_FILE="$1"
local M3U_FIELD="$2"
local LINE
LINE=$(grep "^#${M3U_FIELD}:" "$M3U_FILE")
echo "${LINE#*:}"
}
# Downloads an image from an uri, converts and resizes it
download_image() {
local DOWNLOAD_URI="$1"
local DOWNLOAD_DST="$2"
echo "Downloading image: \"$DOWNLOAD_URI\""
if ! curl -kfsSL "$DOWNLOAD_URI" --output "${DOWNLOAD_DST}.image"
then
rm -f "${DOWNLOAD_DST}.image"
return 1
fi
if ! convert "${DOWNLOAD_DST}.image" "${DOWNLOAD_DST}.webp"
then
rm -f "${DOWNLOAD_DST}.image"
echo "Converting image to webp failed"
return 1
fi
rm -f "${DOWNLOAD_DST}.image"
if ! resize_image "${DOWNLOAD_DST}.webp"
then
return 1
fi
return 0
}
# Resize an image
resize_image() {
local RESIZE_FILE="$1"
local TO_SIZE="400x400"
[ -s "$RESIZE_FILE" ] || exit 1
#get actual size
local CUR_SIZE
CUR_SIZE=$(identify -format "%wx%h" "$RESIZE_FILE")
if [ "${CUR_SIZE}" != "${TO_SIZE}" ]
then
if convert "$RESIZE_FILE" -resize "$TO_SIZE^" -gravity center -extent "$TO_SIZE" "$RESIZE_FILE.resize"
then
mv "$RESIZE_FILE.resize" "$RESIZE_FILE"
else
echo "Error resizing $RESIZE_FILE"
rm -f "$RESIZE_FILE.resize"
return 1
fi
fi
return 0
}
# Normalizes GENRE, CODEC, COUNTRY, REGION, LANGUAGE in all m3u's in specified folder
normalize_fields() {
local DIR=$1
local F
for F in "$DIR"/*.m3u
do
# genres
local GENRE_LINE=""
GENRE_LINE=$(get_m3u_field "$F" "EXTGENRE")
local NEW_GENRE=""
local GENRE=""
while read -r -d, GENRE
do
[ -z "$GENRE" ] && continue
[ "$GENRE" = "&" ] && continue
local REPLACE_GENRE="${genre_map[$GENRE]:-}"
if [ -n "$REPLACE_GENRE" ]
then
REPLACE_GENRE=$(ucwords "$REPLACE_GENRE")
NEW_GENRE="$NEW_GENRE, $REPLACE_GENRE"
else
GENRE=$(ucwords "$GENRE")
NEW_GENRE="$NEW_GENRE, $GENRE"
fi
done < <(sed 's/, /,/g' <<< "$GENRE_LINE,")
NEW_GENRE="${NEW_GENRE:2}"
if [ "$GENRE_LINE" != "$NEW_GENRE" ]
then
echo "$F: $GENRE_LINE -> $NEW_GENRE"
sed -i -e "s/^#EXTGENRE:.*/#EXTGENRE:$NEW_GENRE/" "$F"
fi
# codec
local CODEC=""
CODEC=$(get_m3u_field "$F" "CODEC")
local CODEC_UPPER
CODEC_UPPER=$(ucstring "$CODEC")
CODEC_UPPER=$(trim "$CODEC_UPPER")
if [ "$CODEC" != "$CODEC_UPPER" ]
then
echo "$F: $CODEC -> $CODEC_UPPER"
sed -i -e "s/^#CODEC:.*/#CODEC:$CODEC_UPPER/" "$F"
fi
# country
local COUNTRY=""
COUNTRY=$(get_m3u_field "$F" "COUNTRY")
local COUNTRY_UPPER
COUNTRY_UPPER=$(ucwords "$COUNTRY")
COUNTRY_UPPER=$(trim "$COUNTRY_UPPER")
local REPLACE_COUNTRY="${country_map[$COUNTRY_UPPER]:-}"
if [ -n "$REPLACE_COUNTRY" ]
then
echo "$F: $COUNTRY -> $REPLACE_COUNTRY"
sed -i -e "s/^#COUNTRY:.*/#COUNTRY:$REPLACE_COUNTRY/" "$F"
elif [ "$COUNTRY" != "$COUNTRY_UPPER" ]
then
echo "$F: $COUNTRY -> $COUNTRY_UPPER"
sed -i -e "s/^#COUNTRY:.*/#COUNTRY:$COUNTRY_UPPER/" "$F"
fi
# state
local STATE=""
STATE=$(get_m3u_field "$F" "STATE")
if [ -n "$STATE" ]
then
local STATE_UPPER
STATE_UPPER=$(ucwords "$STATE")
STATE_UPPER=$(trim "$STATE_UPPER")
local REPLACE_STATE="${region_map[$STATE_UPPER]:-}"
if [ -n "$REPLACE_STATE" ]
then
echo "$F: $STATE -> $REPLACE_STATE"
sed -i -e "s/^#STATE:.*/#STATE:$REPLACE_STATE/" "$F"
elif [ "$STATE" != "$STATE_UPPER" ]
then
echo "$F: $STATE -> $STATE_UPPER"
sed -i -e "s/^#STATE:.*/#STATE:$STATE_UPPER/" "$F"
fi
fi
# region
local REGION=""
REGION=$(get_m3u_field "$F" "REGION")
if [ -n "$REGION" ]
then
local REGION_UPPER
REGION_UPPER=$(ucwords "$REGION")
REGION_UPPER=$(trim "$REGION_UPPER")
local REPLACE_REGION="${region_map[$REGION_UPPER]:-}"
if [ -n "$REPLACE_REGION" ]
then
echo "$F: $REGION -> $REPLACE_REGION"
sed -i -e "s/^#REGION:.*/#REGION:$REPLACE_REGION/" "$F"
elif [ "$REGION" != "$REGION_UPPER" ]
then
echo "$F: $REGION -> $REGION_UPPER"
sed -i -e "s/^#REGION:.*/#REGION:$REGION_UPPER/" "$F"
fi
fi
# language
local LANGUAGE_LINE=""
LANGUAGE_LINE=$(get_m3u_field "$F" "LANGUAGE")
local NEW_LANGUAGE=""
local LANGUAGE=""
while read -r -d, LANGUAGE
do
[ -z "$LANGUAGE" ] && continue
[ "$LANGUAGE" = "&" ] && continue
LANGUAGE=$(ucwords "$LANGUAGE")
local REPLACE_LANGUAGE="${language_map[$LANGUAGE]:-}"
if [ -n "$REPLACE_LANGUAGE" ]
then
LANGUAGE="$REPLACE_LANGUAGE"
fi
NEW_LANGUAGE="$NEW_LANGUAGE, $LANGUAGE"
done < <(sed 's/, /,/g' <<< "$LANGUAGE_LINE,")
NEW_LANGUAGE="${NEW_LANGUAGE:2}"
if [ "$LANGUAGE_LINE" != "$NEW_LANGUAGE" ]
then
echo "$F: $LANGUAGE_LINE -> $NEW_LANGUAGE"
sed -i -e "s/^#LANGUAGE:.*/#LANGUAGE:$NEW_LANGUAGE/" "$F"
fi
done
}
# Sync with moode audio webradio list
sync_moode() {
echo "Syncing moode audio webradios"
# fields of cfg_radios are:
# id, station, name, type, logo, genre, broadcaster, language, country, region, bitrate, codec, geo_fenced, home_page, reserved2
local MOODE_DB="https://raw.githubusercontent.com/moode-player/moode/master/var/local/www/db/moode-sqlite3.db.sql"
local MOODE_IMAGES="https://raw.githubusercontent.com/moode-player/moode/master/var/local/www/imagesw/radio-logos/"
# start with clean output dirs
rm -fr "$MOODE_PLS_DIR"
mkdir "$MOODE_PLS_DIR"
mv "$MOODE_PICS_DIR" "${MOODE_PICS_DIR}.old"
mkdir "$MOODE_PICS_DIR"
# fetch the sql file and grep the webradio stations and convert it to csv
local I=0
local S=0
local LINE
while read -r LINE
do
# LINE is a csv: station, name, genre, language, country, bitrate, codec, homepage
# create the same plist name as myMPD
local PLIST
PLIST=$(csvcut -c 1 <<< "$LINE")
PLIST=$(gen_m3u_name "$PLIST")
if grep -q "$PLIST" mappings/moode-ignore
then
printf "s"
S=$((S+1))
continue
fi
# extract fields
local STATION
STATION=$(csvcut -c 1 <<< "$LINE" | sed -e s/\"//g)
local NAME
NAME=$(csvcut -c 2 <<< "$LINE" | sed -e s/\"//g)
local IMAGE
IMAGE=$(csvcut -c 3 <<< "$LINE" | sed -e s/\"//g)
local GENRE
GENRE=$(csvcut -c 4 <<< "$LINE" | sed -e s/\"//g)
local LANGUAGE
LANGUAGE=$(csvcut -c 5 <<< "$LINE" | sed -e s/\"//g)
local COUNTRY
COUNTRY=$(csvcut -c 6 <<< "$LINE" | sed -e s/\"//g)
local BITRATE
BITRATE=$(csvcut -c 7 <<< "$LINE" | sed -e s/\"//g)
local CODEC
CODEC=$(csvcut -c 8 <<< "$LINE" | sed -e s/\"//g)
local HOMEPAGE
HOMEPAGE=$(csvcut -c 9 <<< "$LINE" | sed -e s/\"//g)
# get dates
local ADDED
local LASTMODIFIED
if [ -f "$PLS_DIR/$PLIST.m3u" ]
then
ADDED=$(get_m3u_field "$PLS_DIR/$PLIST.m3u" "ADDED")
[ -z "$ADDED" ] && ADDED=$(date +%s)
LASTMODIFIED=$(get_m3u_field "$PLS_DIR/$PLIST.m3u" "LASTMODIFIED")
[ -z "$LASTMODIFIED" ] && LASTMODIFIED=$(date +%s)
else
ADDED=$(date +%s)
LASTMODIFIED=$ADDED
fi
# get images
local NAME_ENCODED
NAME_ENCODED=$(jq -rn --arg x "$NAME" '$x | @uri')
if [ "$IMAGE" = "local" ]
then
if [ -s "${MOODE_PICS_DIR}.old/${PLIST}.webp" ]
then
cp "${MOODE_PICS_DIR}.old/${PLIST}.webp" "${MOODE_PICS_DIR}/${PLIST}.webp"
IMAGE="${PLIST}.webp"
elif download_image "${MOODE_IMAGES}${NAME_ENCODED}.jpg" "${MOODE_PICS_DIR}/${PLIST}"
then
IMAGE="${PLIST}.webp"
else
IMAGE=""
fi
elif download_image "${MOODE_IMAGES}${NAME_ENCODED}.jpg" "${MOODE_PICS_DIR}/${PLIST}"
then
IMAGE="${PLIST}.webp"
else
IMAGE=""
fi
# write ext m3u with custom myMPD fields
cat > "${MOODE_PLS_DIR}/${PLIST}.m3u" << EOL
#EXTM3U
#EXTINF:-1,$NAME
#EXTGENRE:$GENRE
#PLAYLIST:$NAME
#EXTIMG:$IMAGE
#HOMEPAGE:$HOMEPAGE
#COUNTRY:$COUNTRY
#STATE:
#REGION:
#LANGUAGE:$LANGUAGE
#DESCRIPTION:
#CODEC:$CODEC
#BITRATE:$BITRATE
#ADDED:$ADDED
#LASTMODIFIED:$LASTMODIFIED
$STATION
EOL
printf "."
I=$((I+1))
done < <(wget -q "$MOODE_DB" -O - | \
grep "INSERT INTO cfg_radio" | \
awk -F "VALUES " '{print $2}' | \
sed -e 's/^(//' -e 's/);//' -e "s/', /',/g" -e "s/, '/,'/g" | \
csvcut -q \' -c 2,3,5,6,8,9,11,12,14 |
grep -v -E "(OFFLINE|zx reserved 499)")
rm -fr "${MOODE_PICS_DIR}.old"
echo ""
echo "$I webradios synced"
echo "$S webradios skipped"
normalize_fields "${MOODE_PLS_DIR}"
for F in "${MOODE_PLS_DIR}/"*.m3u
do
if [ -f "$PLS_DIR/$F.m3u" ]
then
if ! diff "${MOODE_PLS_DIR}/$F" "$PLS_DIR/$F" > /dev/null
then
TS=$(get_lastmodified_git "$PLS_DIR/$F")
sed -i -E "s/#LASTMODIFIED:.*/#LASTMODIFIED:$TS/" "$PLS_DIR/$F"
fi
fi
done
}
add_radio() {
# get the uri and write out a skeletion file
read -r -p "URI: " URI
# create the same plist name as myMPD
local PLIST
PLIST=$(gen_m3u_name "$URI")
if [ -f "${MYMPD_PLS_DIR}/${PLIST}.m3u" ]
then
echo "This webradio already exists."
exit 1
fi
local ADDED
ADDED=$(date +%s)
local LASTMODIFIED=$ADDED
# write ext m3u with custom myMPD fields
cat > "${MYMPD_PLS_DIR}/${PLIST}.m3u" << EOL
#EXTM3U
#EXTINF:-1,<name>
#EXTGENRE:<genre>
#PLAYLIST:<name>
#EXTIMG:${PLIST}.webp
#HOMEPAGE:<homepage>
#COUNTRY:<country>
#STATE:<state>
#REGION:<region>
#LANGUAGE:<language>
#DESCRIPTION:<description>
#CODEC:<codec>
#BITRATE:<bitrate>
#ADDED:$ADDED
#LASTMODIFIED:$LASTMODIFIED
$URI
EOL
echo ""
echo "Playlist file ${MYMPD_PLS_DIR}/$PLIST.m3u created."
echo "Edit it to add details."
echo "You shoud add an image with the name $PLIST.webp to the ${MYMPD_PICS_DIR} folder"
echo "or replace the #EXTIMG tag with an url to the image."
echo ""
}
# Creates a m3u from an issue json file
add_radio_from_json() {
local INPUT="$1"
local URI
URI=$(jq -r ".streamuri" < "$INPUT" | head -1 | tr -d '\n')
local NAME
NAME=$(jq -r ".name" < "$INPUT" | head -1 | tr -d '\n')
local GENRE
GENRE=$(jq -r ".genre" < "$INPUT" | head -1 | tr -d '\n' | sed -E -e 's/;\s?/, /g' -e 's/,(\S)/, \1/g')
local IMAGE
IMAGE=$(jq -r ".image" < "$INPUT" | head -1 | tr -d '\n')
local HOMEPAGE
HOMEPAGE=$(jq -r ".homepage" < "$INPUT" | head -1 | tr -d '\n')
local COUNTRY
COUNTRY=$(jq -r ".country" < "$INPUT" | head -1 | tr -d '\n')
local REGION
REGION=$(jq -r ".region" < "$INPUT" | head -1 | tr -d '\n')
local LANGUAGE
LANGUAGE=$(jq -r ".language" < "$INPUT" | head -1 | tr -d '\n')
local DESCRIPTION
DESCRIPTION=$(jq -r ".description" < "$INPUT" | head -1 | tr -d '\n')
local CODEC
CODEC=$(jq -r ".codec" < "$INPUT" | head -1 | tr -d '\n')
local BITRATE
BITRATE=$(jq -r ".bitrate" < "$INPUT" | head -1 | tr -d '\n')
if [ -n "$BITRATE" ] && ! is_uint "$BITRATE"
then
echo "Bitrate must be an unsigned value"
exit 1
fi
# create the same plist name as myMPD
local PLIST
PLIST=$(gen_m3u_name "$URI")
echo "Adding webradio $PLIST"
if [ -f "${MYMPD_PLS_DIR}/${PLIST}.m3u" ]
then
echo "This webradio already exists."
exit 1
fi
if [ -n "$IMAGE" ] && is_uri "$IMAGE"
then
if download_image "$IMAGE" "${MYMPD_PICS_DIR}/${PLIST}"
then
IMAGE="${PLIST}.webp"
else
echo "Download of image has failed"
exit 1
fi
else
echo "Image is not an uri, skipping download"
IMAGE=""
fi
local ADDED
ADDED=$(date +%s)
local LASTMODIFIED=$ADDED
echo "Writing ${PLIST}.m3u"
cat > "${MYMPD_PLS_DIR}/${PLIST}.m3u" << EOL
#EXTM3U
#EXTINF:-1,$NAME
#EXTGENRE:$GENRE
#PLAYLIST:$NAME
#EXTIMG:$IMAGE
#HOMEPAGE:$HOMEPAGE
#COUNTRY:$COUNTRY
#STATE:$REGION
#REGION:$REGION
#LANGUAGE:$LANGUAGE
#DESCRIPTION:$DESCRIPTION
#CODEC:$CODEC
#BITRATE:$BITRATE
#ADDED:$ADDED
#LASTMODIFIED:$LASTMODIFIED
$URI
EOL
}
# Modifies a m3u from an issue json file
modify_radio_from_json() {
local INPUT="$1"
#Webradio to modify
local MODIFY_URI
MODIFY_URI=$(jq -r ".modifyWebradio" < "$INPUT")
local MODIFY_PLIST
MODIFY_PLIST=$(gen_m3u_name "$MODIFY_URI")
if [ ! -f "${MYMPD_PLS_DIR}/${MODIFY_PLIST}.m3u" ]
then
if [ -f "${MOODE_PLS_DIR}/${MODIFY_PLIST}.m3u" ]
then
echo "Move webradio from moode to mympd"
mv "${MOODE_PLS_DIR}/${MODIFY_PLIST}.m3u" "${MYMPD_PLS_DIR}/${MODIFY_PLIST}.m3u"
[ -f "${MOODE_PICS_DIR}/${MODIFY_PLIST}.webp" ] && \
mv "${MOODE_PICS_DIR}/${MODIFY_PLIST}.webp" "${MYMPD_PICS_DIR}/${MODIFY_PLIST}.webp"
#add moode radio ignore
echo "${MODIFY_PLIST}" >> mappings/moode-ignore
else
echo "Webradio ${MODIFY_PLIST} not found"
exit 1
fi
fi
echo "Modifying webradio $MODIFY_PLIST"
#New values
local NEW_URI
NEW_URI=$(jq -r ".streamuri" < "$INPUT" | head -1 | tr -d '\n')
local NEW_NAME
NEW_NAME=$(jq -r ".name" < "$INPUT" | head -1 | tr -d '\n')
local NEW_GENRE
NEW_GENRE=$(jq -r ".genre" < "$INPUT" | head -1 | tr -d '\n' | sed -E -e 's/;\s?/, /g' -e 's/,(\S)/, \1/g')
local NEW_IMAGE
NEW_IMAGE=$(jq -r ".image" < "$INPUT" | head -1 | tr -d '\n')
local NEW_HOMEPAGE
NEW_HOMEPAGE=$(jq -r ".homepage" < "$INPUT" | head -1 | tr -d '\n')
local NEW_COUNTRY
NEW_COUNTRY=$(jq -r ".country" < "$INPUT" | head -1 | tr -d '\n')
local NEW_REGION
NEW_REGION=$(jq -r ".region" < "$INPUT" | head -1 | tr -d '\n')
local NEW_LANGUAGE
NEW_LANGUAGE=$(jq -r ".language" < "$INPUT" | head -1 | tr -d '\n')
local NEW_DESCRIPTION
NEW_DESCRIPTION=$(jq -r ".description" < "$INPUT" | head -1 | tr -d '\n')
local NEW_CODEC
NEW_CODEC=$(jq -r ".codec" < "$INPUT" | head -1 | tr -d '\n')
local NEW_BITRATE
NEW_BITRATE=$(jq -r ".bitrate" < "$INPUT" | head -1 | tr -d '\n')
if [ -n "$NEW_BITRATE" ] && ! is_uint "$NEW_BITRATE"
then
echo "Bitrate must be an unsigned value"
exit 1
fi
local NEW_PLIST
NEW_PLIST=$(gen_m3u_name "$NEW_URI")
#Get old values
local OLD_NAME
OLD_NAME=$(get_m3u_field "${MYMPD_PLS_DIR}/${MODIFY_PLIST}.m3u" "PLAYLIST")
local OLD_GENRE
OLD_GENRE=$(get_m3u_field "${MYMPD_PLS_DIR}/${MODIFY_PLIST}.m3u" "EXTGENRE")
local OLD_IMAGE
OLD_IMAGE=$(get_m3u_field "${MYMPD_PLS_DIR}/${MODIFY_PLIST}.m3u" "EXTIMG")
local OLD_HOMEPAGE
OLD_HOMEPAGE=$(get_m3u_field "${MYMPD_PLS_DIR}/${MODIFY_PLIST}.m3u" "HOMEPAGE")
local OLD_COUNTRY
OLD_COUNTRY=$(get_m3u_field "${MYMPD_PLS_DIR}/${MODIFY_PLIST}.m3u" "COUNTRY")
local OLD_REGION
OLD_REGION=$(get_m3u_field "${MYMPD_PLS_DIR}/${MODIFY_PLIST}.m3u" "REGION")
local OLD_LANGUAGE
OLD_LANGUAGE=$(get_m3u_field "${MYMPD_PLS_DIR}/${MODIFY_PLIST}.m3u" "LANGUAGE")
local OLD_DESCRIPTION
OLD_DESCRIPTION=$(get_m3u_field "${MYMPD_PLS_DIR}/${MODIFY_PLIST}.m3u" "DESCRIPTION")
local OLD_CODEC
OLD_CODEC=$(get_m3u_field "${MYMPD_PLS_DIR}/${MODIFY_PLIST}.m3u" "CODEC")
local OLD_BITRATE
OLD_BITRATE=$(get_m3u_field "${MYMPD_PLS_DIR}/${MODIFY_PLIST}.m3u" "BITRATE")
local ADDED
ADDED=$(get_m3u_field "${MYMPD_PLS_DIR}/${MODIFY_PLIST}.m3u" "ADDED")
if [ "$MODIFY_PLIST" != "$NEW_PLIST" ] && [ -f "${MYMPD_PLS_DIR}/${NEW_PLIST}.m3u" ]
then
echo "A webradio for the new streamuri already exists."
exit 1
fi
#set new plist name
[ -z "$NEW_PLIST" ] && NEW_PLIST="$MODIFY_PLIST"
if [ "$MODIFY_PLIST" != "$NEW_PLIST" ]
then
rm "${MYMPD_PLS_DIR}/${MODIFY_PLIST}.m3u"
mv "${MYMPD_PICS_DIR}/${MODIFY_PLIST}.webp" "${MYMPD_PICS_DIR}/${NEW_PLIST}.webp"
NEW_IMAGE="${NEW_PLIST}.webp"
#rename alternative streams
if [ "$(echo "${MYMPD_PLS_DIR}/$MODIFY_PLIST."*)" != "${MYMPD_PLS_DIR}/$MODIFY_PLIST.*" ]
then
for S in "${MYMPD_PLS_DIR}/$MODIFY_PLIST."*
do
G=${S//$MODIFY_PLIST/$NEW_PLIST}
mv -v "$S" "$G"
done
fi
fi
#get changed image
if [ "$NEW_IMAGE" != "$OLD_IMAGE" ] && [ -n "$NEW_IMAGE" ]
then
echo "Image has changed"
rm -f "$OLD_IMAGE"
if is_uri "$NEW_IMAGE"
then
if download_image "$NEW_IMAGE" "${MYMPD_PICS_DIR}/${NEW_PLIST}"
then
NEW_IMAGE="${NEW_PLIST}.webp"
else
echo "Download of image has failed"
exit 1
fi
else
echo "Image is not an uri, skipping download"
fi
fi
#dates
local LASTMODIFIED
LASTMODIFIED=$(date +%s)
echo "Setting new values"
#merge other values
[ -z "$NEW_URI" ] && NEW_URI="$MODIFY_URI"
[ -z "$NEW_NAME" ] && NEW_NAME="$OLD_NAME"
[ -z "$NEW_IMAGE" ] && NEW_IMAGE="$OLD_IMAGE"
[ -z "$NEW_GENRE" ] && NEW_GENRE="$OLD_GENRE"
[ -z "$NEW_HOMEPAGE" ] && NEW_HOMEPAGE="$OLD_HOMEPAGE"
[ -z "$NEW_COUNTRY" ] && NEW_COUNTRY="$OLD_COUNTRY"
[ -z "$NEW_REGION" ] && NEW_REGION="$OLD_REGION"
[ -z "$NEW_LANGUAGE" ] && NEW_LANGUAGE="$OLD_LANGUAGE"
[ -z "$NEW_DESCRIPTION" ] && NEW_DESCRIPTION="$OLD_DESCRIPTION"
[ -z "$NEW_CODEC" ] && NEW_CODEC="$OLD_CODEC"
[ -z "$NEW_BITRATE" ] && NEW_BITRATE="$OLD_BITRATE"
echo "Writing ${NEW_PLIST}.m3u"
cat > "${MYMPD_PLS_DIR}/${NEW_PLIST}.m3u" << EOL
#EXTM3U
#EXTINF:-1,$NEW_NAME
#EXTGENRE:$NEW_GENRE
#PLAYLIST:$NEW_NAME
#EXTIMG:$NEW_IMAGE
#HOMEPAGE:$NEW_HOMEPAGE
#COUNTRY:$NEW_COUNTRY
#STATE:$NEW_REGION
#REGION:$NEW_REGION
#LANGUAGE:$NEW_LANGUAGE
#DESCRIPTION:$NEW_DESCRIPTION
#CODEC:$NEW_CODEC
#BITRATE:$NEW_BITRATE
#ADDED:$ADDED
#LASTMODIFIED:$LASTMODIFIED
$NEW_URI
EOL
}
# Deletes a m3u from an issue json file
delete_radio_from_json() {
local INPUT="$1"
local URI
URI=$(jq -r ".deleteWebradio" < "$INPUT")
local PLIST
PLIST=$(gen_m3u_name "$URI")
delete_radio_by_m3u "$PLIST"
}
# Deletes a m3u by uri
delete_radio_by_m3u() {
local PLIST=$1
if [ -f "${MYMPD_PLS_DIR}/${PLIST}.m3u" ]
then
echo "Deleting webradio ${PLIST}"
if mv "${MYMPD_PLS_DIR}/${PLIST}.m3u"* "${TRASH_DIR}"
then
[ -f "${MYMPD_PICS_DIR}/${PLIST}.webp" ] && mv "${MYMPD_PICS_DIR}/${PLIST}.webp" "${TRASH_DIR}"
else
exit 1
fi
elif [ -f "${MOODE_PLS_DIR}/${PLIST}.m3u" ]
then
echo "Deleting webradio ${PLIST}"
if mv "${MOODE_PLS_DIR}/${PLIST}.m3u" "${TRASH_DIR}"
then
[ -f "${MOODE_PICS_DIR}/${PLIST}.webp" ] && mv "${MOODE_PICS_DIR}/${PLIST}.webp" "${TRASH_DIR}"
else
exit 1
fi
echo "Adding webradio $PLIST to moode-ignore"
echo "${PLIST}" >> mappings/moode-ignore
else
echo "Webradio ${PLIST} not found"
exit 1
fi
}
# Adds an alternate stream m3u from an issue json file
add_alternate_stream_from_json() {
local INPUT="$1"
local WEBRADIO
WEBRADIO=$(jq -r ".modifyWebradio" < "$INPUT")
# create the same plist name as myMPD
local PLIST
PLIST=$(gen_m3u_name "$WEBRADIO")
local URI
URI=$(jq -r ".streamuri" < "$INPUT" | head -1 | tr -d '\n')
local CODEC
CODEC=$(jq -r ".codec" < "$INPUT" | head -1 | tr -d '\n')
local BITRATE
BITRATE=$(jq -r ".bitrate" < "$INPUT" | head -1 | tr -d '\n')
echo "Writing ${PLIST}.m3u.$CODEC.$BITRATE"
cat > "${MYMPD_PLS_DIR}/${PLIST}.m3u.$CODEC.$BITRATE" << EOL
#CODEC:$CODEC
#BITRATE:$BITRATE
$URI
EOL
# Update last-modified
set_lastmodified "${MYMPD_PLS_DIR}/${PLIST}.m3u"
}
# Deletes an alternate stream m3u from an issue json file
delete_alternate_stream_from_json() {
local INPUT="$1"
local TO_DELETE
TO_DELETE=$(jq -r ".deleteAlternateStream" < "$INPUT")
mv "${MYMPD_PLS_DIR}/${TO_DELETE}" "${TRASH_DIR}"
local PARENT=${TO_DELETE%%.m3u*}
# Update last-modified
set_lastmodified "${MYMPD_PLS_DIR}/${PARENT}.m3u"
}
# Updates the last-modified field
set_lastmodified() {
local LASTMODIFIED
LASTMODIFIED=$(date +%s)
sed -i -E "s/#LASTMODIFIED:.*/#LASTMODIFIED:$LASTMODIFIED/" "$1"
}
# Gets the last-modified from git
get_lastmodified_git() {
git log -1 --pretty="format:%ct" "$1"
}
# Quotes supplied string as json
# This is faster as calling jq but does not handle all cases
json_quote() {
local V="${1//\"/\\\"}"
V="${V//[$'\t\r\n\v']/ }"
V=$(trim "$V")
printf '"%s"' "$V"
}
# Splits supplied string by comma in an json array
json_quote_array() {
printf "["
local VALUES
local V
local I=0
mapfile -t -d, VALUES <<< "$1"
for V in "${VALUES[@]}"
do
[ "$I" -gt 0 ] && printf ","
printf '%s' "$(json_quote "$V")"
I=$((I+1))
done
printf "]"
}
# Converts a m3u line to a json key/value pair
m3u_to_json() {
local LINE="$1"
if [ "${LINE:0:1}" = "#" ]
then
local INFO="${LINE:1}"
local KEY=${INFO%%:*}
local VALUE=${INFO#*:}
if [ "$KEY" = "LANGUAGE" ]
then
# new languages key
local VALUES
#VALUES=$(jq -c -R 'split(", ")' <<< "$VALUE")
VALUES=$(json_quote_array "$VALUE")
printf '"%s":%s,' "Languages" "$VALUES"
# old language key for backward compatibility
#VALUE=$(jq -n --arg value "$VALUE" '$value')
VALUE=$(json_quote "$VALUE")
elif [ "$KEY" = "EXTGENRE" ]
then
#VALUE=$(jq -c -R 'split(", ")' <<< "$VALUE")
VALUE=$(json_quote_array "$VALUE")
elif [ "$KEY" = "BITRATE" ]
then
#enforce bitrate value
[ -z "$VALUE" ] && VALUE="0"
elif [ "$KEY" = "ADDED" ] || [ "$KEY" = "LASTMODIFIED" ]
then
[ -z "$VALUE" ] && VALUE="-1"
else
#VALUE=$(jq -n --arg value "$VALUE" '$value')
VALUE=$(json_quote "$VALUE")
fi
printf '"%s":%s' "${m3ufields_map[$KEY]:-}" "$VALUE"
else
#VALUE=$(jq -n --arg value "$LINE" '$value')
VALUE=$(json_quote "$LINE")
printf '"%s":%s' "StreamUri" "$VALUE"
fi
}
# Parses an alternate stream m3u and outputs it as json
parse_alternative_streams() {
local S="$1"
local RADIO="$2"
local CODEC=""
local BITRATE=""
local URI=""
local NAME=""
local LINE
while read -r LINE
do
if [ "${LINE:0:1}" = "#" ]
then
INFO="${LINE:1}"
KEY=${INFO%%:*}
VALUE=${INFO#*:}
case "$KEY" in
CODEC)
#CODEC=$(jq -n --arg value "$VALUE" '$value')
CODEC=$(json_quote "$VALUE")
ALL_CODECS["$VALUE"]="1"
;;
BITRATE)
BITRATE="$VALUE"
ALL_BITRATES["$VALUE"]="1"
;;
esac
else
NAME=$(gen_m3u_name "$LINE")
#URI=$(jq -n --arg value "$LINE" '$value')
URI=$(json_quote "$LINE")
fi
done < "$S"
#print to index
printf "\"%s\":{\"StreamUri\":%s,\"Codec\":%s,\"Bitrate\":%s}" "$NAME" "$URI" "$CODEC" "$BITRATE"
#create m3u for alternative stream
head -9 "$RADIO" > "$PLS_DIR/$NAME.m3u"
cat "$S" >> "$PLS_DIR/$NAME.m3u"
rm "$S"
}
# Move a file if it has changed
move_compress_changed() {
local FILE=$1
local SRC_CHKSUM
SRC_CHKSUM=$(md5sum "${FILE}.tmp" | cut -d" " -f1)
local DST_CHKSUM
if [ -f "$FILE" ]
then
DST_CHKSUM=$(md5sum "${FILE}" | cut -d" " -f1)
else
DST_CHKSUM=""
fi
if [ "$SRC_CHKSUM" != "$DST_CHKSUM" ]
then
#file has changed
mv "$FILE.tmp" "$FILE"
gzip -9 -c "$FILE" > "$FILE".gz
return 0
else
rm "$FILE.tmp"
return 1
fi
}
# Creates the webradioDB index
create_index() {
echo "Cleaning up"
rm -fr "$PLS_DIR"
mkdir "$PLS_DIR"
rm -fr "$PICS_DIR"
mkdir "$PICS_DIR"
echo "Copy moode webradios"
normalize_fields "${MOODE_PLS_DIR}"
cp "${MOODE_PICS_DIR}"/* "${PICS_DIR}"
cp "${MOODE_PLS_DIR}"/* "${PLS_DIR}"
echo "Copy myMPD webradios"
normalize_fields "${MYMPD_PLS_DIR}"
cp "${MYMPD_PICS_DIR}"/* "${PICS_DIR}"
cp "${MYMPD_PLS_DIR}"/* "${PLS_DIR}"
echo "Creating json index"
rm -f "${INDEXFILE}.tmp"
exec 3<> "${INDEXFILE}.tmp"
printf "{" >&3
WEBRADIO_COUNT=0
local F
for F in "$PLS_DIR"/*.m3u
do
local FILENAME=${F##*/}
[ "$WEBRADIO_COUNT" -gt 0 ] && printf "," >&3
printf "\"%s\":{" "$FILENAME" >&3
local LINE_COUNT=0
declare -A ALL_CODECS=()
declare -A ALL_BITRATES=()
local LINE
while read -r LINE
do
local KEY=${LINE%%:*}
[ "$LINE" = "#EXTM3U" ] && continue
[ "$KEY" = "#EXTINF" ] && continue
[ "$LINE" = "" ] && continue
[ "$LINE_COUNT" -gt 0 ] && printf "," >&3
m3u_to_json "$LINE" >&3
local VALUE=${LINE#*:}
[ "$KEY" = "#CODEC" ] && [ "$VALUE" != "" ] && ALL_CODECS["$VALUE"]="1"
[ "$KEY" = "#BITRATE" ] && [ "$VALUE" != "" ] && ALL_BITRATES["$VALUE"]="1"
LINE_COUNT=$((LINE_COUNT+1))
done < "$F"
#alternative streams
printf ",\"alternativeStreams\":{" >&3
if [ "$(echo "$F".*)" != "$F.*" ]
then
FILE_COUNT=0
for S in "$F."*
do
[ "$FILE_COUNT" -gt 0 ] && printf "," >&3
parse_alternative_streams "$S" "$F" >&3
FILE_COUNT=$((FILE_COUNT+1))
done
fi
printf "},\"allCodecs\":[" >&3
local CODEC_COUNT=0
local C
for C in "${!ALL_CODECS[@]}"
do
[ "$CODEC_COUNT" -gt 0 ] && printf "," >&3
printf "\"%s\"" "$C" >&3
CODEC_COUNT=$((CODEC_COUNT+1))
done
printf "],\"allBitrates\":[" >&3
local BITRATE_COUNT=0
local BITRATE_HIGHEST=0
local B
for B in "${!ALL_BITRATES[@]}"
do
[ "$BITRATE_COUNT" -gt 0 ] && printf "," >&3
printf "%s" "$B" >&3
BITRATE_COUNT=$((BITRATE_COUNT+1))
[ "$B" -gt "$BITRATE_HIGHEST" ] && BITRATE_HIGHEST=$B
done
printf "],\"highestBitrate\":%s" "$BITRATE_HIGHEST" >&3
printf "}" >&3
WEBRADIO_COUNT=$((WEBRADIO_COUNT+1))
printf "."
done
printf "}" >&3
exec 3>&-
echo ""
#validate the json file
if jq < "${INDEXFILE}.tmp" > /dev/null
then
echo "${WEBRADIO_COUNT} webradios in index"
#create other index files
# languages
jq -r '.[] | .Languages[]' "${INDEXFILE}.tmp" | sort -u | \