Instantly share code, notes, and snippets.

@girst /asciiplayer
Last active Aug 29, 2015

Embed
What would you like to do?
PornHub ASCII Player
#!/bin/bash
#PornHub ASCII player 1.0
#Copyright Tobias Girstmair
#Licenced under GPLv3 (GNU Public Licence)
#iSticktoit.net
#Do not remove this notice
progName="PornHub / YouPorn ASCII Player v1.0"
playMode=0 # 0=vlc, 1=vlc new window, 2=mplayer, 3=mplayer new window
logFile=/dev/null
setSearch() { #params: 1=lastsearch
page=1
isLastPage=0
while : ; do
exec 3<&1 #avoid tempfiles
search=$(dialog --title "$progName"\
--ok-label "Search" \
--cancel-label "Quit" \
--inputbox "Enter Search Term(s):" 8 52 "$1" 2>&1 1>&3)
[[ $? -ne 0 ]] && exit
exec 3<&-
[[ $search == "" ]] && continue
break;done
}
setCategory() { #params: 1=site
page=1
isLastPage=0
case $1 in
*ph) local categories=$(curl "http://www.pornhub.com/webmasters/categories" 2>$logFile);;
*yp) local categories=$(curl "http://www.youporn.com/api/webmasters/categories/" 2>$logFile);;
esac
local categoryList=$(echo "$categories"|grep -Po '"category":.*?[^\\]"},')
local arr=()
while read -r categoryItem; do
categoryItem=${categoryItem#'"category":"'}
categoryItem=${categoryItem%'"},'}
arr=("${arr[@]}" ${categoryItem// /%20} "$categoryItem" off)
done <<< "$categoryList"
exec 3<&1
category=$(dialog --title "$progName" \
--ok-label "OK" \
--cancel-label "Quit" \
--radiolist "Select a category to search in (SPACE selects, RETURN confirms)" 15 50 8 \
ALLCAT "All Categories" on "${arr[@]}" 2>&1 1>&3)
[[ $? -ne 0 ]] && exit
exec 3<&-
}
setSite() {
page=1
isLastPage=0
category=ALLCAT
exec 3<&1
site=$(dialog --title "$progName" \
--ok-label "Search" \
--cancel-label "Quit" \
--help-button --help-label "Categories" \
--menu "Search on..." 15 50 8 \
ph "PornHub" \
yp "YouPorn" 2>&1 1>&3)
[[ $? -eq 1 ]] && exit
exec 3<&-
if [[ $site == HELP* ]] ; then
setCategory "$site"
fi
}
doSearch() { #params: 1=lastsearch
setSearch "$1"
setSite
}
getVideoUrl_Pornhub() { #params: 1=videoid
local url=$(curl -L "http://www.pornhub.com/embed/$1" 2>$logFile |head -128 | tail -1)
url=${url#*: \'}
url=${url%\'*}
echo $url
}
getVideoUrl_YouPorn() { #params: 1=videoid
local url=$(curl -L "http://www.youporn.com/embed/$1/" 2>$logFile |head -112 | tail -1)
url=${url#*'<video src="'}
url=${url%%'\"*'}
url=${url//'&amp;'/'&'}
echo $url
}
playVideoId() { #params: 1=videoid 2=site
case $2 in
*ph) local url=$(getVideoUrl_Pornhub $1);;
*yp) local url=$(getVideoUrl_YouPorn $1);;
esac
# they filter useragents, we fuck up their statistics on pornhub.com/insights ;)
case $playMode in
0) CACA_DRIVER=ncurses cvlc -V caca --http-user-agent="Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:36.0) Gecko/20100101 Firefox/36.0" "$url" 2>$logFile & ;;
1) cvlc -V caca --http-user-agent="Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:36.0) Gecko/20100101 Firefox/36.0" "$url" 2>$logFile & ;;
2) CACA_DRIVER=ncurses mplayer -vo caca -quiet -http-header-fields 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:36.0) Gecko/20100101 Firefox/36.0' "$url" 2>$logFile & ;;
3) mplayer -vo caca -quiet -http-header-fields 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:36.0) Gecko/20100101 Firefox/36.0' "$url" 2>$logFile & ;;
esac
local pid=$! #return PID
read -n1 -r -p " Video is loading... `echo -en $'\n '`Press any key to quit..." key
kill -9 $pid
wait $pid 2>$logFile #discard killmsg
}
getSearchResults() { #params: 1=searchjson 2=page 3=islastpage
local videoIdList=$(echo "$1"|grep -Po '"video_id":.*?[^\\]",')
local videoTitleList=$(echo "$1"|grep -Po '"title":.*?[^\\]",')
local arr=()
#iterate over lists to fill array
[[ $2 -gt 1 ]] && arr=("${arr[@]}" PREV "Previous Page")
exec 6<<<"$videoTitleList"
while read -r vId; do
read -r vTitle <&6
local videoId=${vId#'"video_id":"'}
videoId=${videoId%'",'}
local videoTitle=${vTitle#'"title":"'}
videoTitle=${videoTitle%'",'}
arr=("${arr[@]}" $videoId "$videoTitle")
done <<< "$videoIdList"
exec 6<&-
[[ $3 -ne 1 ]] && arr=("${arr[@]}" NEXT "Next Page")
exec 3<&1
local selectedItem=$(dialog --clear --backtitle "(C) Tobias Girstmair - iSticktoit.net" \
--ok-label "Play" \
--cancel-label "Quit" \
--help-button --help-label "New Search" \
--title "$progName" \
--menu "Select on a Video below" 17 60 10 "${arr[@]}" 2>&1 1>&3)
exec 3<&-
echo $selectedItem 1>&2
}
getJson() { #params: 1=search 2=category 3=page 4=site
case $4 in
*ph) results=$(curl "http://www.pornhub.com/webmasters/search?search=${1// /%20}&category=$2&page=$3&thumbsize=small" 2>$logFile);;
*yp) results=$(curl "http://www.youporn.com/api/webmasters/search/?search=${1// /%20}&category=$2&page=$3&thumbsize=small" 2>$logFile);;
esac
echo "$results"|grep -Po '"code":.*?[^\\]",' >$logFile
if [[ $? -eq 0 ]] ; then
if [[ $3 -eq 1 ]] ; then
#return 2 #no results
dialog --title "$progName" \
--ok-label "New Search" \
--extra-button --extra-label "Select Category" \
--cancel-label "Set Site" \
--yesno "No results.\nWhat now?!" 7 52
case $? in
0) doSearch $1 && getJson $search $category 1 $site;;
3) setCategory $4 && getJson $1 $category 1 $4;;
1) setSite && getJson $1 $category 1 $site;;
esac
elif [[ $3 -gt 1 ]] ; then
local isLastPage=1
local page=$3
page=$((page-1))
getJson $1 $2 $page $4
return 1 #last page
else
return 0 #ok
fi
fi
#return 0=ok, 1=LastPage, 2=noresults
}
#global vars:
# search: Search text
# site: PornHub or YouPorn
# category
# page
doSearch
getJson "$search" "$category" $page "$site"
while : ; do
exec 3<&1
retval=$(getSearchResults "$results" $page $isLastPage 2>&1 1>&3)
[[ $retval == "" ]] && exit
exec 3<&-
case $retval in
NEXT)
page=$((page+1))
getJson "$search" "$category" $page "$site"
isLastPage=$?
[[ $isLastPage -eq 1 ]] && page=$((page-1))
continue;;
PREV)
page=$((page-1))
getJson "$search" "$category" $page "$site"
isLastPage=0
continue;;
HELP*) # new search button
doSearch "$search"
getJson "$search" "$category" $page "$site"
isLastPage=0
continue;;
*) #video
playVideoId $retval "$site"
esac
clear
break;done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment