mosaid

myanime.sh

Feb 29th, 2020
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.71 KB | None | 0 0
  1. #!/bin/bash
  2. dir="${HOME}/articls/manga"
  3. pyS="${HOME}/myanime/myanime.py"
  4.  
  5. function get_ans(){
  6.     ans=$(
  7.         python "$pyS" all|awk '{print $3}'|
  8.         rofi -i -dmenu -p "my mangas" -width -180
  9.     )
  10.     [[ -z "$ans" ]] && exit
  11.     echo "$ans"
  12. }
  13.  
  14. function CURL() {
  15.     userAgent="Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:48.0) "
  16.     userAgent+="Gecko/20100101 Firefox/48.0"
  17.     curl -s -A "$userAgent" "$@"
  18. }
  19.  
  20. function getInfo(){
  21.     title=$(get_ans)
  22.     echo
  23.     echo "    $title"
  24.     echo
  25.     if ! [[ -f "$dir/$title" ]] || \
  26.     ! [[ -f "$dir/pics/$title" ]] ; then
  27.         l=$( python "$pyS" url "$title" )
  28.         data=$(CURL "$l")
  29.         text=$( echo "$data" \
  30.             | sed -n -e '/<h3>Info:/{N;N;N;N;s/<[^>]*>//g;p}' \
  31.             | perl -C -MHTML::Entities -pe 'decode_entities($_);' \
  32.             | sed 's/[^[:print:]]//g' \
  33.             | fold -w 60 -s \
  34.         )
  35.         ! [[ -f "$dir/$title" ]] && {
  36.             echo "$text" > "$dir/$title"
  37.         }
  38.         pic=$(echo "$data" |
  39.             grep '<meta property="og:image" content='|
  40.             sed 's/<meta property="og:image" content=//'|
  41.             sed 's/ \/>//;s/"//'
  42.         )
  43.         ! [[ -f "$dir/pics/$title" ]] && {
  44.             wget -c "$pic" -q -O "$dir/pics/$title"
  45.         }
  46.     fi
  47.     cat "$dir/$title"
  48.     #tycat "$dir/pics/$title"
  49. }
  50.  
  51. function list_them(){
  52.     python "$pyS" all|
  53.     #sort -k2 -n |
  54.     awk -v p="$dir" '
  55.    {
  56.        color="\033[1;0m"
  57.        red="\033[1;31m"
  58.        file=p "/" $3
  59.        if($1 == "ongoing") color="\033[1;32m"
  60.        if(system("[ ! -f " file " ]") == 0) color=red
  61.        printf("%s%10s %s%d%s %s\n",color,$1,red,$2,color,$3)
  62.    }'
  63. }
  64.  
  65. function set_rank(){
  66.     title=$(get_ans)
  67.     python "$pyS" setr "$title" "$1"
  68. }
  69.  
  70. function set_status(){
  71.     title=$(get_ans)
  72.     python "$pyS" sets "$title" "$1"
  73.     echo "$title: $1"
  74. }
  75. function delete_it(){
  76.     title=$(get_ans)
  77.     python "$pyS" rm "$title"
  78.     rm "$dir/$title"
  79. }
  80.  
  81. function watch_f(){
  82.     code=$(ping -c 1 8.8.8.8 2>&1 |grep unreachable >/dev/null; echo $? )
  83.     today=$(date '+%Y-%m-%d %H:%M:%S' )
  84.     n='^[\.0-9]*$'
  85.     while IFS="|" read -r id title elink etitle edate episode adate entitle aa; do
  86.         [[ "$edate" == "None" ]] && edate=$( date -d '-1 day' '+%Y-%m-%d %H:%M:%S' )
  87.         [[ "$adate" == "None" ]] && adate=$( date -d '-1 day' '+%Y-%m-%d %H:%M:%S' )
  88.         a=$(date -d "$edate" '+%s' )
  89.         b=$(date -d "$today" '+%s' )
  90.         d=$(date -d "$adate" '+%s' )
  91.         c=$((b-a))
  92.         color='\033[1;0m'
  93.         if (( $(($b-$d)) > 100 )) && (( code != 0 )) ; then
  94.             #update access date
  95.             python "$pyS" sad "$title" "$today"
  96.             link=$(python "$pyS" url "$title")
  97.             data=$( CURL "$link" | sed -E -n '/<div class="cat-eps">/{n;p;q}' )
  98.             nlink=$(echo "$data" | sed -E 's/^.*href="(.*)" rel=.*$/\1/' )
  99.             ntitle=$(echo "$data" | sed -E 's/^.*title="(.*)" class=.*$/\1/' \
  100.                 | perl -C -MHTML::Entities -pe 'decode_entities($_);' \
  101.                 | sed 's/[^[:print:]]//g;s/Watch//' \
  102.             )
  103.             nepisode=$(echo "$ntitle" |
  104.                 sed -E 's/^.* Episode ([\.0-9]*) .*$/\1/g'
  105.             )
  106.             if [[ -z "$nepisode" ]] ; then nepisode="0" ; fi
  107.             if ! [[ "$nepisode" =~ $n ]] ; then nepisode="0" ; fi
  108.             if (( $(echo "$nepisode > $episode" |bc -l) )) ; then
  109.                     python "$pyS" wadd "$title" "$nlink" \
  110.                            "$ntitle" "$today" "$nepisode"
  111.                     color='\033[1;32m'
  112.                     etitle="$ntitle"
  113.                     episode="$nepisode"
  114.             fi
  115.         fi
  116.         if [[ "$entitle" != "None" ]] && [[ "$1" != o ]]
  117.             then
  118.                 etitle=" $entitle"
  119.             else
  120.                 etitle=$(echo "$etitle" |
  121.                     sed -E 's/^(.*)Episode ([0-9]*) (.*$)/\1\3/g'
  122.                 )
  123.         fi
  124.         (( ${#etitle} > 70 )) && etitle="${etitle:0:70}..."
  125.         D="$(echo "$c / 3600 / 24 " |bc )"
  126.         c="$(echo "$c - ( $D * 3600 * 24 ) " |bc )"
  127.        H="$(echo "$c / 3600 " |bc )"
  128.        c="$(echo "$c - ( $H * 3600 ) " |bc )"
  129.        M="$(echo "$c / 60 " |bc )"
  130.        if [[ "$color" != '\033[1;32m' ]] ; then
  131.            if (( $D < 10 )) ; then color='\033[1;33m' ; fi
  132.            if (( $D == 0 )) ; then color='\033[1;36m' ; fi
  133.        fi
  134.        printf "$color"
  135.        printf  '%4d d %2d h %2d m, Episode ' "$D" "$H" "$M"
  136.        printf '\033[1;31m%.1f' "$episode" |sed 's/\.0//'
  137.        printf "$color$etitle\n"
  138.    done <<< "$(python "$pyS" wl)"
  139. }
  140.  
  141. function add_it(){
  142.    url="$1"
  143.    url="${url//\\/}"
  144.    t=${url##*/}
  145.    title=$( echo "$t" \
  146.        |sed 's/-english-subbed-guide//' \
  147.        |sed 's/-english-dubbed-guide//' \
  148.        |sed 's/-english-subbed-episodes//' \
  149.        |sed 's/-english-dubbed-episodes//' \
  150.        |sed 's/-english-subbed//' \
  151.        |sed 's/-english-dubbed//'
  152.    )
  153.    python "$pyS" add "$url" "$title"
  154. }
  155.  
  156. function set_entitle(){
  157.    title=$(get_ans)
  158.    python "$pyS" seten "$title" "$1"
  159.  
  160. }
  161.  
  162.  
  163. function help_f(){
  164. echo '
  165.    l : list them
  166.    t : get info
  167.    w : watch current episodes
  168.    r : set rank
  169.    x : delete it
  170.    s : set status: ongoing, finished
  171.    a : add
  172.    o : list ongoing
  173.    f : list finished
  174.    e : set engilsh title
  175.    h : help
  176.  
  177. '
  178. }
  179. case "$1" in
  180.    l)  list_them ;;
  181.    t)  getInfo ;;
  182.    w)  watch_f  ;;
  183.    wo)  watch_f o ;;
  184.    r)  set_rank "$2" ;;
  185.    x)  delete_it ;;
  186.    s)  set_status "$2" ;;
  187.    a)  add_it "$2" ;;
  188.    o) list_them |grep ongoing ;;
  189.    f) list_them |grep finished ;;
  190.    h) help_f ;;
  191.    e) set_entitle  "$2" ;;
  192.    *) python "$pyS" url "$(get_ans)"
  193. esac
Add Comment
Please, Sign In to add comment