#!/bin/sh
# first sh box parser for titannit mfg obi

SRC=$1
INPUT=$2
PAGE=$3
NEXT=$4
PAGE2=$5
PARSER=`echo $SRC | tr '/' '\n' | tail -n1 | sed 's/.sh//'`

URLMAIN=https://s.to
#URL=http://190.115.18.20
URL=http://186.2.175.5

PARSER=`echo $SRC | tr '/' '\n' | tail -n1 | sed 's/.sh//'`
NAME=SerienStream

case $2 in
	init)	;;
	*) 	. /tmp/localhoster/hoster.sh
	   	mkdir $TMP > /dev/null 2>&1
		FILENAME="$PARSER $INPUT $PAGE $NEXT $PAGE2"
	   	FILENAME=$(echo $FILENAME | tr '&' '.' | tr '/' '.' | tr '?' '.' | tr '=' '.' | sed -e 's/\&\+/./g' -e 's#\/\+#.#g' -e 's/\?\+/./g' -e 's/;\+/./g' -e 's/=\+/./g' -e 's/-\+/./g' -e 's/[()]/./g' -e 's/://g' -e 's/\《//g' -e 's/\》//g' -e 's/➾//g' -e 's/ \+/./g' -e 's/\.\+/./g')

		if [ -z "$FILENAME" ]; then FILENAME=none;fi
		PICNAME="$FILENAME"

		if [ -e /etc/.oebuild ];then
			unset PYTHONHOME
			BIN=python3
		fi
		;;
esac

if [ `echo $SRC | grep ^"/mnt/parser" | wc -l` -gt 0 ];then
	TYPE="$SRC - Shell script"
elif [ `echo $SRC | grep ^"/var/swap" | wc -l` -gt 0 ];then
	TYPE="Swap - Shell script"
elif [ `echo $SRC | grep ^"/mnt/swapextensions" | wc -l` -gt 0 ];then
	TYPE="Mnt - Shell script"
else
	TYPE="Flash - Shell script"
fi

init()
{
	rm -rf $TMP > /dev/null 2>&1
	echo "$NAME ($TYPE)#$SRC $SRC mainmenu#http://openaaf.dyndns.tv/mediathek/menu/$PARSER.jpg#$PARSER.jpg#TiThek#0"
}

mainmenu()
{
	echo "Neue Episoden#$SRC $SRC latest#http://openaaf.dyndns.tv/mediathek/menu/new.jpg#new.jpg#$NAME#0" >$TMP/$FILENAME.list
	echo "Neue Serien#$SRC $SRC new#http://openaaf.dyndns.tv/mediathek/menu/all-newfirst.jpg#all-newfirst.jpg#$NAME#0" >>$TMP/$FILENAME.list
	echo "Populär#$SRC $SRC popular 'beliebte-serien'#http://openaaf.dyndns.tv/mediathek/menu/popularity.rank.tv.jpg#popularity.rank.tv.jpg#$NAME#0" >>$TMP/$FILENAME.list
	echo "Genre#$SRC $SRC genre 'suche?tab=genres'#http://openaaf.dyndns.tv/mediathek/menu/genre.tv.jpg#genre.jpg#$NAME#0" >>$TMP/$FILENAME.list
	echo "Alle#$SRC $SRC all 'serien'#http://openaaf.dyndns.tv/mediathek/menu/all-sorted.jpg#all-sorted.jpg#$NAME#0" >>$TMP/$FILENAME.list
	echo "A-Z#$SRC $SRC sorted 'katalog'#http://openaaf.dyndns.tv/mediathek/menu/a-z.jpg#a-z.jpg#$NAME#0" >>$TMP/$FILENAME.list
	echo "Suche#$SRC $SRC search 'suche?term=%search%&page=' 1#http://openaaf.dyndns.tv/mediathek/menu/search.jpg#search.jpg#$NAME#112" >>$TMP/$FILENAME.list
	echo "$TMP/$FILENAME.list"
}

genre()
{
	if [ ! -e "$TMP/$FILENAME.list" ]; then
		$curlbin $URL/$PAGE -o $TMP/cache.$PARSER.$INPUT.$NEXT.$FILENAME.1
		cat $TMP/cache.$PARSER.$INPUT.$NEXT.$FILENAME.1 | tr -d '\n' | sed 's/<li class=/\n<li class=/g' | grep ^"<li class=" | grep /genre/ >$TMP/cache.$PARSER.$INPUT.$NEXT.$FILENAME.2
		
		while read -u 3 ROUND; do
			TITLE=`echo $ROUND | cut -d'>' -f3 | cut -d'<' -f1 | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'`
			NEWPAGE=`echo $ROUND | sed -nr 's/.*href="([^"]+)".*/\1/p'`
            filename=`echo $NEWPAGE | sed 's!/genre/!!g'`
			if [ ! -z "$TITLE" ] && [ ! -z "$NEWPAGE" ];then
    			echo "$TITLE#$SRC $SRC listgenre '$NEWPAGE?page=' 1#http://openaaf.dyndns.tv/mediathek/menu/$filename.jpg#$filename.jpg#$NAME#0" >> $TMP/$FILENAME.list
			fi
	
		done 3<$TMP/cache.$PARSER.$INPUT.$NEXT.$FILENAME.2
    fi
  	echo "$TMP/$FILENAME.list"
}

sorted()
{
	if [ ! -e "$TMP/$FILENAME.list" ]; then
		watchlist="0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"

		rm $TMP/$FILENAME.list > /dev/null 2>&1

		for ROUND in $watchlist; do
			TITLE=`echo $ROUND`
			filename=`echo $TITLE | tr [A-Z] [a-z]`
			echo "$TITLE#$SRC $SRC listsorted '$PAGE/$ROUND?page=' 1#http://openaaf.dyndns.tv/mediathek/menu/$filename.jpg#$filename.jpg#$NAME#0" >> $TMP/$FILENAME.list
		done
	fi
  	echo "$TMP/$FILENAME.list"
}

all()
{
	if [ ! -e "$TMP/$FILENAME.list" ]; then
		$curlbin -o - $URL/$PAGE | sed 's/<li class=/\n<li class=/g' | grep "/serie/" | sort -u | awk -v URLMAIN=$URLMAIN -v SRC=$SRC -v NAME=$NAME -v PICNAME=$PICNAME -v INPUT=$INPUT -v URL=$URL -v PAGE=$PAGE -v NEXT=$NEXT -v BIN=$BIN \
		'
			BEGIN \
				{
					suche = 1
				}
				/href/ \
				{
					i = index($0, "href=\"") + 6
		            j = index(substr($0, i), "\"") - 1
		            newpage = substr($0, i, j)

					i = index($0, "\">") + 2
		            j = index(substr($0, i), "<") - 1
		            title = substr($0, i, j)

					gsub(/&amp;+/, "\\&", title)
					gsub(/&amp;+/, "\\&", title)
					gsub(/&quot;+/, "\x22", title)
					gsub(/&quot;+/, "\x22", title)
					gsub(/&#039;+/, "\x27", title)
					gsub(/&#039;+/, "\x27", title)
					gsub(/\x23/, "\"hashtag\"", title)

					if (title != "")
					{
						if ( pic == "" )
						{
			      			pic = "http://openaaf.dyndns.tv/mediathek/menu/default.jpg"
						}

                        desc = "curl --connect-timeout 5 " URL newpage " | tr -d \"\\n\" | sed -nr \"s/.*<span class=\\\"description-text\\\">([^>]+)<.*/\\1/p\""

						searchname = newpage
						gsub(/\/serie\//, "", searchname)

						if ( searchname ~ /\// )
						{
							split(searchname, a, "/")
							searchname = a[1]
						}

                       	pic = "echo " URLMAIN " | tr -d \"\\n\" && curl -s --connect-timeout 5 " URL newpage " | tr -d \"\\n\" | sed \"s/data-src=/\\ndata-src=/g\" | sed -nr \"s/.*data-src=\\\"([^\\\"]+)\\\".*/\\1/p\" | grep \"/" searchname "-\" | grep desktop | tail -n1 | tr -d \"\\n\""

						piccount += 1

						print title "#" SRC " " SRC " season \x27" newpage "\x27#" pic "#" PICNAME "." piccount ".jpg#" NAME "#0#" desc
					}
					next
				}
		' >$TMP/$FILENAME.list
	fi
	echo "$TMP/$FILENAME.list"
}

season()
{
	if [ ! -e "$TMP/$FILENAME.list" ]; then
		$curlbin -o - $URL$PAGE | tr -d '\n' | sed 's/<li class=/\n<li class=/g' | awk -v SRC=$SRC -v NAME=$NAME -v PICNAME=$PICNAME -v INPUT=$INPUT -v URL=$URL -v PAGE=$PAGE -v NEXT=$NEXT \
		'
			BEGIN \
				{
					suche = 0
				}
				/Staffeln:/ \
				{
					suche = 1
					next
				}
				/<li class="nav-item me-1 mb-2">/ \
				{
					if ( suche == 1 )
					{
						i = index($0, "href=\"") + 6
				        j = index(substr($0, i), "\"") - 1
				        newpage = substr($0, i, j)

						gsub(URL, "", newpage)

						i = index($0, "data-season-pill=\"") + 18
				        j = index(substr($0, i), "\"") - 1
				        season = substr($0, i, j)

						if (season != "")
						{
							if ( pic == "" )
							{
					  			pic = "http://openaaf.dyndns.tv/mediathek/menu/default.jpg"
							}

							piccount += 1

							if (season == "0")
								print "Filme#" SRC " " SRC " episode \x27" newpage "\x27#http://openaaf.dyndns.tv/mediathek/menu/s" season ".jpg#s" season ".jpg#" NAME "#0"
							else
								print "Staffel " season "#" SRC " " SRC " episode \x27" newpage "\x27#http://openaaf.dyndns.tv/mediathek/menu/s" season ".jpg#s" season ".jpg#" NAME "#0"
						}
						next
					}
				}
				/<li class="nav-item me-2">/ \
				{
					if ( suche == 1 )
					{
						suche = 0
					}
					next
				}
		' >$TMP/$FILENAME.list
	fi
	echo "$TMP/$FILENAME.list"
}

episode()
{
	if [ ! -e "$TMP/$FILENAME.list" ]; then
		$curlbin -o - $URL$PAGE | tr -d '\n' | sed 's/onclick="window.location=/\nonclick="window.location=/g' | awk -v SRC=$SRC -v NAME=$NAME -v PICNAME=$PICNAME -v INPUT=$INPUT -v URL=$URL -v PAGE=$PAGE -v NEXT=$NEXT \
		'
			BEGIN \
				{
					suche = 0
				}
				/onclick="window.location=/ \
				{
					i = index($0, "onclick=\"window.location=") + 26
			        j = index(substr($0, i), "\"") - 2
			        newpage = substr($0, i, j)
					gsub(URL, "", newpage)

					i = index(newpage, "/staffel-") + 9
			        j = index(substr(newpage, i), "/") - 1
			        season = substr(newpage, i, j)

					i = index(newpage, "/episode-") + 9
				    episode = substr(newpage, i, length(newpage) - 1)

					i = index($0, "episode-title-ger\" title=\"") + 26
			        j = index(substr($0, i), "\"") - 1
			        title = substr($0, i, j)

					if ( title == "" )
					{
						i = index($0, "episode-title-eng\" title=\"") + 26
					    j = index(substr($0, i), "\"") - 1
					    title = substr($0, i, j)
					}

					if ( title == "" )
					{
						i = index($0, "<span class=\"badge badge-release mt-1\" title=\"Releases soon\">") + 61
				        j = index(substr($0, i), "<") - 1
				        title = substr($0, i, j)

						i = index($0, "<span class=\"badge badge-release mt-1\" title=\"Releases soon\">") + 61
				        j = index(substr($0, i), "<") - 1
				        title = substr($0, i, j)
					}

                    gsub(/^[ \t]+/, "", title)
                    gsub(/[ \t]+$/, "", title)
                    gsub(/\t+/, "", title)

                    gsub(/^[ ]+/, "", title)
                    gsub(/[ ]+$/, "", title)
                    gsub(/ +/, " ", title)

					gsub(/&amp;+/, "\\&", title)
					gsub(/&amp;+/, "\\&", title)
					gsub(/&quot;+/, "\x22", title)
					gsub(/&quot;+/, "\x22", title)
					gsub(/&#039;+/, "\x27", title)
					gsub(/&#039;+/, "\x27", title)

					gsub(/\x23/, "\"hashtag\"", title)
					if (season != "")
					{
						if ( pic == "" )
						{
				  			pic = "http://openaaf.dyndns.tv/mediathek/menu/default.jpg"
						}

						piccount += 1

						piccount += 1

						if (season == "0")
						{
							if (title == "")
								print "Film " episode "#" SRC " " SRC " hosterlist \x27" newpage "\x27#http://openaaf.dyndns.tv/mediathek/menu/s" season "e" episode ".jpg#s" season "e" episode ".jpg#" NAME "#0"
	                        else
	   							print "Film " episode " - " title "#" SRC " " SRC " hosterlist \x27" newpage "\x27#http://openaaf.dyndns.tv/mediathek/menu/s" season "e" episode ".jpg#s" season "e" episode ".jpg#" NAME "#0"
						}
						else
						{
							if (title == "")
								print "S" season "E" episode "#" SRC " " SRC " hosterlist \x27" newpage "\x27#http://openaaf.dyndns.tv/mediathek/menu/s" season "e" episode ".jpg#s" season "e" episode ".jpg#" NAME "#0"
	                        else
	   							print "S" season "E" episode " - " title "#" SRC " " SRC " hosterlist \x27" newpage "\x27#http://openaaf.dyndns.tv/mediathek/menu/s" season "e" episode ".jpg#s" season "e" episode ".jpg#" NAME "#0"
						}
					}
					next
				}

		' >$TMP/$FILENAME.list
	fi
	echo "$TMP/$FILENAME.list"
}

listpopular()
{
	if [ ! -e "$TMP/$FILENAME.list" ]; then
		$curlbin -o - "$URL/$PAGE" | tr -d '\n' | sed 's/<h2 class=/\n<h2 class=/g' | sed 's/<h3 class=/\n<h3 class=/g' | sed 's/<div class=/\n<div class=/g' | awk -v URLMAIN=$URLMAIN -v SRC=$SRC -v pages=$pages -v NAME=$NAME -v NEXT="$NEXT" -v PICNAME=$PICNAME -v INPUT=$INPUT -v URL="$URL" -v PAGE="$PAGE" -v PAGE2=$PAGE2 -v BIN=$BIN \
		'
			BEGIN \
				{
					suche = 0
				}
				/<div class="mb-5">/ \
				{
					suche = 1
				}
				/class="h5 fw-bold text-white mb-/ \
				{
					i = index($0, "<h2 class=\"h5 fw-bold text-white mb") + 43
			        j = index(substr($0, i), "<") - 1
			       	title = substr($0, i, j)

                    gsub(/^[ \t]+/, "", title)
                    gsub(/[ \t]+$/, "", title)
                    gsub(/\t+/, "", title)

                    gsub(/^[ ]+/, "", title)
                    gsub(/[ ]+$/, "", title)
                    gsub(/ +/, " ", title)

					gsub("⭐️", "", title)
					gsub("🥰 ", "", title)
					gsub("🔥 ", "", title)

					if (title != NEXT)
					{
						next
					}
					else
					{
						suche = 3
					}
				}
				/<div class="col-6 col-sm-4 col-md-3 col-lg-2 col-xl-2">/ \
				{
					if ( suche == 3 )
					{
						i = index($0, "href=\"") + 6
				        j = index(substr($0, i), "\"") - 1
				        newpage = substr($0, i, j)

						gsub(URL, "", newpage)

						i = index($0, "alt=\"") + 5
				        j = index(substr($0, i), "\"") - 1
				       	title = substr($0, i, j)

						gsub(/&amp;+/, "\\&", title)
						gsub(/&amp;+/, "\\&", title)
						gsub(/&quot;+/, "\x22", title)
						gsub(/&quot;+/, "\x22", title)
						gsub(/&#039;+/, "\x27", title)
						gsub(/&#039;+/, "\x27", title)
						gsub(/\x23/, "\"hashtag\"", title)

						i = index($0, "src=\"") + 5
				        j = index(substr($0, i), "\"") - 1
				        pic = substr($0, i, j)

						if (substr(pic,1,1) == "/") 
							pic = URLMAIN pic

						if (title != "")
						{
							if ( pic == "" )
							{
				  				pic = "http://openaaf.dyndns.tv/mediathek/menu/default.jpg"
							}

                            if (pic ~ /png/)
                                picext = ".png"
                            else
                                picext = ".jpg"

                            desc = "curl --connect-timeout 5 " URL newpage " | tr -d \"\\n\" | sed -nr \"s/.*<span class=\\\"description-text\\\">([^>]+)<.*/\\1/p\""

							piccount += 1

    						print title "#" SRC " " SRC " season \x27" newpage "\x27#" pic "#" PICNAME "." piccount picext "#" NAME "#0#" desc

						}
						next
					}
				}
			END \
				{
				}
		' >$TMP/$FILENAME.list
	fi
	echo "$TMP/$FILENAME.list"
}

popular()
{
	if [ ! -e "$TMP/$FILENAME.list" ]; then
		$curlbin -o - $URL/$PAGE | tr -d '\n' | sed 's/<h2 class=/\n<h2 class=/g' | sed 's/<h3 class=/\n<h3 class=/g' | sed 's/<div class=/\n<div class=/g' | awk -v URLMAIN=$URLMAIN -v SRC=$SRC -v pages=$pages -v NAME=$NAME -v NEXT=$NEXT -v PICNAME=$PICNAME -v INPUT=$INPUT -v URL=$URL -v PAGE=$PAGE -v NEXT=$NEXT -v PAGE2=$PAGE2 -v BIN=$BIN \
		'
			BEGIN \
				{
					suche = 0
				}
				/<div class="mb-5">/ \
				{
					suche = 1
				}
				/class="h5 fw-bold text-white mb-/ \
				{
					if ( suche == 1 )
					{
						i = index($0, "href=\"") + 6
				        j = index(substr($0, i), "\"") - 1
				        newpage = substr($0, i, j)

						gsub(URL, "", newpage)

						i = index($0, "<h2 class=\"h5 fw-bold text-white mb") + 43
				        j = index(substr($0, i), "<") - 1
				       	title = substr($0, i, j)

                        gsub(/^[ \t]+/, "", title)
                        gsub(/[ \t]+$/, "", title)
                        gsub(/\t+/, "", title)

                        gsub(/^[ ]+/, "", title)
                        gsub(/[ ]+$/, "", title)
                        gsub(/ +/, " ", title)

						gsub("⭐️", "", title)
						gsub("🥰 ", "", title)
						gsub("🔥 ", "", title)

						gsub(/&amp;+/, "\\&", title)
						gsub(/&amp;+/, "\\&", title)
						gsub(/&quot;+/, "\x22", title)
						gsub(/&quot;+/, "\x22", title)
						gsub(/&#039;+/, "\x27", title)
						gsub(/&#039;+/, "\x27", title)
						gsub(/\x23/, "\"hashtag\"", title)

						pic = title
						gsub(" ", "_", pic)

                        desc = "curl --connect-timeout 5 " URL newpage " | tr -d \"\\n\" | sed -nr \"s/.*<span class=\\\"description-text\\\">([^>]+)<.*/\\1/p\""

						if (title != "")
						{
      						pic = tolower(pic)
    						print title "#" SRC " " SRC " listpopular \x27" PAGE "\x27 \x27" title "\x27#http://openaaf.dyndns.tv/mediathek/menu/" pic ".jpg#" pic ".jpg#" NAME "#0#" desc
						}
						next
					}
				}
			END \
				{
				}
		' >$TMP/$FILENAME.list
	fi
	echo "$TMP/$FILENAME.list"
}

new()
{
	if [ ! -e "$TMP/$FILENAME.list" ]; then
		$curlbin -o - $URL$PAGE | tr -d '\n' | sed 's/<div class=/\n<div class=/g' | awk -v URLMAIN=$URLMAIN -v SRC=$SRC -v pages=$pages -v NAME=$NAME -v NEXT=$NEXT -v PICNAME=$PICNAME -v INPUT=$INPUT -v URL=$URL -v PAGE=$PAGE -v NEXT=$NEXT -v PAGE2=$PAGE2 -v BIN=$BIN \
		'
			BEGIN \
				{
					suche = 0
				}
				/>Neu auf S.to</ \
				{
					suche = 1
					next
				}
				/>Beliebte Sammlungen</ \
				{
					suche = 0
					next
				}
				/<div class="swiper-slide">/ \
				{
					if (suche == 1)
					{
						i = index($0, "href=\"") + 6
				    	j = index(substr($0, i), "\"") - 1
				    	newpage = substr($0, i, j)

						gsub(URL, "", newpage)

						i = index($0, "alt=\"") + 5
				    	j = index(substr($0, i), "\"") - 1
				    	title = substr($0, i, j)

						i = index($0, "src=\"") + 5
				    	j = index(substr($0, i), "\"") - 1
				    	pic = substr($0, i, j)

						if (substr(pic,1,1) == "/") 
							pic = URLMAIN pic

						gsub(/&amp;+/, "\\&", title)
						gsub(/&amp;+/, "\\&", title)
						gsub(/&quot;+/, "\x22", title)
						gsub(/&quot;+/, "\x22", title)
						gsub(/&#039;+/, "\x27", title)
						gsub(/&#039;+/, "\x27", title)

						if (title != "")
						{
							if ( pic == "" || pic ~ /.svg/)
							{
				  				pic = "http://openaaf.dyndns.tv/mediathek/menu/default.jpg"
							}

		                    desc = "curl --connect-timeout 5 " URL newpage " | tr -d \"\\n\" | sed -nr \"s/.*<span class=\\\"description-text\\\">([^>]+)<.*/\\1/p\""

							piccount += 1

							print title "#" SRC " " SRC " season \x27" newpage "\x27#" pic "#" PICNAME "." piccount picext "#" NAME "#0#" desc

						}
					}
                    next
                }             
			END \
				{
				}
		' >$TMP/$FILENAME.list
	fi
	echo "$TMP/$FILENAME.list"
}

latest()
{
	if [ ! -e "$TMP/$FILENAME.list" ]; then
		$curlbin -o - $URL$PAGE | tr -d '\n' | sed 's/<a class=/\n<a class=/g' | awk -v URLMAIN=$URLMAIN -v SRC=$SRC -v pages=$pages -v NAME=$NAME -v NEXT=$NEXT -v PICNAME=$PICNAME -v INPUT=$INPUT -v URL=$URL -v PAGE=$PAGE -v NEXT=$NEXT -v PAGE2=$PAGE2 -v BIN=$BIN \
		'
			BEGIN \
				{
					suche = 0
				}
				/<a class="latest-episode-row/ \
				{
					i = index($0, "href=\"") + 6
		        	j = index(substr($0, i), "\"") - 1
		        	newpage = substr($0, i, j)

					gsub(URL, "", newpage)

					i = index($0, "<strong>") + 8
					i = index($0, "\"ep-title\" title=\"") + 18
		        	j = index(substr($0, i), "\"") - 1
		        	name = substr($0, i, j)

					gsub(/&amp;+/, "\\&", name)
					gsub(/&amp;+/, "\\&", name)
					gsub(/&quot;+/, "\x22", name)
					gsub(/&quot;+/, "\x22", name)
					gsub(/&#039;+/, "\x27", name)
					gsub(/&#039;+/, "\x27", name)
					gsub(/\x23/, "\"hashtag\"", name)

					i = index($0, "ep-season\">") + 11
		        	j = index(substr($0, i), "<") - 1
		        	season = substr($0, i, j)

					i = index($0, "ep-episode\">") + 12
		        	j = index(substr($0, i), "<") - 1
		        	episode = substr($0, i, j)

					i = index($0, "\"ep-time\">") + 10
		        	j = index(substr($0, i), "<") - 1
		        	date = substr($0, i, j)

                    gsub(/^[ \t]+/, "", date)
                    gsub(/[ \t]+$/, "", date)
                    gsub(/\t+/, "", date)

                    gsub(/^[ ]+/, "", date)
                    gsub(/[ ]+$/, "", date)
                    gsub(/ +/, "", date)

					i = index($0, "title=\"") + 7
		        	j = index(substr($0, i), "\"") - 1
		        	episodename = substr($0, i, j)

					if ( $0 ~ /icon-flag-german/ )
						lang = "de"
					else if ( $0 ~ /icon-flag-english-german/ )
						lang = "de/en"
					else if ( $0 ~ /icon-flag-english/ )
						lang = "en"
					else
						lang = "??"

					cmd = "curl " URL newpage " | tr -d \x27\\n\x27 | tr -d \x27\\r\x27 | sed \x27s/<h2 class=/\\nfound=/g\x27 | sed \x27s,</h2>,\\n</h2>,g\x27 | grep found= | grep \x27h4 mb-1\x27 | cut -d \x27>\x27 -f2 | sed -e \x27s/[[:space:]]\\+/ /g\x27 -e \x27s/^ //\x27 -e \x27s/ $//\x27"
					cmdck=cmd | getline cmdmap
					close(cmd)
					if(cmdck == 1) title = cmdmap

					if(title == "")
						title = season "" episode

					gsub(/&amp;+/, "\\&", title)
					gsub(/&amp;+/, "\\&", title)
					gsub(/&quot;+/, "\x22", title)
					gsub(/&quot;+/, "\x22", title)
					gsub(/&#039;+/, "\x27", title)
					gsub(/&#039;+/, "\x27", title)
					gsub(/\x23/, "\"hashtag\"", title)

					if (name != "")
					{
						if ( pic == "" || pic ~ /.svg/)
						{
			  				pic = "http://openaaf.dyndns.tv/mediathek/menu/default.jpg"
						}
                        desc = "curl --connect-timeout 5 " URL newpage " | tr -d \"\\n\" | sed -nr \"s/.*<span class=\\\"description-text\\\">([^>]+)<.*/\\1/p\""

						searchname = newpage
						gsub(/\/serie\//, "", searchname)

						if ( searchname ~ /\// )
						{
							split(searchname, a, "/")
							searchname = a[1]
						}
                      	pic = "echo " URLMAIN " | tr -d \"\\n\" && curl -s --connect-timeout 5 " URL newpage " | tr -d \"\\n\" | sed \"s/data-src=/\\ndata-src=/g\" | sed -nr \"s/.*data-src=\\\"([^\\\"]+)\\\".*/\\1/p\" | grep \"/" searchname "-\" | grep desktop | tail -n1 | tr -d \"\\n\""

						piccount += 1

                        gsub("S0", "S", season)
                        gsub("E0", "E", episode)

                        gsub("S", "", season)
                        gsub("E", "", episode)

						if ( pic == "" || pic ~ /.svg/ || pic == URLMAIN)
							print name " (" title ") (" lang ") (" date ")#" SRC " " SRC " hosterlist \x27" newpage "\x27#http://openaaf.dyndns.tv/mediathek/menu/s" season "e" episode ".jpg#s" season "e" episode ".jpg#" NAME "#0#" desc
						else
							print name " (" title ") (" lang ") (" date ")#" SRC " " SRC " hosterlist \x27" newpage "\x27#" pic "#" PICNAME "." piccount picext "#" NAME "#0#" desc
					}
                    next
                }             
			END \
				{
				}
		' >$TMP/$FILENAME.list
	fi
	echo "$TMP/$FILENAME.list"
}

hosterlist()
{
	if [ ! -e "$TMP/$FILENAME.list" ]; then
		$curlbin -o - $URL$PAGE | tr -d '\n' | tr -d '\r' | sed 's/<div class=/\n<div class=/g' | grep ^"<div class=" | awk -v SRC=$SRC -v NAME=$NAME -v PICNAME=$PICNAME -v INPUT=$INPUT -v URL=$URL -v PAGE=$PAGE -v NEXT=$NEXT \
		'
			BEGIN \
				{
					suche = 1
				}
				/<div class="link-wrapper position-relative">/ \
				{
					if ( suche == 1 )
					{
						i = index($0, "data-play-url=\"") + 15
				        j = index(substr($0, i), "\"") - 1
				        newpage = substr($0, i, j)

						gsub(URL, "", newpage)

						i = index($0, "data-provider-name=\"") + 20
				        j = index(substr($0, i), "\"") - 1
				        title = substr($0, i, j)

						i = index($0, "data-language-label=\"") + 21
				        j = index(substr($0, i), "\"") - 1
				        label = substr($0, i, j)

                        if ($0 ~ /data-language-id="1"/)
                            lang = "de"
                        else if ($0 ~ /data-language-id="2"/)
                            lang = "en"
                        else if ($0 ~ /data-language-id="3"/)
                            lang = "de sub"
                        else
                            lang = label

						pic = tolower(title)

						if (title != "")
						{
							if ( pic == "" )
							{
					  			pic = "http://openaaf.dyndns.tv/mediathek/menu/default.jpg"
							}

							piccount += 1

							print title " (" lang ")#" SRC " " SRC " hoster \x27" newpage "\x27#http://openaaf.dyndns.tv/mediathek/menu/" pic ".jpg#" pic ".jpg#" NAME "#111"
						}
						next
					}
				}
		' >$TMP/$FILENAME.list
	fi
	echo "$TMP/$FILENAME.list"
}

hoster()
{
	rm -f $TMP/cache.hoster.$hoster.* > /dev/null 2>&1
	$curlbin  "$URL/login" -o $TMP/cache.hoster.$hoster.1.login

	cat $TMP/cache.hoster.$hoster.1.login | sed -nr 's/.*<meta name="csrf-token" content="([^"]+)".*/\1/p' >$TMP/cache.hoster.$hoster.2.login.csrftoken
	csrftoken=$(cat $TMP/cache.hoster.$hoster.2.login.csrftoken)

	redirect=1
	if [ "$redirect" == 1 ];then
		$curlbin -H "X-Requested-With: XMLHttpRequest" -X POST --data-raw "_token=$csrftoken&email=Ither1981%40dayrep.com&password=abc123456" "$URL/login" -o $TMP/cache.hoster.$hoster.3.post
		$curlbin  "$URL" -o $TMP/cache.hoster.$hoster.4.index
	else
		$curlbin2 -H "X-Requested-With: XMLHttpRequest" -X POST --data-raw "_token=$csrftoken&email=Ither1981%40dayrep.com&password=abc123456" "$URL/login" -o $TMP/cache.hoster.$hoster.3.post
		$curlbin2 "$URL/account" -o $TMP/cache.hoster.$hoster.4.account
	fi

	$curlbin2 "$URL$PAGE" -o $TMP/cache.hoster.$hoster.5.page

	if [ $(cat /tmp/localcache/cache.hoster.hoster.5.page | grep "Redirecting to" | wc -l) != 0 ]; then
		cat $TMP/cache.hoster.$hoster.5.page | sed -nr 's/.*Redirecting to <a href="([^"]+)".*/\1/p' >$TMP/cache.hoster.$hoster.6.url.redirecting
		URL=`cat $TMP/cache.hoster.$hoster.6.url.redirecting | tr -d '\n' | tr -d '\r'`
	else
		cat $TMP/cache.hoster.$hoster.5.page >$TMP/cache.hoster.$hoster.6.url
		URL=`cat $TMP/cache.hoster.$hoster.6.url | tr -d '\n' | tr -d '\r'`
	fi

	echo $URL
}

search()
{
	MAXPAGES=$($curlbin "$URL/$PAGE$NEXT" | sed -nr 's/.*amp;page=([^=]+)">.*/\1/p' | sort -um | tail -n1)
	if [ ! -e "$TMP/$FILENAME.list" ]; then
		$curlbin -o - "$URL/$PAGE$NEXT" | tr -d '\n' | tr -d '\r' | sed 's/<div class="col-6 col-md-4/\n<div class="col-6 col-md-4/g' | awk -v URLMAIN="$URLMAIN" -v SRC="$SRC" -v NAME="$NAME" -v PICNAME="$PICNAME" -v INPUT="$INPUT" -v PAGE="$PAGE" -v NEXT="$NEXT" -v BIN="$BIN" -v MAXPAGES="$MAXPAGES" \
		'
			# BEGIN variable setzen
			BEGIN \
				{
					suche = 0
				}
				/<div class="col-6 col-md-4 col-lg-2">/ \
				{
					i = index($0, "href=\"") + 6
			        j = index(substr($0, i), "\"") - 1
			        newpage = substr($0, i, j)

					i = index($0, "alt=\"") + 5
			        j = index(substr($0, i), "\"") - 1
			        title = substr($0, i, j)

					gsub(/&amp;+/, "\\&", title)
					gsub(/&amp;+/, "\\&", title)
					gsub(/&quot;+/, "\x22", title)
					gsub(/&quot;+/, "\x22", title)
					gsub(/&#039;+/, "\x27", title)
					gsub(/&#039;+/, "\x27", title)
					gsub(/\x23/, "\"hashtag\"", title)

					i = index($0, "src=\"") + 5
			        j = index(substr($0, i), "\"") - 1
			        pic = substr($0, i, j)

	                gsub(/^[ \t]+/, "", pic)
	                gsub(/[ \t]+$/, "", pic)
	                gsub(/\t+/, "", pic)

	                gsub(/^[ ]+/, "", pic)
	                gsub(/[ ]+$/, "", pic)
	                gsub(/ +/, "", pic)
	                gsub(/ /, "", pic)

					if (substr(pic,1,1) == "/") 
						pic = URLMAIN pic

					if (++dup[title] == 1 && title != "" && title !~ "= = =")
					{

						if ( pic == "" )
						{
				  			pic = "http://openaaf.dyndns.tv/mediathek/menu/default.jpg"
						}

                        desc = "curl --connect-timeout 5 " URLMAIN newpage " | tr -d \"\\n\" | sed -nr \"s/.*<span class=\\\"description-text\\\">([^>]+)<.*/\\1/p\""

						piccount += 1
						print title "#" SRC " " SRC " season \x27" newpage "\x27#" pic "#" PICNAME "." piccount ".jpg#" NAME "#0#" desc
					}
					next
				}
			END \
				{
					if (NEXT <= MAXPAGES && MAXPAGES != "")
						print "Page (" NEXT + 1 "/" MAXPAGES ")#" SRC " " SRC " " INPUT " \x27" PAGE "\x27 " NEXT + 1 "#http://openaaf.dyndns.tv/mediathek/menu/next.jpg#next.jpg#" NAME "#0"

				}
		' >$TMP/$FILENAME.list
	fi
	echo "$TMP/$FILENAME.list"
}

listsorted()
{
	MAXPAGES=$($curlbin "$URL/$PAGE$NEXT" | sed -nr 's/.*page=([^=]+)">.*/\1/p' | sort -um | tail -n1)
	if [ ! -e "$TMP/$FILENAME.list" ]; then
		$curlbin -o - "$URL/$PAGE$NEXT" | tr -d '\n' | sed 's/<h2 class=/\n<h2 class=/g' | sed 's/<h3 class=/\n<h3 class=/g' | sed 's/<div class=/\n<div class=/g' | awk -v URLMAIN="$URLMAIN" -v SRC="$SRC" -v NAME="$NAME" -v PICNAME="$PICNAME" -v INPUT="$INPUT" -v PAGE="$PAGE" -v NEXT="$NEXT" -v BIN="$BIN" -v MAXPAGES="$MAXPAGES" \
		'
			BEGIN \
				{
					suche = 0
				}
				/<div class="col-6 col-sm-4 col-md-3 col-lg-2 col-xl-2">/ \
				{
					i = index($0, "href=\"") + 6
			        j = index(substr($0, i), "\"") - 1
			        newpage = substr($0, i, j)

					i = index($0, "alt=\"") + 5
			        j = index(substr($0, i), "\"") - 1
			        title = substr($0, i, j)

					gsub(/&amp;+/, "\\&", title)
					gsub(/&amp;+/, "\\&", title)
					gsub(/&quot;+/, "\x22", title)
					gsub(/&quot;+/, "\x22", title)
					gsub(/&#039;+/, "\x27", title)
					gsub(/&#039;+/, "\x27", title)
					gsub(/\x23/, "\"hashtag\"", title)

					i = index($0, "src=\"") + 5
			        j = index(substr($0, i), "\"") - 1
			        pic = substr($0, i, j)

	                gsub(/^[ \t]+/, "", pic)
	                gsub(/[ \t]+$/, "", pic)
	                gsub(/\t+/, "", pic)

	                gsub(/^[ ]+/, "", pic)
	                gsub(/[ ]+$/, "", pic)
	                gsub(/ +/, "", pic)
	                gsub(/ /, "", pic)

					if (substr(pic,1,1) == "/") 
						pic = URLMAIN pic

					if (++dup[title] == 1 && title != "" && title !~ "= = =")
					{

						if ( pic == "" )
						{
				  			pic = "http://openaaf.dyndns.tv/mediathek/menu/default.jpg"
						}

                        desc = "curl --connect-timeout 5 " URLMAIN newpage " | tr -d \"\\n\" | sed -nr \"s/.*<span class=\\\"description-text\\\">([^>]+)<.*/\\1/p\""

						piccount += 1
						print title "#" SRC " " SRC " season \x27" newpage "\x27#" pic "#" PICNAME "." piccount ".jpg#" NAME "#0#" desc
					}
					next
				}
			END \
				{
					if (NEXT <= MAXPAGES && MAXPAGES != "")
						print "Page (" NEXT + 1 "/" MAXPAGES ")#" SRC " " SRC " " INPUT " \x27" PAGE "\x27 " NEXT + 1 "#http://openaaf.dyndns.tv/mediathek/menu/next.jpg#next.jpg#" NAME "#0"

				}
		' >$TMP/$FILENAME.list
	fi
	echo "$TMP/$FILENAME.list"
}

listgenre()
{
	MAXPAGES=$($curlbin "$URL$PAGE$NEXT" | sed -nr 's/.*page=([^=]+)">.*/\1/p' | sort -um | tail -n1)
	if [ ! -e "$TMP/$FILENAME.list" ]; then
		$curlbin -o - "$URL$PAGE$NEXT" | tr -d '\n' | sed 's/<div class=/\n<div class=/g' | awk -v URLMAIN="$URLMAIN" -v SRC="$SRC" -v NAME="$NAME" -v PICNAME="$PICNAME" -v INPUT="$INPUT" -v PAGE="$PAGE" -v NEXT="$NEXT" -v BIN="$BIN" -v MAXPAGES="$MAXPAGES" \
		'
			BEGIN \
				{
					suche = 0
				}
				/<div class="col-6 col-sm-4 col-md-3 col-lg-2 col-xl-2">/ \
				{
					i = index($0, "href=\"") + 6
			        j = index(substr($0, i), "\"") - 1
			        newpage = substr($0, i, j)

					i = index($0, "alt=\"") + 5
			        j = index(substr($0, i), "\"") - 1
			        title = substr($0, i, j)

					gsub(/&amp;+/, "\\&", title)
					gsub(/&amp;+/, "\\&", title)
					gsub(/&quot;+/, "\x22", title)
					gsub(/&quot;+/, "\x22", title)
					gsub(/&#039;+/, "\x27", title)
					gsub(/&#039;+/, "\x27", title)
					gsub(/\x23/, "\"hashtag\"", title)

					i = index($0, "src=\"") + 5
			        j = index(substr($0, i), "\"") - 1
					# newpage = /view_video.php?viewkey=127170590
			        pic = substr($0, i, j)

	                gsub(/^[ \t]+/, "", pic)
	                gsub(/[ \t]+$/, "", pic)
	                gsub(/\t+/, "", pic)

	                gsub(/^[ ]+/, "", pic)
	                gsub(/[ ]+$/, "", pic)
	                gsub(/ +/, "", pic)
	                gsub(/ /, "", pic)

					if (substr(pic,1,1) == "/") 
						pic = URLMAIN pic

					if (++dup[title] == 1 && title != "" && title !~ "= = =")
					{

						if ( pic == "" )
						{
				  			pic = "http://openaaf.dyndns.tv/mediathek/menu/default.jpg"
						}

                        desc = "curl --connect-timeout 5 " URLMAIN newpage " | tr -d \"\\n\" | sed -nr \"s/.*<span class=\\\"description-text\\\">([^>]+)<.*/\\1/p\""

						piccount += 1

						print title "#" SRC " " SRC " season \x27" newpage "\x27#" pic "#" PICNAME "." piccount ".jpg#" NAME "#0#" desc
					}
					next
				}
			END \
				{
					if (NEXT <= MAXPAGES && MAXPAGES != "")
						print "Page (" NEXT + 1 "/" MAXPAGES ")#" SRC " " SRC " " INPUT " \x27" PAGE "\x27 " NEXT + 1 "#http://openaaf.dyndns.tv/mediathek/menu/next.jpg#next.jpg#" NAME "#0"

				}
		' >$TMP/$FILENAME.list
	fi
	echo "$TMP/$FILENAME.list"
}

case $INPUT in
	init) $INPUT;;
	mainmenu) $INPUT;;
	all) $INPUT;;
	sorted) $INPUT;;
	season) $INPUT;;
	episode) $INPUT;;
	latest) $INPUT;;
	listpopular) $INPUT;;
	listsorted) $INPUT;;
	listgenre) $INPUT;;
	hosterlist) $INPUT;;
	hoster) $INPUT;;
	search) $INPUT;;
	genre) $INPUT;;
	new) $INPUT;;
	popular) $INPUT;;
esac

