Project

General

Profile

Picons support (was: Helper script: matchIcons.sh...) » matchIcons.sh

version 20140227 - Dietmar Konermann, 2014-02-27 12:07

 
1
#!/bin/bash
2
# matchIcons.sh, [email protected], 20140227
3
#
4
# Takes a Tvheadend configuration and a directory containing channel icons, scans
5
# the channels in the config and tries to match them to the icon file names, creates
6
# symlinks in the icon directory for non-exact matches, writes file:// URLs to the
7
# configuration. 
8
# The orignal channel configuration is NOT touched by default. Instead we work on 
9
# a copy that needs to be moved into place manually before restarting Tvheadend.
10
#
11
# Tested only on recent Tvheadend 3.9.x versions (dvb rewrite)!
12
# Feedback welcome!
13
#
14

    
15
# config section
16

    
17
htsconf="/home/hts/.hts/tvheadend"
18
newchannel="$htsconf/channel.new$$"
19
icondir="/home/hts/channelicons"
20

    
21
[ "$1" = "-c" ] && clear=1
22

    
23
# function secion
24

    
25
function simplify {
26
	simple=$(echo "$1" | sed -e 's/[^a-zA-Z0-9]//g' -e 's/\(.*\)/\L\1/' -e 's/.*\///')
27

    
28
	echo "$simple"
29
}
30

    
31
function exist {
32
	[ -f "$1" -o -h "$1" ]
33
}
34

    
35

    
36
function name2icon {
37
	typeset icon=""
38
	while [ -n "$1" -a -z "$icon" ]; do
39
	        simplename=$(simplify "$1")
40
		icon=$(echo "$iconlist" | awk -F'\t' -vname="$simplename" '{if ($2==name) print $1;}' | head -1)
41
		shift
42
	done
43
	echo "$icon"
44
}
45

    
46
function service2name {
47
	grep '"svcname"' "$htsconf/input/linuxdvb/networks/"*"/muxes/"*"/services/$1" |
48
	awk -F\" '{print $4}' |
49
	sed -e 's/[ /:\\<>|*?'"'"'"][ /:\\<>|*?'"'"'"]*/-/g'
50
	#sed -e 's/\//-/g'/-/g'
51
}
52

    
53
function patchicon {
54
	channelfile="$1"
55
	iconfile="$2"
56

    
57
	exist "$channelfile" || return
58

    
59
	if [ -n "$iconfile" ]; then
60
		exist "$iconfile" || return
61
		url=$(echo "file://$iconfile" | sed -e 's/"/\\"/g')
62
	else
63
		url=""
64
	fi
65
	
66
	savetxt=$(grep -v -Ee '{|}|"icon"' "$channelfile")
67
	echo '{' >"$channelfile"
68
	[ -n "$url" ] && echo "        \"icon\": \"$url\"," >>"$channelfile"
69
	echo "$savetxt" >>"$channelfile"
70
	echo '}' >>"$channelfile"
71
}
72

    
73
############## MAIN
74

    
75
[ -d "$newchannel" ] && echo "$newcchannel exists!" && exit 1
76

    
77
echo "Copying current channel dir to $newchannel..."
78
cp -rp "$htsconf/channel" "$newchannel" || exit 1
79

    
80
echo "Scanning $icondir..."
81
iconlist=$(
82
	ls -1 "$icondir" | 
83
	while read line; do
84
		if [[ "$icondir/$line" == *.png ]] && exist "$icondir/$line"; then
85
			printf "%s\t%s\n" "$line" $(simplify "${line%.png}") 
86
			printf . >&2
87
		fi
88
	done
89
	echo >&2
90
)
91
#echo "$iconlist" && exit 0
92

    
93
ls -1 "$newchannel/"* |
94
while read channelfile; do
95
	exist "$channelfile" || continue
96

    
97
	if [ "$clear" = "1" ]; then
98
		patchicon "$channelfile" ""
99
		continue
100
	fi
101

    
102
	name=$(service2name $(grep -A 1 '"services"' "$channelfile" | tail -1 | awk -F\" '{print $2}'))
103
	#icon=$(name2icon "${name% [hH][dD]}" "${name/[hH][dD]//}" "$name")
104
	icon=$(name2icon "$name" "${name/[hH][dD]//}")
105

    
106
	echo "Processing '$name'..."
107
	if exist "$icondir/$icon"; then
108
		if ! exist "$icondir/$name.png"; then
109
			echo "  Creating symlink $icondir/$name.png -> $icondir/$icon"
110
			ln -sf "$icondir/$icon" "$icondir/$name.png"
111
		fi
112
		echo "  Setting URL 'file://$icondir/$name.png'..."
113
		patchicon "$channelfile" "$icondir/$name.png"
114
	else
115
		echo "  No icon found for '$name'."
116
	fi
117
done
118

    
119
echo "
120
Finished creating $newchannel.
121
Next steps: move in place and restart tvheadend.
122
Enjoy!
123
"
(2-2/2)