Advertisement
mosaid

animes.sh

Aug 6th, 2019
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.22 KB | None | 0 0
  1. #!/bin/bash
  2. dir="directory where to save files"
  3. file="file where to save links to your favorite animes from https://www.thewatchcartoononline.tv"
  4. linksfile="this file is updated to the latest episodes of your animes"
  5.  
  6. i=0
  7. while read -r l ; do
  8.     links[$i]="$l"
  9.     i=$((i+1))
  10. done < "$file"
  11.  
  12. function get_title(){
  13.     t=$( echo "$1" \
  14.         |cut -d'|' -f3 \
  15.         |cut -d'/' -f5 \
  16.         |sed 's/-english-subbed-guide//' \
  17.         |sed 's/-english-dubbed-guide//' \
  18.         |sed 's/-english-subbed-episodes//' \
  19.         |sed 's/-english-dubbed-episodes//' \
  20.         |sed 's/-english-subbed//' \
  21.         |sed 's/-english-dubbed//'
  22.     )
  23.     echo "$t"
  24. }
  25.  
  26. function get_ans(){
  27.     ans=$(
  28.         for k in "${links[@]}" ; do
  29.             get_title "$k"
  30.         done |rofi -dmenu -p "my mangas" -width -180
  31.     )
  32.     [[ -z "$ans" ]] && exit
  33. }
  34.  
  35. function get_index(){
  36.     get_ans
  37.     index=$(
  38.         for (( i=0 ; i <=${#links[@]} ; i++ )) ; do
  39.             if echo "${links[$i]}" |grep "$ans" >/dev/null
  40.                 then echo "$i"
  41.                 break
  42.             fi
  43.         done
  44.     )
  45. }
  46.  
  47. function CURL() {
  48.     userAgent="Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:48.0) "
  49.     userAgent+="Gecko/20100101 Firefox/48.0"
  50.     curl -s -A "$userAgent" "$@"
  51. }
  52.  
  53. function getInfo(){
  54.     get_index
  55.     echo
  56.     echo "    $ans"
  57.     echo
  58.     if ! [[ -f "$dir/$ans" ]] ; then
  59.         l=$( echo "${links[$index]}" |cut -d'|' -f3 )
  60.         CURL "$l" \
  61.         | sed -n -e '/<h3>Info:/{N;N;N;N;s/<[^>]*>//g;p}' \
  62.         | perl -C -MHTML::Entities -pe 'decode_entities($_);' \
  63.         | sed 's/[^[:print:]]//g' \
  64.         | fold -w 60 -s \
  65.         > "$dir/$ans"
  66.     fi
  67.     cat "$dir/$ans"
  68. }
  69.  
  70. function list_them(){
  71.     i=1
  72.     for k in "${links[@]}" ; do
  73.         t=$( get_title "$k" )
  74.         stars=$(echo "$k" | cut -d'|' -f1)
  75.         stat=$(echo "$k" | cut -d'|' -f2)
  76.         if echo "$k" |grep ongoing >/dev/null
  77.             then printf '\033[1;32m'
  78.             else printf '\033[1;0m'
  79.         fi
  80.         if ! [[ -f "$dir/$t" ]]
  81.             then printf '\033[1;31m'
  82.         fi
  83.         printf '%12s %5s %2d: %s\n' "$stat" "$stars" "$i" "$t"
  84.         i=$((i+1))
  85.     done
  86. }
  87.  
  88. function star_it(){
  89.     get_index
  90.     stars="$2"
  91.     line=$( cat "$file" |grep -no "${links[$index]}"| cut -d: -f1)
  92.     if [[ "$1" == s ]] ; then
  93.     awk -i inplace -F'|' -v s="$stars" -v l="$line" 'NR==l{ $1=s }{print}' OFS="|" "$file"
  94.     fi
  95.     if [[ "$1" == f ]] ; then
  96.         awk -i inplace -F'|' -v s="$stars" -v l="$line" 'NR==l{ $2=s }{print}' OFS="|" "$file"
  97.     fi
  98. }
  99.  
  100. function delete_it(){
  101.     get_index
  102.     t=$(get_title "${links[$index]}" )
  103.     rm "$dir/$t"
  104. }
  105.  
  106. function watch_f(){
  107.     declare -A arr=()
  108.     n=${#links[@]}
  109.     modif=$(stat "$linksfile" |grep Modify | awk '{print $2}' )
  110.     today=$(date '+%Y-%m-%d' )
  111.     if [[ "$modif" != "$today" ]] || [[ "$1" == f ]] ; then
  112.         > "$linksfile"
  113.         for (( i=0 ; i<$n ; i++ )) ; do
  114.             url=$(echo "${links[$i]}" |cut -d'|' -f3 )
  115.             data=$( CURL "$url" | sed -E -n '/<div class="cat-eps">/{n;p;q}' )
  116.             link=$(echo "$data" | sed -E 's/^.*href="(.*)" rel=.*$/\1/' )
  117.             title=$(echo "$data" | sed -E 's/^.*title="(.*)" class=.*$/\1/' )
  118.             echo "$link|$title" >> "$linksfile"
  119.         done
  120.     fi
  121.     while read -r l ; do
  122.         title="${l#*|}"
  123.         link="${l%|*}"
  124.         ! [[ -z "$title" ]] && arr["$title"]="$link"
  125.     done < "$linksfile"
  126.     LIST=$( for k in "${!arr[@]}" ; do
  127.         printf '%s\n' "$k"
  128.         done
  129.     )
  130.     ans=$(
  131.     echo "$LIST" \
  132.         |sort \
  133.         |rofi -dmenu -p "watch manga" -width -80 -columns 1 -location 8
  134.     )
  135.     ! [[ -z "$ans" ]] && echo "${arr[$ans]}"
  136. }
  137.  
  138.  
  139. case "$1" in
  140.     l)  list_them ;;
  141.     t)  getInfo ;;
  142.     w)  watch_f  "$2" ;;
  143.     s)  star_it s "$2" ;;
  144.     r)  delete_it ;;
  145.     f)  star_it f "$2" ;;
  146.     a)  url="$2"
  147.         url="${url//\\/}"
  148.         if grep -w "$url" "$file" >/dev/null
  149.         then
  150.             echo already added
  151.             else echo "||$url" >> "$file"
  152.         fi
  153.         ;;
  154.     o) list_them |grep ongoing ;;
  155.     n) list_them |grep -F '[new]' ;;
  156.     *)
  157.         get_index
  158.         echo "${links[$index]}" |cut -d'|' -f3
  159. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement