-
Notifications
You must be signed in to change notification settings - Fork 2
/
genfav.sh
262 lines (210 loc) · 7.82 KB
/
genfav.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
#!/bin/sh
# CHANGELOG
# * Version 1.0
# - Initial version
# * Version 1.0.1
# - Correcao na geracao do favorito que contem todos os canais
# - Ignorando espaco inicial e final no nome dos favoritos e filtros
# * Version 1.1
# - O favorito que contem todos os canais agora inclui o numero do satelite a qual o canal pertence
# * Version 1.2
# - Incluido verificacao da existencia do arquivo de regras
# * Version 1.3
# - Incluido funcionalidade de recarregar os favoritos automaticamente
LAMEFILE=/etc/enigma2/lamedb
RULES=/etc/enigma2/genfav_rules.txt
OUTDIR=/etc/enigma2
TMPDIR=/tmp/genfav_tmp
LOGFILE=$TMPDIR/genfav.log
SATS=""
SERVICE=""
EXCLUDE=""
if [ ! -e $RULES ]; then
echo "!!! ARQUIVO DE REGRAS NAO ENCONTRADO EM: $RULES !!!"
exit 1
fi
if [ ! -d $TMPDIR ]; then
mkdir $TMPDIR
fi
rm -rf $TMPDIR/*
if [ ! -d $OUTDIR ]; then
mkdir $OUTDIR
fi
rm -rf $OUTDIR/userbouquet.*
rm -rf $OUTDIR/bouquets.*
log()
{
if [ "$2" = "2" ]; then
touch $LOGFILE
printf "$1 \r\n"
printf "$1 \r\n" >> $LOGFILE
else
touch $LOGFILE
echo "$1"
echo "$1" >> $LOGFILE
fi
}
convertdb ()
{
log "### Convertendo a base de dados de canais"
LINENUMBERFIRSTEND=$(grep -n '^end$' $LAMEFILE | cut -d: -f1 | head -n 1)
log " -> Separando lista de TPs"
head -n $LINENUMBERFIRSTEND $LAMEFILE > $TMPDIR/transponder.txt
tail -n $(($(wc -l $LAMEFILE | cut -d' ' -f1)-$(wc -l $TMPDIR/transponder.txt | cut -d' ' -f1))) $LAMEFILE > $TMPDIR/services.txt
log " -> Separando lista de canais"
grep -E '^.{8}:.{4}:.{4}$' $TMPDIR/transponder.txt | cut -d: -f1 | sort | uniq > $TMPDIR/transponder_code.txt
touch $TMPDIR/tp-sat.txt
log " -> Classificando TPs por satelite"
for CODE in $(cat $TMPDIR/transponder_code.txt); do
SAT=$(grep -A 1 $CODE $TMPDIR/transponder.txt | grep -Ev '^.{8}:.{4}:.{4}$' | cut -d: -f5 | grep -v ^--$ | sort | uniq | cut -c2-3)
echo "$CODE:$SAT" >> $TMPDIR/tp-sat.txt
done
log "### Classificando canais por satelite"
for SAT in $(cut -d: -f2 $TMPDIR/tp-sat.txt | sort | uniq); do
echo " * Satelite $SAT"
SATS="$SATS $SAT"
touch $TMPDIR/sat-$SAT.txt
for CODESAT in $(grep ":$SAT" $TMPDIR/tp-sat.txt | cut -d: -f1); do
grep -A 1 $CODESAT $TMPDIR/services.txt | tr '\n' ':' | sed 's/:--:/\n/g' | sed 's/.:$/\n/g' | grep -v '^$' >> $TMPDIR/sat-$SAT.txt
done
done
}
checkexclude ()
{
if cat "$RULES" | grep -i "^exclude=" &>/dev/null; then
EXCLUDE=$(cat "$RULES" | grep -i "^exclude=" | cut -d= -f2 | sed 's/,/|:/g')
fi
}
mkservice ()
{
CHANNEL="$1"
NAME="$2"
CHANNELCODE1=$(echo $CHANNEL | cut -d: -f1 | sed 's/^0*//g' | tr 'a-z' 'A-Z')
CHANNELTP=$(echo $CHANNEL | cut -d: -f2 | sed 's/^0*//g' | tr 'a-z' 'A-Z')
CHANNELCODE2=$(echo $CHANNEL | cut -d: -f3 | sed 's/^0*//g' | tr 'a-z' 'A-Z')
if [ "$CHANNELCODE2" == "" ]; then
CHANNELCODE2=0
fi
CHANNELTYPE=$(echo $CHANNEL | cut -d: -f5 | sed 's/^0*//g' | tr 'a-z' 'A-Z')
if [ "$CHANNELTYPE" == "25" ]; then
CHANNELTYPE=19
fi
if [ "$NAME" == "" ]; then
SERVICE="#SERVICE 1:0:$CHANNELTYPE:$CHANNELCODE1:$CHANNELCODE2:1:$CHANNELTP:0:0:0:\n"
else
SERVICE="#SERVICE 1:0:$CHANNELTYPE:$CHANNELCODE1:$CHANNELCODE2:1:$CHANNELTP:0:0:0::$NAME\n#DESCRIPTION $NAME\n"
fi
}
genfavall ()
{
SAT="$1"
CHANNELTYPE="$2"
log "### Gerando favoritos gerais"
if [ ! -e $OUTDIR/userbouquet.favourites.tv ]; then
echo "#NAME Favourites (TV)" > $OUTDIR/userbouquet.favourites.tv
fi
if [ ! -e $OUTDIR/userbouquet.favourites.radio ]; then
echo "#NAME Favourites (Radio)" > $OUTDIR/userbouquet.favourites.radio
fi
if [ "$EXCLUDE" == "" ]; then
cat "$TMPDIR/sat-$SAT.txt" | sort -t: -k7 | grep -E '.*:.*:.*:.*:.*:.*:' > $TMPDIR/genfavall-$SAT.txt
else
cat "$TMPDIR/sat-$SAT.txt" | sort -t: -k7 | grep -E '.*:.*:.*:.*:.*:.*:' | grep -Eiv "$EXCLUDE" > $TMPDIR/genfavall-$SAT.txt
fi
while read REG; do
CHANNEL="$(echo "$REG" | cut -d: -f1-6)"
NAME="$(echo "$REG" | cut -d: -f7)"
mkservice "$CHANNEL" "$NAME ($SAT)"
if [ "$CHANNELTYPE" == "1" ]; then
printf "$SERVICE" >> $OUTDIR/userbouquet.favourites.tv
elif [ "$CHANNELTYPE" == "2" ]; then
printf "$SERVICE" >> $OUTDIR/userbouquet.favourites.radio
else
printf "$SERVICE" >> $OUTDIR/userbouquet.favourites.tv
fi
done < "$TMPDIR/genfavall-$SAT.txt"
log " -> Gerado favorito com $(wc -l $OUTDIR/userbouquet.favourites.tv | cut -d' ' -f1) canais de TV"
log " -> Gerado favorito com $(wc -l $OUTDIR/userbouquet.favourites.radio | cut -d' ' -f1) canais de Radio"
SERVICE=""
}
genfav ()
{
SAT=$1
FAV=$2
log " -> Gerando favorito com $(wc -l $TMPDIR/parserule-$SAT-$(echo $FAV | tr ' ' '_')-channel.txt | cut -d' ' -f1) canais"
echo "#NAME $FAV ($SAT)" > $OUTDIR/userbouquet.$SAT.$(echo $FAV | tr ' ' '_' | tr 'A-Z' 'a-z').tv
for CHANNEL in $(cat $TMPDIR/parserule-$SAT-$(echo $FAV | tr ' ' '_')-channel.txt | cut -d: -f1-6); do
mkservice "$CHANNEL"
printf "$SERVICE" >> $OUTDIR/userbouquet.$SAT.$(echo $FAV | tr ' ' '_' | tr 'A-Z' 'a-z').tv
done
SERVICE=""
}
parserule ()
{
SAT="$1"
FAV="$2"
FILERULECHANNEL="$3"
if [ "$FAV" != "exclude" ]; then
log " -> Aplicando $(wc -l $FILERULECHANNEL | cut -d' ' -f1) filtros"
FILTERIN=""
FILTEROUT=""
while read RULECHANNEL; do
if ( echo $RULECHANNEL | grep '^!' &>/dev/null); then
FILTEROUT="$FILTEROUT|:$(echo $RULECHANNEL | sed 's/!//g')"
else
FILTERIN="$FILTERIN|:$(echo $RULECHANNEL)"
fi
done < "$FILERULECHANNEL"
touch $TMPDIR/parserule-$SAT-$(echo $FAV | tr ' ' '_')-channel.txt
if [ "$EXCLUDE" == "" ]; then
if [ "$FILTEROUT" == "" ]; then
cat "$TMPDIR/sat-$SAT.txt" | grep -Ei "${FILTERIN##|}" | sort -t: -k7 >> $TMPDIR/parserule-$SAT-$(echo $FAV | tr ' ' '_')-channel.txt
else
cat "$TMPDIR/sat-$SAT.txt" | grep -Ei "${FILTERIN##|}" | grep -Eiv "${FILTEROUT##|}" | sort -t: -k7 >> $TMPDIR/parserule-$SAT-$(echo $FAV | tr ' ' '_')-channel.txt
fi
else
if [ "$FILTEROUT" == "" ]; then
cat "$TMPDIR/sat-$SAT.txt" | grep -Ei "${FILTERIN##|}" | grep -Eiv "$EXCLUDE" | sort -t: -k7 >> $TMPDIR/parserule-$SAT-$(echo $FAV | tr ' ' '_')-channel.txt
else
cat "$TMPDIR/sat-$SAT.txt" | grep -Ei "${FILTERIN##|}" | grep -Eiv "${FILTEROUT##|}" | grep -Eiv "$EXCLUDE" | sort -t: -k7 >> $TMPDIR/parserule-$SAT-$(echo $FAV | tr ' ' '_')-channel.txt
fi
fi
fi
}
rules () {
SAT=$1
cat $RULES | grep -v "^#" | grep -v "^$" > $TMPDIR/rulespt.txt
while read RULELINE; do
FAV=$(echo $(echo "$RULELINE" | cut -d'=' -f1))
CHANNELS=$(echo "$RULELINE" | cut -d'=' -f2)
if [ "$FAV" != "exclude" ]; then
log "### Lendo regra do favorito: $FAV"
echo "$CHANNELS" | tr ',' '\n' >> $TMPDIR/rules-$SAT-$(echo $FAV | tr ' ' '_').txt
parserule "$SAT" "$FAV" "$TMPDIR/rules-$SAT-$(echo $FAV | tr ' ' '_').txt"
genfav "$SAT" "$FAV"
echo "#SERVICE 1:7:1:0:0:0:0:0:0:0:FROM BOUQUET \"userbouquet.$SAT.$(echo $FAV | tr ' ' '_' | tr 'A-Z' 'a-z').tv\" ORDER BY bouquet" >> $OUTDIR/bouquets.tv
fi
done < "$TMPDIR/rulespt.txt"
}
log "### Iniciado em $(date)"
log "----------------------------------------"
convertdb
log "----------------------------------------"
log "### Check exclude rule"
checkexclude
log "----------------------------------------"
echo "#NAME User - bouquets (TV)" > $OUTDIR/bouquets.radio
echo "#SERVICE 1:7:2:0:0:0:0:0:0:0:FROM BOUQUET \"userbouquet.favourites.radio" ORDER BY bouquet\" >> $OUTDIR/bouquets.radio
echo "#NAME User - bouquets (TV)" > $OUTDIR/bouquets.tv
echo "#SERVICE 1:7:1:0:0:0:0:0:0:0:FROM BOUQUET \"userbouquet.favourites.tv\" ORDER BY bouquet" >> $OUTDIR/bouquets.tv
for SAT in $SATS; do
log ""
log "------------- Satellite $SAT -------------"
genfavall $SAT
rules $SAT
log "----------------------------------------"
done
log "### Recarregando os favoritos"
wget -qO - http://127.0.0.1/web/servicelistreload?mode=1 &>/dev/null
wget -qO - http://127.0.0.1/web/servicelistreload?mode=2 &>/dev/null
log "### Finalizado em $(date)"