Project

General

Profile

export channels to mediatomb ยป channel.sh

sanshiro san, 2017-09-24 20:56

 
1
#!/bin/bash
2
# you must have installed jq (apt-get install jq)
3

4
############### BEGIN CONFIG ###################
5
profile="pass" 
6
host="192.168.0.2:9981" 
7
###############  END CONFIG  ###################
8

9
wget -O channels_unsorted.json http://$host/api/channel/grid?limit=100000
10

11
#cp channels_unsorted.json  channels.json
12

13
#jq  '.entries |= sort_by(.name)' channels_unsorted.json > channels.json
14
jq  '.entries |= sort_by(.number)' channels_unsorted.json > channels.json
15

16
echo \#EXTM3U
17

18
entries=$(/usr/bin/jq -c  '.total' channels.json)
19

20
#clear sql code
21
rm channels.sql
22

23
#filter the quote character in channel name to avoid producing SQL issue
24
quote=\'
25

26
#clear all previous channel to avoid duplicate, create all channel from scratch
27
echo "delete from mt_cds_object where metadata like '%tvheadend%';" >> channels.sql
28

29
for (( service=0; service<= $entries; service++ ))
30
do
31
        enabled=$(/usr/bin/jq -c  '.entries['$service'].enabled'  channels.json)
32
        if [ "$enabled" = "true" ]
33
                then
34
                        svcname=$(/usr/bin/jq -c -r  '.entries['$service'].svcname'  channels.json)
35
                        uuid=$(/usr/bin/jq -c -r  '.entries['$service'].uuid'  channels.json)
36
                        name=$(/usr/bin/jq -c -r  '.entries['$service'].name'  channels.json)
37
						newname=$(echo $name | sed "s/$quote/ /g")
38
						number=$(/usr/bin/jq -c -r  '.entries['$service'].number'  channels.json)
39
                        desc="$number - $name" 
40
                        echo \#EXTINF:-1, $desc
41
                        #echo http://$host/stream/channel/$uuid\
42
                        #echo http://$host/stream/channel/$uuid\?profile=$profile
43
						#forge the SQL line to inject the channel into mediatomb db
44
						#we identify the channel created by this script by puttin 2tvheadend" into the description so we can delete them 
45
						#when we re-execute the script
46
						#replace the id column parent by the one in which you want the channel to appears
47
echo "insert into mt_cds_object (parent_id, object_type, upnp_class, dc_title, location, metadata, resources, update_id,mime_type,flags) values ((select id from mt_cds_object where dc_title='TNT'), 10, 'object.item.videoItem', '$newname', 'http://$host/stream/channel/$uuid', 'dc%3Adescription=tvheadend', '0~protocolInfo=http-get%3A%2A%3Avideo%2Fx-matroska%3A%2A~~',0, 'video/x-matroska',1);" >> channels.sql
48

49
        fi
50
done
51
#inject the sql code into mediatomb db
52
cat channels.sql | sqlite3 mediatomb.db
53
#rm channels_unsorted.json  channels.json
    (1-1/1)