forked from h0me5k1n/iptv-strmmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iptv-strmmaker.sh
executable file
·359 lines (320 loc) · 11 KB
/
iptv-strmmaker.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
#!/bin/bash
SCRIPTNAME="$(basename $0)"
SCRIPTDIR="$(cd $(dirname $0) && pwd)"
vLOG="$SCRIPTDIR/${SCRIPTNAME%.*}.log"
vFILEPROCESSEDLOG="$SCRIPTDIR/${SCRIPTNAME%.*}-FILESPROCESSED.log"
vFILEEXISTINGLOG="$SCRIPTDIR/${SCRIPTNAME%.*}-FILESEXISTING.log"
vFILESTODELETE="$SCRIPTDIR/${SCRIPTNAME%.*}-FILESTODELETE.log"
vNewTV="$SCRIPTDIR/${SCRIPTNAME%.*}-NewTV.log"
vNewMOVIES="$SCRIPTDIR/${SCRIPTNAME%.*}-NewMOVIES.log"
vFILESTODELETE="$SCRIPTDIR/${SCRIPTNAME%.*}-FILESTODELETE.log"
LOCKFILE="$SCRIPTDIR/${SCRIPTNAME%.*}.lock"
#source "$SCRIPTDIR/vodselection-tv"
#source "$SCRIPTDIR/vodselection-movies"
# Log function
PrintLog(){
echo "[`date`] - ${*}" >> "${vLOG}"
}
# counter function
AddCounter()(
TEMPFILE=$1.tmp
COUNTER=$[$(cat $TEMPFILE) + 1]
echo $COUNTER > $TEMPFILE
)
# Set variables using parameters - m3u url first then output location for strm file folders
if [ -n "$1" ]; then
vURL="$1"
if [ -n "$2" ]; then
OUTPUTDIR="$2"
OUTPUTDIR=${OUTPUTDIR%/}
if [ ! -d "$OUTPUTDIR" ]; then
PrintLog "ERROR: output path $OUTPUTDIR does not exist"
exit 1
fi
else
echo
PrintLog "ERROR: need an output path - nothing defined"
exit 1
fi
else
echo
PrintLog "ERROR: need a url to an m3u as input - nothing defined"
exit 1
fi
# This changes each playlist entry to appear on a single line instead of 2 lines
to_one_line_per_record() {
local inf_line= line=
while read -r line; do
if [[ $line = "#"* ]]; then
inf_line=$line
elif [[ $line = "" ]]; then
echo blank line
else
printf '%s\n' "${inf_line},$line"
fi
done
}
# This changes each line in a "single lined" list of m3u entries back to 2 lines
# doesn't work for vod!! too many commas
from_one_line_per_record() {
local inf_f1 inf_f2 url
while IFS=, read -r inf_f1 inf_f2 url; do
printf '%s,%s\n%s\n' "$inf_f1" "$inf_f2" "$url"
done
}
ScanEntries_TV() {
while read -r catLine; do
## DO NOT ECHO ENTRIES IN THIS FUNCTION ##
# PrintLog "catLine is $catLine (output line)"
# set newLine as default
newLine="$catLine"
# parse url
vMediaUrl=http${newLine##*,http}
# parse existing channel name - from the end of the line before the url
vChannelName="${newLine%%,http*}"
vChannelName="${vChannelName##*,}"
#clear unwanted prefix strings in title
vChannelName="$(echo $vChannelName | sed "s/|FR| //g")"
vChannelName="$(echo $vChannelName)" # remove extra spaces?
# parse season and episode
# vSeasonAndEpisode="${vChannelName:(-7)}" error if 99+ episodes
vSeasonAndEpisode="$(echo $vChannelName | grep -Po 'S[[:digit:]]{2,3}[[:space:]]E[[:digit:]]{2,3}')"
# parse season
vSeason="${vSeasonAndEpisode% *}"
vSeason="${vSeason#*S}"
vSeason="$(expr $vSeason + 0)"
printf -v vSeason "%02d" $vSeason
# parse episode
vEpisode="${vSeasonAndEpisode#* }"
vEpisode="${vEpisode#*E}"
vEpisode="$(expr $vEpisode + 0)"
printf -v vEpisode "%02d" $vEpisode
# parse tv series
vSeries="${vChannelName% $vSeasonAndEpisode}"
# vSeries=$(echo $vSeries | sed -e 's/[^A-Za-z0-9._- ]//g')
# write output
# PrintLog "FOUND - $vSeries from season $vSeason and episode $vEpisode. URL $vMediaUrl"
WriteSTRMFile_TV
done
}
WriteSTRMFile_TV(){
#remove special caracters
# vSeries="$(echo $vSeries | sed 's/[^a-zA-Z0-9\p{Arabic} ()_-]//g')"
vSeries="$(echo $vSeries | sed 's/[:.\,\\\/|?]//g')"
mkdir -p "$OUTPUTDIR/STRM TV/$vSeries/Season $vSeason"
vSTRMFile="$OUTPUTDIR/STRM TV/"
vSTRMFile+="$vSeries/Season $vSeason/$vSeries "
vSTRMFile+="S${vSeason}"
vSTRMFile+="E${vEpisode}"
vTVEpisode="${vSTRMFile##*/}"
vSTRMFile+=".strm"
# check if file contents are different - only write if different
if [ -f "$vSTRMFile" ]; then
content=$(cat "$vSTRMFile")
if [ "$vMediaUrl" != "$content" ];then
echo "$vMediaUrl" > "$vSTRMFile"
AddCounter countTVCurrent
PrintLog "WROTE UPDATED LINK - $vSTRMFile"
echo "$vSTRMFile" >> "${vFILEPROCESSEDLOG}"
else
AddCounter countTVCurrent
PrintLog "ALREADY EXISTS - $vSTRMFile"
echo "$vSTRMFile" >> "${vFILEPROCESSEDLOG}"
fi
else
echo "$vMediaUrl" > "$vSTRMFile"
AddCounter countTVNew
PrintLog "WROTE NEW - $vSTRMFile"
echo "> $vTVEpisode" >> "${vNewTV}"
echo "$vSTRMFile" >> "${vFILEPROCESSEDLOG}"
fi
}
ScanEntries_Movies() {
while read -r catLine; do
## DO NOT ECHO ENTRIES IN THIS FUNCTION ##
# PrintLog "catLine is $catLine (output line)"
# set newLine as default
newLine="$catLine"
# parse url
vMediaUrl=http${newLine##*,http}
# parse existing channel name - from the end of the line before the url
vMovieName="${newLine%%,http*}"
vMovieName="${vMovieName##*,}"
#clear unwanted prefix strings in title
vMovieName="$(echo $vMovieName | sed "s/|FR| //g")"
vMovieName="$(echo $vMovieName)" # remove extra spaces?
# vMovieName=$(echo $vMovieName | sed -e 's/[^A-Za-z0-9._- ]//g')
# write output
# PrintLog "FOUND - $vMovieName. URL $vMediaUrl"
WriteSTRMFile_Movies
done
}
WriteSTRMFile_Movies(){
#remove special caracters
# vMovieName="$(echo $vMovieName | sed 's/[^a-zA-Z0-9\p{Arabic} ()_-]//g')"
vMovieName="$(echo $vMovieName | sed 's/[:.\,\\\/|?]//g')"
mkdir -p "$OUTPUTDIR/STRM Movies/$vMovieName"
vSTRMFile="$OUTPUTDIR/"
vSTRMFile+="STRM Movies/$vMovieName/"
vSTRMFile+="$vMovieName.strm"
# check if file contents are different - only write if different
if [ -f "$vSTRMFile" ]; then
content=$(cat "$vSTRMFile")
if [ "$vMediaUrl" != "$content" ];then
echo "$vMediaUrl" > "$vSTRMFile"
AddCounter countMovieCurrent
PrintLog "WROTE UPDATED LINK - $vSTRMFile"
echo "$vSTRMFile" >> "${vFILEPROCESSEDLOG}"
else
AddCounter countMovieCurrent
PrintLog "ALREADY EXISTS - $vSTRMFile"
echo "$vSTRMFile" >> "${vFILEPROCESSEDLOG}"
fi
else
echo "$vMediaUrl" > "$vSTRMFile"
AddCounter countMovieNew
PrintLog "WROTE NEW - $vSTRMFile"
echo "> $vMovieName" >> "${vNewMOVIES}"
echo "$vSTRMFile" >> "${vFILEPROCESSEDLOG}"
fi
}
# VARIABLES
vTEMPFILE=temp.m3u
# PROCESSING
echo "[`date`] - Script started"
cd $SCRIPTDIR
[ -f "${vLOG}" ] && rm "${vLOG}"
[ -f "${vFILEPROCESSEDLOG}" ] && rm "${vFILEPROCESSEDLOG}"
[ -f "${vFILEEXISTINGLOG}" ] && rm "${vFILEEXISTINGLOG}"
[ -f "${vFILESTODELETE}" ] && rm "${vFILESTODELETE}"
[ -f "*.m3u" ] && rm "*.m3u"
[ -f "*.tmp" ] && rm "*.tmp"
echo 0 > countMovieCurrent.tmp
echo 0 > countMovieNew.tmp
echo 0 > countTVCurrent.tmp
echo 0 > countTVNew.tmp
echo 0 > countDeleted.tmp
if [ ! -f vodselection-tv ]; then
PrintLog "vodselection-tv file does not exist. Choose TV series to be included by updating the sample file."
exit 1
fi
if [ ! -f vodselection-movies ]; then
PrintLog "vodselection-movies file does not exist. Choose Movies to be included by updating the sample file."
exit 1
fi
wget --quiet -O "$vTEMPFILE" "$vURL" -nv -T 10 -t 1
if [ $? -ne 0 ]; then
PrintLog "wget reported an error"
rm "$vTEMPFILE"
exit 1
else
FILESIZE=$(stat -c%s "$vTEMPFILE")
PrintLog "wget completed... $vTEMPFILE downloaded ($FILESIZE)"
fi
# fix line feeds
sed -i 's/^M$//' "$vTEMPFILE"
sed -i 's/\r$//' "$vTEMPFILE"
# fix dollar signs! replace with "s"
sed -i 's/\$/s/g' "$vTEMPFILE"
# remove blank lines
PrintLog "Removing blank lines from $vTEMPFILE"
cat "$vTEMPFILE" | sed '/^$/d' > 1_remblank.tmp
if [ $? -ne 0 ]; then
echo "ERROR REPORTED"
exit 1
fi
# remove commas from between quotes - it breaks splitting to one line and rebuilding back together at the end
PrintLog "Removing commas from between quotes in file"
cat 1_remblank.tmp | awk -F'"' -v OFS='"' '{for(i=2;i<NF;i+=2) gsub(",", "", $i)}1' > 2_quotefix.tmp
if [ $? -ne 0 ]; then
echo "ERROR REPORTED"
exit 1
fi
# one line per record
PrintLog "Rebuilding M3U file as single lined entries"
cat 2_quotefix.tmp | to_one_line_per_record > 3_1line.tmp
if [ $? -ne 0 ]; then
echo "ERROR REPORTED"
exit 1
fi
# sort records based on channel name
PrintLog "Sorting entries based on channel name"
cat 3_1line.tmp | sort -t, -k2,2 > 4_1linesorted.tmp
if [ $? -ne 0 ]; then
echo "ERROR REPORTED"
exit 1
fi
# extract only entries that end with mkv/mp4
PrintLog "Extracting VOD entries based on file extension"
cat 4_1linesorted.tmp | grep -E "\.avi$|\.mp4$|\.m4v$|\.mkv$" > 5_vodentries.tmp
if [ $? -ne 0 ]; then
echo "ERROR REPORTED"
exit 1
fi
# extract tv series vod entries
PrintLog "Extracting VOD TV series entries"
cat 5_vodentries.tmp | grep -iE "^.*/series/.*$" > 6_vodentries_tv.tmp
if [ $? -ne 0 ]; then
echo "ERROR REPORTED"
exit 1
fi
# extract movies vod entries
PrintLog "Extracting VOD movie entries"
cat 5_vodentries.tmp | grep -iE "^.*/(movie|movies)/.*$" > 6_vodentries_movies.tmp
if [ $? -ne 0 ]; then
echo "ERROR REPORTED"
exit 1
fi
# write strm files for tv series vod entries - with "S## E##" entry
input=vodselection-tv
cat "$input" | while read -r line
do
# PrintLog "Processing $line from $input"
cat 6_vodentries_tv.tmp | grep -E '^.*S[[:digit:]]{2,3}[[:space:]]*E[[:digit:]]{2,3}.*$' | grep -iE "group-title=\"$line\"" | ScanEntries_TV
# added the below line to process tv series that don't use group-title. Duplicate entries found should create/edit the same output file
cat 6_vodentries_tv.tmp | grep -E '^.*S[[:digit:]]{2,3}[[:space:]]*E[[:digit:]]{2,3}.*$' | grep -iE "tvg-name=\"$line\"" | ScanEntries_TV
done
# write strm files for movies vod entries
input=vodselection-movies
cat "$input" | while read -r line
do
# PrintLog "Processing $line from $input"
cat 6_vodentries_movies.tmp | grep -iE "tvg-name=\"$line\"" | ScanEntries_Movies
cat 6_vodentries_movies.tmp | grep -iE "group-title=\"$line\"" | ScanEntries_Movies
done
# purging strm files not processed during this script run
#find "$OUTPUTDIR/STRM"* -name "*.strm" -mmin +60 -exec rm "{}" \;
find "$OUTPUTDIR/STRM"* -name "*.strm" -exec echo "{}" >> "${vFILEEXISTINGLOG}" \;
grep -Fxv -f "${vFILEPROCESSEDLOG}" "${vFILEEXISTINGLOG}" > "${vFILESTODELETE}"
# if there are files to delete - remove them
if [ -f "${vFILESTODELETE}" ]; then
while read -r filename; do
rm "$filename"
PrintLog "DELETED $filename"
AddCounter countDeleted
done <${vFILESTODELETE}
fi
# remove empty directories
find "$OUTPUTDIR/STRM"* -type d -exec rmdir "{}" + 2>/dev/null
echo $(cat countMovieCurrent.tmp) current movies
echo $(cat countMovieNew.tmp) new movies
[ -f "${vNewMOVIES}" ] && cat "${vNewMOVIES}"
echo $(cat countTVCurrent.tmp) current tv episodes
echo $(cat countTVNew.tmp) new tv episodes
[ -f "${vNewTV}" ] && cat "${vNewTV}"
echo $(cat countDeleted.tmp) files purged after removal from the playlist
echo $(cat countMovieCurrent.tmp) current movies >> "${vLOG}"
echo $(cat countMovieNew.tmp) new movies >> "${vLOG}"
[ -f "${vNewMOVIES}" ] && cat "${vNewMOVIES}" >> "${vLOG}"
echo $(cat countTVCurrent.tmp) current tv episodes >> "${vLOG}"
echo $(cat countTVNew.tmp) new tv episodes >> "${vLOG}"
[ -f "${vNewTV}" ] && cat "${vNewTV}" >> "${vLOG}"
echo $(cat countDeleted.tmp) files purged after removal from the playlist >> "${vLOG}"
[ -f "${vFILEPROCESSEDLOG}" ] && rm "${vFILEPROCESSEDLOG}"
[ -f "${vFILEEXISTINGLOG}" ] && rm "${vFILEEXISTINGLOG}"
[ -f "${vFILESTODELETE}" ] && rm "${vFILESTODELETE}"
[ -f "${vNewTV}" ] && rm "${vNewTV}"
[ -f "${vNewMOVIES}" ] && rm "${vNewMOVIES}"
rm *.m3u
rm *.tmp
echo "[`date`] - Script completed"