#!/bin/bash
# first known torrent
first=15
# last known torrent
last=`wget "http://www.nyaa.eu/?page=rss" -O - | awk -F "tid=" '{print $2}' | awk -F "<" '{print $1}' | head -n1`
i=$first
mkdir -p "data"
while [ $i -le $last ] 
do  
	wget -T 10 --retry-connrefused --random-wait -U "Mozilla/5.0 (X11; Linux x86_64; rv:10.0.2) Gecko/20100101 Firefox/13" -O "work.html" "http://www.nyaa.eu/?page=torrentinfo&tid=$i"
	grep "does not appear to be in our database" "work.html" -q && rm "work.html"
	if [ -f "work.html" ]
	then
		echo $i>>id.txt
		wget -T 10 --retry-connrefused --random-wait -U "Mozilla/5.0 (X11; Linux x86_64; rv:10.0.2) Gecko/20100101 Firefox/13" -O "work.torrent" "http://www.nyaa.eu/?page=download&tid=$i"
		id=$i
		category=`cat work.html | awk -F "<td" '{print $2}' | awk -F ">" '{print $5}' | awk -F "<" '{print $1}' | sed '/^$/d' | head -n1`
		adddate=`cat work.html | awk -F "<td" '{print $7}' | awk -F ">" '{print $2}' | awk -F "<" '{print $1}' | sed '/^$/d' | sed 's/,//' | awk -F " " '{print $1" "$2", "$3" "$4}' | head -n1`
		adddate_unix=`date -ud "$adddate" +'%s'`
		submitter=`cat work.html | awk -F "<td" '{print $9}' | awk -F ">" '{print $4}' | awk -F "<" '{print $1}' | sed '/^$/d' | head -n1 | sed -f urlencode.sed`
		seed=`cat work.html | awk -F "<td" '{print $11}' | awk -F ">" '{print $3}' | awk -F "<" '{print $1}' | sed '/^$/d'`
		leech=`cat work.html | awk -F "<td" '{print $15}' | awk -F ">" '{print $3}' | awk -F "<" '{print $1}' | sed '/^$/d'`
		name=`cat work.html | awk -F "<td" '{print $5}' | awk -F ">" '{print $2}' | awk -F "<" '{print $1}' | sed '/^$/d' | head -n1 | sed -f urlencode.sed`
		downloads=`cat work.html | awk -F "<td" '{print $19}' | awk -F ">" '{print $3}' | awk -F "<" '{print $1}' | sed '/^$/d' | head -n1`
		torrentname=`timeout 30 php -r 'require_once("Torrent.php");$torrent = new Torrent("work.torrent");echo $torrent->name();' | sed -f urlencode.sed`; if [ $torrentname ="" ]; then torrentname="(torrent error)";fi
		torrentsize=`timeout 30 php -r 'require_once("Torrent.php");$torrent = new Torrent("work.torrent");echo $torrent->size();'`; if [ $torrentsize ="" ]; then torrentsize="0";fi
		btih=`timeout 30 php -r 'require_once("Torrent.php");$torrent = new Torrent("work.torrent");echo $torrent->hash_info();'`; if [ $btih ="" ]; then btih="(torrent error)";fi
		update=`date +'%s'`
		dbfile=`c=$(($i/1000));echo $c`
			if ! [[ "$seed" =~ ^[0-9]+$ ]];then seed="?";fi
			if ! [[ "$leech" =~ ^[0-9]+$ ]];then leech="?";fi
		sed -i '$ d' "data/info$dbfile.php" #remove last line
		tail -n +2 "data/info$dbfile.php" > "data/_info$dbfile.php"; mv "data/_info$dbfile.php" "data/info$dbfile.php" #remove first line
		echo "\$tid$id = array('name'=>'$name','tname'=>'$torrentname','tsize'=>'$torrentsize','btih'=>'$btih','cat'=>'$category','add'=>'$adddate_unix','sub'=>'$submitter');">>"data/info$dbfile.php"
		sed -i '1i <?php ' "data/info$dbfile.php" #prepend line
		echo " ?>">>"data/info$dbfile.php"
		sed -i '$ d' "data/stat$dbfile.php" #remove last line
		tail -n +2 "data/stat$dbfile.php" > "data/_stat$dbfile.php"; mv "data/_stat$dbfile.php" "data/stat$dbfile.php" #remove first line
		echo "\$tidstat$id = array('se'=>'$seed','le'=>'$leech','dl'=>'$downloads','up'=>'$update');">>"data/stat$dbfile.php"
		sed -i '1i <?php ' "data/stat$dbfile.php" #prepend line
		echo " ?>">>"data/stat$dbfile.php"	
		rm "work.html" "work.torrent"
	fi
let i=1+$i
done
echo "Database built"
