SHARE
TWEET

Untitled

a guest Mar 25th, 2014 250 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. ua='Mozilla/5.0 (Windows NT 5.2; rv:2.0.1) Gecko/20100101 Firefox/4.0.1'
  4. login='RUTRACKER_LOGIN'
  5. password='RUTRACKER_PASSWORD'
  6. query="$*"
  7. limit=5
  8. downlink='http://dl.rutracker.org/forum/dl.php?t='
  9. referer='http://rutracker.org/forum/viewtopic.php?t='
  10.  
  11. write() {
  12.         echo -e "\e[1;36m[`date +%k:%M:%S`]\e[00m \e[1;37m$1 \e[00m"
  13. }
  14.  
  15. ask() {
  16.         echo -ne "\e[1;36m[`date +%k:%M:%S`]\e[00m \e[1;33m$1 \e[00m"
  17. }
  18.  
  19. die() {
  20.         echo -e "\e[1;36m[`date +%k:%M:%S`]\e[00m \e[1;31m$1 \e[00m"
  21.         exit
  22. }
  23.  
  24. startup() {
  25.         rm /tmp/rutracker* 2>/dev/null
  26.         which phantomjs >/dev/null && return 0 || return 1
  27. }
  28.  
  29. auth() {
  30.         wget --user-agent="$ua" -qO /tmp/rutracker_index "http://login.rutracker.org/forum/login.php" --post-data="login_username=$login&login_password=$password&redirect=index.php&login=Вход" --save-cookies=/tmp/rutracker_cookies --keep-session-cookies
  31.         grep -q "med\">$login" /tmp/rutracker_index && return 0 || return 1
  32. }
  33.  
  34. urlencode() {
  35.         echo "$(perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "$*")"
  36. }
  37.  
  38. phantomjs_init() {
  39.         echo "
  40.                 var login = '$login';
  41.                 var password = '$password';
  42.                 var query = '$(urlencode "$query")';
  43.                 var page = require('webpage').create();
  44.                 var querypage = require('webpage').create();
  45.  
  46.                 var loginurl = 'http://login.rutracker.org/forum/login.php';
  47.                 var loginpost = 'login_username='+login+'&login_password='+password+'&login=Вход';
  48.  
  49.                 var queryurl = 'http://rutracker.org/forum/tracker.php?nm='+query;
  50.                 var querypost = 'prev_my=0&prev_new=0&prev_oop=0&f[]=-1&o=10&s=2';
  51.  
  52.                 function retr_query() {
  53.  
  54.                   querypage.open(queryurl, 'post', querypost, function (status) {
  55.                   if (status === 'success') {
  56.                         console.log(querypage.content);
  57.                   } else {
  58.                         console.log('Error');
  59.                           }      
  60.                   phantom.exit();
  61.                   });
  62.                 }
  63.  
  64.                 page.open(loginurl, 'post', loginpost, function (status) {
  65.                 if (status === 'success') {
  66.                         retr_query();
  67.                 } else {
  68.                         console.log('Error');
  69.                         phantom.exit();
  70.         }
  71.                 });
  72.  
  73.         " > /tmp/rutracker_parse.js
  74. }
  75.  
  76. phantomjs_query() {
  77.         phantomjs /tmp/rutracker_parse.js > /tmp/rutracker_index
  78.         [ "$(cat /tmp/rutracker_index)" == "Error" ] && return 1
  79.         # count results
  80.         export count=$(grep Результатов\ поиска /tmp/rutracker_index|sed 's/<[^>]\+>//g'|awk '{ print $3 }')
  81.         [ $count -eq 0 ] && return 1 || return 0
  82. }
  83.  
  84. parse() {
  85.         OLD_IFS=$IFS
  86.         IFS=$'\n'
  87.         topics=(` grep viewtopic.php?t= /tmp/rutracker_index | grep tLink | awk '{ print $2 }' | grep -oE '[0-9]{1,15}' `)
  88.         names=(` grep viewtopic.php?t= /tmp/rutracker_index | grep tLink | sed 's/<[^>]\+>//g' | sed 's/\t//g' `)
  89.  
  90.         [ ${#topics[@]} -ge $limit ] && max=$limit || max=${#topics[@]}
  91.         for i in `seq 0 $(($max-1))`; do
  92.                 echo -e "   \e[1;35m*$i*\e[00m \e[1;32m${names[$i]}\e[00m"
  93.         done
  94. }
  95.  
  96. get_torrent() {
  97.         wget -qO "/tmp/rutracker_torrent.torrent" "${downlink}${topics[$target]}"  --load-cookies=/tmp/rutracker_cookies --keep-session-cookies --post-data='dummy=' --referer="${referer}${topics[$target]}"
  98. }
  99.  
  100. cleanup() {
  101.         rm /tmp/rutracker*
  102.         exit 0
  103. }
  104.  
  105. write "Welcome to ruTracker CLI Client v0.01 by annelin"
  106. startup || die "Can't find phantomjs installed."
  107. auth || die "Incorrect login/password"
  108. write "Logged in as $login."
  109. phantomjs_init
  110. phantomjs_query || die "Can't process request to tracker or no results found"
  111. write "Found $count results. Displaying first $limit."
  112. parse
  113. ask "Select torrent number... "
  114. read target
  115. write "Downloading ${names[$target]}"
  116. get_torrent
  117. xdg-open /tmp/rutracker_torrent.torrent
  118. # cleanup
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top