Monday, November 22, 2010

Download Fileserve links from shell

The script assumes, you have
  • ImageMagick
  • Curl
  • Common Unix utilities (eg. sed, awk, cut etc.)
... are installed in the path. The captcha is shown using 'display' component from ImageMagick. You need X forwarding if you are running the script over ssh.


#!/bin/bash
#source /home/babil/Desktop/recaptcha/plowshare-0.9.4/src/lib.sh

PUB_URL='http://www.fileserve.com/file/zXvrJS2'
FILESERVE_RECAPTCHA_PUBKEY='6LdSvrkSAAAAAOIwNj-IY-Q-p90hQrLinRIpZBPi'
IMAGE_VIEWER="display"

CAPTCHA_IMAGE=$(mktemp) #'captcha.jpg'
COOKIE=$(mktemp) #'tmp_cookie.txt'
HTML=$(mktemp) #'tmp_fileserve.htm'

AGENT='Mozilla/5.0 (X11; U; Linux x86_64; en-AU; rv:1.9.2.12) Gecko/20101027 Ubuntu/10.10 (maverick) Firefox/3.6.12'
CUSTOM_HEADERS='-H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" -H "Accept-Language: en-us,en;q=0.5" -H "Accept-Encoding: gzip,deflate" -H "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7" -H "Keep-Alive: 115" -H "Connection: keep-alive"'
XML_HTTP_REQUEST='-H "X-Requested-With: XMLHTTPRequested"'

RED='\e[0;31m'
CYAN='\e[0;36m'
NC='\e[0m' # No Color


function regex_replace() {
RE='s/\(\/\|\\\|&\)/\\&/g'
FROM=$(sed -e "$RE" <<< "$1")
TO=$(sed -e "$RE" <<< "$2")
sed -e "s/$FROM/$TO/g"
}

# Delete leading and trailing spaces, tabs, \r, ...
function strip_whiteSpace() {
echo "$1" | sed "s/^[[:space:]]*//; s/[[:space:]]*$//"
}

# Return uppercase string
function uppercase() {
tr '[a-z]' '[A-Z]'
}

function clean_up()
{
rm -f $COOKIE
rm -f $CAPTCHA_IMAGE
rm -f $HTML
}

function find_fileName()
{

if [ -z $FILE_NAME ]
then
FILE_NAME=$(cat $HTML | grep 'Forum (BBCode)' | sed -n 's/.*File name: \(.*\) File size.*/\1/p')
fi

if [ -z $FILE_NAME ]
then
FILE_NAME=$(cat $HTML | grep 'Forum (BBCode)' | sed -n 's/.*url=\(.*\)\]\[b\].*/\1/p')
fi

if [ -z $FILE_NAME ]
then
FILE_NAME=$(cat $HTML | grep 'Website (HTML)' | sed -n 's/.*href=.\(.*\).>.b.File name.*/\1/p')
fi

if [ -z $FILE_NAME ]
then
FILE_NAME=$(cat $HTML | grep 'Text link' | sed -n 's/.*value="\(.*\)".*/\1/p')
fi

FILE_NAME=$(strip_whiteSpace $FILE_NAME)

}

function delay()
{
for((i=$1;i>=1;i--))
do
echo -n "$i "
sleep 1s
done
echo
}

function check_waitTime()
{
WAIT_TIME=$(cat $HTML | sed -n 's/.*You need to wait \(.*\) seconds to start another download.*/\1/p')
if [ -z $WAIT_TIME ]
then
WAIT_TIME=3
fi
WAIT_TIME=$(strip_whiteSpace $WAIT_TIME)
echo $WAIT_TIME
}

function solve_recaptcha()
{
CAPTCHA_URL='http://www.google.com/recaptcha/api/challenge?k='$FILESERVE_RECAPTCHA_PUBKEY
echo -e "$RED [*] captcha url: $NC $CAPTCHA_URL "

CHALLENGE=$(wget -q --referer $PUB_URL "$CAPTCHA_URL" -O - | grep challenge | sed -n "s/.*'\(.*\)',/\1/p")
echo -e "$RED [*] captcha challenge: $NC $CHALLENGE"
wget -q --referer $PUB_URL http://www.google.com/recaptcha/api/image?c=$CHALLENGE -O $CAPTCHA_IMAGE

#convert -trim $CAPTCHA_IMAGE $CAPTCHA_IMAGE
#$IMAGE_VIEWER "$CAPTCHA_IMAGE" &
#pid=$!
#read -p "[*] enter captcha response: " RESPONSE
#RESPONSE=$(echo $RESPONSE | sed 's/ /+/g')
#disown $(kill -9 $pid) 2>&1 1>/dev/null

echo -e -n "$RED [*] captcha input : $NC"
RESPONSE=$(prompt_captcha.py $CAPTCHA_IMAGE | sed 's/ /+/g')
echo "$RESPONSE"

RESULT=$(curl -s --user-agent "$AGENT" --cookie-jar $COOKIE --referer $PUB_URL http://www.fileserve.com/checkReCaptcha.php --data "recaptcha_challenge_field=$CHALLENGE&recaptcha_response_field=$RESPONSE&recaptcha_shortencode_field=$SHORTENCODE")
}


### - 0
if [ -z "$1" ]
then
echo
echo "[*] usage: $(basename $0) $PUB_URL"
echo
exit -1
else
PUB_URL="$(strip_whiteSpace $1 | sed 's#//fileserve.com#//www.fileserve.com#g')"
fi
echo -e "$RED [*] attempting to download: $CYAN $PUB_URL $NC"


### - 1
clean_up

### - 2
SHORTENCODE=$(echo $PUB_URL | sed -n 's/.*file\/\(.*\)/\1/p' | cut -d '/' -f 1)
SHORTENCODE=$(strip_whiteSpace $SHORTENCODE)

### - 3 save cookie
curl -s --user-agent "$AGENT" --cookie-jar $COOKIE --referer $PUB_URL $PUB_URL --data 'checkTimeLimit=check' --output $HTML

### - 4 immitating "slow download".click()
curl -s --user-agent "$AGENT" --cookie $COOKIE --referer $PUB_URL $PUB_URL --data "recaptcha_shortencode_field=$SHORTENCODE" --output $HTML

### - 5
if [ -z "$2" ]
then
FILE_NAME="$(strip_whiteSpace $2)"
else
FILE_NAME=""
fi
find_fileName
if [ -z $FILE_NAME ]
then
echo "[*] can not find file-name, exiting ..."
exit -1
else
echo -e "$RED [*] file-name found: $NC $FILE_NAME"
fi

### - 6 solve captcha
RESULT="(null)"
while [ $RESULT != "success" ]
do
solve_recaptcha
RESULT=$(echo $RESULT | sed -n 's/{.*"\(.*\)".*/\1/p')
done
echo -e "$RED [*] captcha solved :)$NC"

### - 7 free-user delay
WAIT_TIME=$(curl -s --user-agent "$AGENT" --cookie $COOKIE --referer $PUB_URL $PUB_URL --data "downloadLink=wait" --output -)
echo -e "$RED [*] free-user delay: $NC $WAIT_TIME"
WAIT_TIME=$(echo $WAIT_TIME | sed 's/[^0-9]//g' | bc)
delay $WAIT_TIME
$(curl -s --user-agent "$AGENT" --cookie $COOKIE --referer $PUB_URL $PUB_URL --data "downloadLink=show" --output /dev/null)


### - 8 final download ... should give a 302 redirection
echo -e "$RED [*] Starting download ...$NC"
REDIR=$(curl --user-agent "$AGENT" --cookie $COOKIE --referer $PUB_URL $PUB_URL --data "download=normal" --location --location-trusted --output $FILE_NAME --write-out 'REDIRECT-URL:[%{url_effective}]' | sed -n 's/.*:\[\(.*\)\]/\1/p')

if [[ ! "$REDIR" =~ "/dl/" ]]
then
echo -e "$RED [*] incorrect redirection; delay then restart ...$NC"
mv -f $FILE_NAME $HTML
WAIT_TIME=$(check_waitTime)
delay $WAIT_TIME
$0 $PUB_URL
else
echo -e "$RED [*] done :D. $NC"
clean_up
exit
fi

Tuesday, October 19, 2010

IDLE not working in ActivePython

The problem is that "idle.bat" is missing some double-quotes. If you have installed Python in "C:\Programs files\Python27" instead of "C:\Python27\", "idle.bat" will fail to execute "idle.pyw".

Copy and paste the follow 2 lines in "C:\Program Files\Python27\Lib\idlelib\idle.bat" (or wherever "idle.bat" is located) to fix it:


@echo off
rem Start IDLE using the appropriate Python interpreter
set CURRDIR=%~dp0
start "%CURRDIR%..\..\pythonw.exe" "%CURRDIR%idle.pyw" %1 %2 %3 %4 %5 %6 %7 %8 %9

Monday, October 18, 2010

"Unlocker" not working on Windows 7 ?? Try "LockHunter"

Unlocker by http://ccollomb.free.fr/unlocker/ was a nice tool to find, unlock, delete files when Windows explorer can't delete them (as they are locked by other processes).

After my upgrade to Windows 7 - 64bit, Unlocker tool stopped working. Even the 64bit from author's official site doesn't work at all (no window shows up after running the application).

So, for whatever it is worth and whoever is looking for an alternative, LockHunder by http://lockhunter.com is a great alternative, works much like Unlocker and is working without any hiccup (so far) on my Windows 7.



Thursday, October 14, 2010

MikTex 2.8 Downloading/Missing "ifxetex.sty" On Every Run

I debugged this problem after seeing my LyX on my Windows 7 machine taking so long to finish. I saw "latex" process was taking too long to finish. The I ran "latex" manually and the problem was obvious - "ifxetex" is being downloaded in every run.

This seems to be a bug in MikTex 2.8. The bug report can be found here: http://sourceforge.net/tracker/?func=detail&aid=3067362&group_id=10783&atid=110783

The solution is:
  1. Copy ...
                from: C:\Program Files (x86)\MikTeX 2.8\tex\generic\ifxetex\ifxetex.sy
                to: C:\Program Files (x86)\MikTeX 2.8\tex\latex\ifxetex\ifxetex.sty
  2. From Start-Menu > MikTex 2.8 > Maintenance (Admin), run "Update (Admin)" and update all pending update-able packages.
  3. Start-Menu > MikTex 2.8 > Maintenance (Admin), run "Settings (Admin)". and Then from "General" tab, click "Refresh FNDB" and "Update Formats"
MikTex should not download "ifxetex.sty" in every run now ;-).

Friday, October 8, 2010

Post to PasteBin from Command-Line

Full credit for "urlencode" script goes to the author. 
I modified original "urlencode" script slightly to meet my 
needs. Original script can be downloaded at:  
http://www.shelldorado.com/scripts/cmds/urlencode
 
============
pastebin.sh
============


 
#!/bin/bash
#

name="your_name"
format="text"
expire_date="N"
private="0"


if [ -z $1 ]; then
 echo
 echo "[*] usage:  `basename $0`  filename"
 echo
 exit
fi


#DATA=`cat "$@" | uuencode -m -`
DATA=`cat "$@" | urlencode.sh -l`

curl -s -d paste_code="$DATA" -d paste_name="$name" -d paste_format="$format" -d
paste_expire_date="$expire_date" -d paste_private=$private --include http:
//pastebin.com/api_public.php | grep 'http://pastebin.com/' 
 


============
urlencode.sh
============



##########################################################################
# Title      : urlencode - encode URL data
# Author     : Heiner Steven (heiner.steven@odn.de)
# Date       : 2000-03-15
# Requires   : awk
# Categories : File Conversion, WWW, CGI
# SCCS-Id.   : @(#) urlencode 1.4 06/10/29
##########################################################################

PN=`basename "$0"`   # Program name
VER='1.4'

: ${AWK=awk}

Usage () {
    echo >&2 "$PN - encode URL data, $VER
usage: $PN [-l] [file ...]
    -l:  encode line endings (result will be one line of output)

The default is to encode each input line on its own."
    exit 1
}

Msg () {
    for MsgLine
    do echo "$PN: $MsgLine" >&2
    done
}

Fatal () { Msg "$@"; exit 1; }

set -- `getopt hl "$@" 2>/dev/null` || Usage
[ $# -lt 1 ] && Usage   # "getopt" detected an error

EncodeEOL=no
while [ $# -gt 0 ]
do
    case "$1" in
     -l) EncodeEOL=yes;;
 --) shift; break;;
 -h) Usage;;
 -*) Usage;;
 *) break;;   # First file name
    esac
    shift
done

LANG=C export LANG
$AWK '
    BEGIN {
 # We assume an awk implementation that is just plain dumb.
 # We will convert an character to its ASCII value with the
 # table ord[], and produce two-digit hexadecimal output
 # without the printf("%02X") feature.

 EOL = "%0D%0A"  # "end of line" string (encoded)
 split ("1 2 3 4 5 6 7 8 9 A B C D E F", hextab, " ")
 hextab [0] = 0
 for ( i=1; i<=255; ++i ) ord [ sprintf ("%c", i) "" ] = i + 0
 if ("'"$EncodeEOL"'" == "yes") EncodeEOL = 1; else EncodeEOL = 0
    }
    {
 encoded = ""
 for ( i=1; i<=length ($0); ++i ) {
     c = substr ($0, i, 1)
     if ( c ~ /[a-zA-Z0-9.-]/ ) {
  encoded = encoded c  # safe character
     } else if ( c == " " ) {
  encoded = encoded "+" # special handling
     } else {
  # unsafe character, encode it as a two-digit hex-number
  lo = ord [c] % 16
  hi = int (ord [c] / 16);
  encoded = encoded "%" hextab [hi] hextab [lo]
     }
 }
 if ( EncodeEOL ) {
     printf ("%s", encoded EOL)
 } else {
     print encoded
 }
    }
    END {
     #if ( EncodeEOL ) print ""
    }
' $@