#!/bin/bash
######################################################################
#Copyright (C) 2020 Kris Occhipinti
#https://filmsbykris.com
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program. If not, see .
######################################################################
csv="/tmp/torrent.csv"
url="https://gitlab.com/dessalines/torrents.csv/-/raw/master/torrents.csv"
open="xdg-open"
red=`echo -en "\e[31m"`
normal=`echo -en "\e[0m"`
#check dependences
for d in aria2c fzf
do
[[ ! -f "/usr/bin/$d" ]] &&
echo -e "${red}$d${normal} is needed\nplease install it\nsudo apt install ${red}$d${normal}" &&
sleep 3 &&
continue="$(echo -e "Continue\nExit"|fzf --prompt="Continue without $d: ")"
[[ "$continue" == "Exit" ]] && exit 1
done
function main(){
[[ ! -f "$csv" || "$1" == "update" ]] && getList
[[ "$1" == "sites" ]] && (sites;exit)
[[ "$1" == "" ]] && search
}
function getList(){
aria2c -x 10 "$url" -o "$csv"
# wget "$url" -O-|while read line;do
# size_bytes=$(echo -e "$line" | cut -d ';' -f3)
# size=$(numfmt --to=iec-i --suffix=B $size_bytes)
# echo "$line"|cut -d\; -f1,2|tr -d "\n"
# echo ";$size"
# done > "$csv"
}
function search(){
item="$(cat "$csv"|awk -F';' '{print $1 ";" $2 ";" $3 ";" $5}'|fzf)"
title="$(echo "$item"|cut -d\; -f2)"
magnet="magnet:?xt=urn:btih:$(echo "$item"|cut -d\; -f1)"
seeds=$(echo -e "$item" | cut -d ';' -f4)
size_bytes=$(echo -e "$item" | cut -d ';' -f3)
size=$(numfmt --to=iec-i --suffix=B $size_bytes)
a="$(echo -e "Download\nCancel"|fzf --prompt "Download $title [$size - SEEDS: $seeds]?")"
echo -e "=======================\n$title\n$size\n$magnet\n======================="
[[ "$a" == "Download" ]] && aria2c "$magnet" || (echo "Goodbye";exit)
}
function sites(){
read -p "Enter Search String: " q
$open "https://t.extto.com/search/?q=$q"
read -p "Enter Torrent/Magnet Link: " magnet
[[ "$magnet" == "" ]] && echo "Good-bye" || aria2c --seed-time=0 "$magnet"
}
main $*