Image downloader

By TheImrac on Oct 28, 2008

Version: 1.0.2

Load it up and double click on a image link =)

Thanks: neptune BrAndo|

This is a rough version, it works, but it ain't too pretty as of now.

1.0.1: Updated, fixed aspect ratio crap
1.0.2: Rewrite of sockets/hot link/finish alias (not backwards compatible)

;Version: 1.0.2
;Author: Imrac
;thanks: neptune, BrAndo|

on ^*:hotlink:*:*:$iif($ValidImgLink($1),return,halt)

on *:hotlink:*:*:{ if ($ValidImgLink($1)) { getImage $regml(ValidImgLink,1) HotlinkImageLoaded HotlinkImageUpdate $+(tmp.,$regml(ValidImgLink,5)) } }

alias HotlinkImageLoaded {
  If (!$1) {
    Echo 2 -e Error Downloading File $2-
  }

  Else {
    if ($window(@loader)) window -c @Loader
    if ($window(@Image)) window -c @Image
    var %img.h = $pic($1-).height, %img.w = $pic($1-).width, %img.aspect = $calc(%img.w / %img.h)
    var %win.h = $round($calc($window(-3).h * .75),0), %win.w = $round($calc($window(-3).w * .75)),0)
    if ((%img.aspect >= 1) && (%img.w > %win.w)) { var %img.h = %win.h, %img.w = $round($calc(%img.h * %img.aspect),0) } 
    if ((%img.aspect < 1) && (%img.h > %win.h)) { var %img.w = %win.w, %img.h = $round($calc(%img.w * %img.aspect),0) } 

    echo -b Dimentions: %img.w %img.h
    window -dapBCw3 +d @Image 50 50 %img.w %img.h
    drawpic -s @Image 0 0 %img.w %img.h $1-
    toolbar -d loaderw
  }
}

alias HotlinkImageUpdate {
  If (!$window(@Loader)) window -hdpBCw0 +d @Loader 5 5 150 16
  drawgrad @loader #eeeef6 #c7c7dd 0 0 150 16
  drawgrad @loader #a4cf00 #a5d000 0 0 $round($calc(150 * $1 / $2),0) 8
  drawgrad @loader #95b901 #93ba00 0 8 $round($calc(150 * $1 / $2),0) 8
  var %d = $chr(2) $+ $round($calc($1 / $2 * 100),0) $+ %
  var %w = $width(%d,Arial,12), %x = $round($calc((150 - %w) / 2),0)
  drawtext -cnrp @loader $rgb(255,255,255) Arial 12 %x 0 %w 16 %d
  drawrect -r @loader $rgb(65,65,65) 1 0 0 150 16

  if (!$toolbar(loaderw)) { toolbar -az3 loaderw "Downloading Image" @loader }
  toolbar -p loaderw @loader
}

;alias HotlinkImageUpdate {
;  echo -b $chr(2) $+ $round($calc($1 / $2 * 100),0) $+ %
;}

alias drawgrad {
  if ($regex(draw,$1-,/^(@.[^ ]*) (#?[0-9a-fA-f]{6}) (#?[0-9a-fA-f]{6}) (\d+) (\d+) (\d+) (\d+) ?(1)?$/)) {
    tokenize 44 $+($h2r($regml(draw,2)),$chr(44),$h2r($regml(draw,3)))
    var %a = 0
    if ($regml(draw,8) == 1) {
      while (%a <= $regml(draw,6)) {
        var %r = $floor($calc($1 + (($4 - $1) * (%a / $v2)))), %g = $floor($calc($2 + (($5 - $2) * (%a / $v2)))), %b = $floor($calc($3 + (($6 - $3) * (%a / $v2))))
        var %x = $calc($regml(draw,4) + %a)
        drawrect -nrf $regml(draw,1) $rgb(%r,%g,%b) 0 %x $regml(draw,5) 1 $regml(draw,7)
        inc %a
      }
    }
    else {
      while (%a <= $regml(draw,7)) {
        var %r = $floor($calc($1 + (($4 - $1) * (%a / $v2)))), %g = $floor($calc($2 + (($5 - $2) * (%a / $v2)))), %b = $floor($calc($3 + (($6 - $3) * (%a / $v2))))
        var %y = $calc($regml(draw,5) + %a)
        drawrect -nrf $regml(draw,1) $rgb(%r,%g,%b) 0 $regml(draw,4) %y $regml(draw,6) 1
        inc %a
      }
    }
  }
  else { echo -e $color(info) * /drawgrad: insufficient parameters }
}

alias h2r {
  if ($regex($1,$+(/^#?,$str(([0-9a-fA-F]),6),$/))) {
    var %x = 1
    while (%x <= $regml(0)) {  var %c = $+(%c,$replace($regml(%x),a,10,b,11,c,12,d,13,e,14,f,15),$chr(44)) | inc %x }
    tokenize 44 %c
    return $+($calc($1 * 16 + $2),$chr(44),$calc($3 * 16 + $4),$chr(44),$calc($5 * 16 + $6))
  }
  return $false
}

menu @Image {
  mouse:{
    if (%Image.mx) {
      var %x = $calc(%Image.wx - (%Image.mx - $mouse.dx ))
      var %y = $calc(%Image.wy - (%Image.my - $mouse.dy))
      window -p @Image %x %y
    }
  }
  sclick:{
    set %Image.mx $mouse.dx
    set %Image.my $mouse.dy
    set %Image.wx $window(@Image).x
    set %Image.wy $window(@Image).y
  }
  uclick:{
    unset %Image.*
  }
  leave:{
    unset %Image.*
  }
  -
  Close:window -c @Image
  -
}

alias ValidImgLink { return $regex(ValidImgLink,$1,/((?:http:\/\/)?([\w.]+\.\w{2,3})((?:\/\S+)?(\/\S+\.(jpe?g|bmp|gif|png))))/i) } 
;; /getImage <URL> <alias> <alias> <File>
;; URL = Image URL
;; alias #1 = Call Back alias when finished Returns $false if failed or file name.
;; alias #2 = Update alias (can be NOOP) Returns <bytes> <total bytes>
alias getImage {
  IF (($ValidImgLink($1)) && ($0 > 3)) {
    var %sockname = $+(IMG_,$md5($regml(ValidImgLink,1)))

    IF ($sock(%sockname)) sockclose %sockname
    IF ($hget(%sockname)) .hfree %sockname
    hadd -m %sockname host $regml(ValidImgLink,2)
    hadd %sockname get $regml(ValidImgLink,3)
    hadd %sockname callback $2
    hadd %sockname update $3
    hadd %sockname file $qt($4-)
    if ($exists($qt($4-))) .remove $qt($4-)
    sockopen %sockname $regml(ValidImgLink,2) 80
  }
}

on *:SOCKOPEN:IMG_*:{
  IF ($sockerr) { $hget($sockname,callback) $hget($sockname,file) $false | halt }
  sockwrite -n $sockname GET $hget($sockname,get) HTTP/1.1
  sockwrite -n $sockname Host: $hget($sockname,host)
  sockwrite -n $sockname Connection: close
  sockwrite -n $sockname $crlf
}
on *:SOCKREAD:IMG_*:{
  IF ($sockerr) { $hget($sockname,callback) $hget($sockname,file) $false | halt }

  if (!$sock($sockname).mark) {
    var %read
    sockread %read
    while (%read) {
      tokenize 32 %read 
      IF (($1 == HTTP/1.1) && ($2 !isnum 200)) { 
        $hget($sockname,callback) $false 
        hfree $sockname
        sockclose $sockname
        halt 
      }
      ELSEIF ($1 == Content-Length:) hadd $sockname length $2
      sockread %read
    }
  }
  sockmark $sockname $true

  sockread &tmp
  while ($sockbr) {
    var %file = $hget($sockname,file)
    bwrite %file -1 -1 &tmp
    var %size = $file(%file).size
    var %length = $hget($sockname,length)
    $hget($sockname,update) %size %length

    if (%size == %length) { 
      $hget($sockname,callback) %file
      hfree $sockname
      sockclose $sockname
      halt
    }

    sockread &tmp
  }
}

Comments

Sign in to comment.
TheImrac   -  Oct 19, 2010

What version of mIRC are you using?

 Respond  
_Teen_   -  Oct 19, 2010

i tested again and then,

  • /hfree: Insuficient Parameters (line 259, remote_p.cyb)

$hget($sockname,callback) %file

  • /sockread: Invalid Socket (line 264, remote_p.cyb)

sockread &tmp

 Respond  
TheImrac   -  Oct 19, 2010

Aight, complete rewrite of sockets and hotlink catcher, should work nicely now.

May update later to accommodate redirects.

 Respond  
[85]   -  Oct 19, 2010

;;]

 Respond  
_Teen_   -  Oct 18, 2010

nice code dude, but look
when i try to download the pic from a different server

like

http://images.orkut.com/orkut/photos/OgAAAEnnWRcrKjI82dsKc5I_DjWPNzZHy0Ab4RE5aCMUI7B769puV1Q8Gv1qPqNk4d65WJ0eRBxJInzb02cqU7bG6ZYAm1T1UKqyk9GxYWz8yg2vxxo9O_ETVxh4.jpg

its fail... would be nice to add a function to receive from many other websities

and yes, i dont have copyrights about this pic

 Respond  
[85]   -  Oct 18, 2010

woow ... :D

 Respond  
Jace   -  Oct 29, 2008

Oh sorry Imrac I didn't didn't rate."Rate it? Or Hate it?"...7/10.`-.-´

 Respond  
^Neptune   -  Oct 29, 2008

Sweet! I've been working on the -w parameter :P

It just about works now.

 Respond  
TheImrac   -  Oct 29, 2008

FYI I changed a couple things neptune, added -u parameter for an "update alias" meaning it will be called every new packet received.

 Respond  
^Neptune   -  Oct 29, 2008

I'll be posting my script with that awesome alias sometime in the next week maybe. :P

 Respond  
Jace   -  Oct 29, 2008

Looks good.`-.-´

 Respond  
napa182   -  Oct 28, 2008

Nice work..

 Respond  
napalm`   -  Oct 28, 2008

/me ripz

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.