        __  _                        __ _                           _  __
  ______\ \_\\_______________________\///__________________________//_/ /______
  \___\                                                                   /___/
   | .__                                 __                                  |
   | |                   ___  __________/  |________                         |
   |                     \  \/  / ____/\   __\_  __ \                        |
   ;                      >    < <_|  | |  |  |  | \/                        ;
   :                     /__/\_ \__   | |__|  |__|                           :
   .                           \/  |__|                                      .
   .                                                                         .
   :           H/Q Another Droid BBS - andr01d.zapto.org:9999                :
   ;                                                                         ;
   + --- --  -   .     -        ---    ---    ---        -     .    - -- --- +
   :                                                                         :
   |                             torrentz part ii                            |
   :                                                                         :
   ` --- --  -   .     -        ---    ---    ---        -     .    - -- --- '
  
    In the previous issue i posted a script for downloading torrent files 
    to a BBS. I made some additions to that so, i am posting it here to be 
    available for others too.
  
    The new stuff is that we delete torrent files that are older than a 
    specific amount of days ex: 7. This way, we only keep "fresh" 
    torrents, which have more seeders, as we only get the top100 of them.
    
    Also we add some info for the torrent, like seeders, leechers and file 
    size. To accomplish that, i used this trick. I placed each torrent 
    file into an archive and created a file_id.diz that contains the info. 
    This way, in any bbs system, the torrent will display also the 
    file_id.diz and the user will have some idea of the original file 
    size, seeders etc.
    
    We could also add the description of the torrent, from TPB, but i 
    tried with mixed results. The site uses UTF8 encoding, but many BBSes 
    use CP437. A conversion from UTF8 to CP437 is not always succesfull, 
    so i got some weird results, that made the bbs server to kick the 
    users who viewed those files. If you use a UTF8 system, perhaps it 
    will work for you. 

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=    
#!/bin/bash

#Use a valid address. Check https://piratebay-proxylist.se/ for new ones.
prt="https://unblocktheship.org"

#Check https://unblocktheship.org/top to get area numbers
areas=$'201\n205\n101\n301\n306'

#Edit this to your torrents root folder.
root="/home/$(whoami)/mystic/files/torrentz"

echo "Deleting Files older than 7 days..."
find /home/pi/mystic/files/torrentz/ -type f -mtime +7 -name '*.zip' \
-execdir rm -- '{}' \;

while read -r area; do
    mkdir -p $root/$area
    sleep 2s
    echo "Getting Area: $area"
    curl -s $prt/top/$area > /tmp/tmp.htm
    
    #iconv -f utf8 -t cp437 /tmp/tmp.htm > /tmp/tmp2.htm
    #rm /tmp/tmp.htm
    #mv /tmp/tmp2.htm /tmp/tmp.htm
    
    page="$(cat /tmp/tmp.htm)"
    links="$(cat /tmp/tmp.htm | grep "<div class=\"detName\">" | sed \
    's/^<.*<a href="//g' | sed 's/" class=.*>$//g' | tail -n100)"
    
    while read -r line; do
        #title="$(echo $line | cut -d '/' -f 4)"
        title="$(echo $line | cut -d '/' -f 4 | sed 's/%5//g' | sed 's/\./_/g' \
        | sed 's/)//g' | sed 's/(/_/g' | sed 's/__/_/g')"
        
        echo "Checking: $title"
        if [ ! -f "$root/$area/$title.zip" ]; then
          echo "Getting page: $title"
          curl -s $prt$line > /tmp/tor.htm
          #iconv -f utf8 -t cp437 /tmp/tor.htm > /tmp/torr.htm
          #rm /tmp/tor.htm
          #mv /tmp/torr.htm /tmp/tor.htm
          cat /tmp/tor.htm | grep "href\=\"magnet" -m 1 | sed -e \
          's/.*href\=\"\(.*\)" title.*/\1/' > "/tmp/$title.magnet"
          
          #size
          tsize="$(cat /tmp/tor.htm | grep "<dt>Size:</dt>" -A 1 | tail -n1 \
          | sed 's/.*<dd>\(.*\).*<\/dd>/\1/')"
          #files
          tfiles="$(cat /tmp/tor.htm | grep "<dt>Files:</dt>" -A 1 | tail -n1 \
          | sed 's/.*false;">\(.*\).*<\/dd>/\1/' | cut -d "<" -f1)"
          #uploaded
          tuploaded="$(cat /tmp/tor.htm | grep "<dt>Uploaded:</dt>" -A 1 | \
          tail -n1 | sed 's/.*<dd>\(.*\).*<\/dd>/\1/')"
          #seeders/leechers
          tseed="$(cat /tmp/tor.htm | grep "<dt>Seeders:</dt>" -A 1 | tail \
          -n1 | sed 's/.*<dd>\(.*\).*<\/dd>/\1/')"
          tleech="$(cat /tmp/tor.htm | grep "<dt>Leechers:</dt>" -A 1 | tail \
          -n1 | sed 's/.*<dd>\(.*\).*<\/dd>/\1/')"
          
          echo "Files    : $tfiles " > /tmp/file_id.diz
          echo "Size     : $tsize" >> /tmp/file_id.diz
          echo "Uploaded : $tuploaded" >> /tmp/file_id.diz
          echo "Seeders  : $tseed" >> /tmp/file_id.diz
          echo "Leechers : $tleech" >> /tmp/file_id.diz
          echo "-------------------------------" >> /tmp/file_id.diz
          
          zip -j "$root/$area/$title.zip" /tmp/*.magnet /tmp/file_id.diz
          sleep 3s
          rm /tmp/file_id.diz
          rm /tmp/*.magnet
        fi
    done <<< "$links"
done <<< "$areas"

cd "/home/$(whoami)/mystic/"
./mutil massupload.ini
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=    
 

   + --- --  -   .     -        ---    ---    ---        -     .    - -- --- '
         _____         _   _              ____          _   _ 
        |  _  |___ ___| |_| |_ ___ ___   |    \ ___ ___|_|_| |        8888
        |     |   | . |  _|   | -_|  _|  |  |  |  _| . | | . |     8 888888 8
        |__|__|_|_|___|_| |_|_|___|_|    |____/|_| |___|_|___|     8888888888
                                                                   8888888888
                DoNt Be aNoTHeR DrOiD fOR tHe SySteM               88 8888 88
                                                                   8888888888
 /: HaM RaDiO   /: ANSi ARt!     /: MySTiC MoDS   /: DooRS         '88||||88'
 /: NeWS        /: WeATheR       /: FiLEs         /: SPooKNet       ''8888"'
 /: GaMeS       /: TeXtFiLeS     /: PrEPardNeSS   /: FsxNet            88
 /: TuTors      /: bOOkS/PdFs    /: SuRVaViLiSM   /: ArakNet    8 8 88888888888
                                                              888 8888][][][888
   TeLNeT : andr01d.zapto.org:9999 [UTC 11:00 - 20:00]          8 888888##88888
   SySoP  : xqtr                   eMAiL: xqtr@gmx.com          8 8888.####.888
   DoNaTe : https://paypal.me/xqtr                              8 8888##88##888
