Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- alias youtube {
- var %sock $Socket(youtube.com,80),%link = $1-
- if (!%link) { var %link = $input(Enter Link,oe,Youtube Downloader) }
- if (%link) && ($urlparse(%link).host == www.youtube.com) && (/watch?v=* iswm $urlparse(%link).path) {
- - $Socket(%sock,%link).settag
- - $Socket(%sock,OnYoutubeOpen).sockopen
- - $Socket(%sock,OnYoutubeClose).sockclose
- - $Socket(%sock,OnYoutubeRead).sockread
- - $Socket(%sock).connect
- }
- else { echo -a Youtube Downloader: Invalid Link, Syntax: /youtube http://www.youtube.com/watch?v=Code }
- }
- alias onYoutubeOpen {
- var %sw = sockwrite -n $sockname, %mark = $Socket($1).tag
- if (!$regex(%mark,/v=(.+)$/) || $sockerr) { echo -a Youtube Downloader: Connection Failed }
- var %code = $regml(1)
- %sw GET $urlparse(%mark).path HTTP/1.1
- %sw Host: www.youtube.com
- %sw Connection: Close
- %sw
- - $Class($1,file,%code $+ .tmp).setv
- }
- alias onYoutubeRead {
- sockread -f &d
- bwrite $Class($1,file).getv -1 -1 &d
- }
- alias onYoutubeClose {
- bread $Class($1,file).getv 1 $file($Class($1,file).getv).size &d
- var %1 = $bfind(&d,1,fmt_url_map=), %2 = $bfind(&d,%1,&)
- var %url = $bvar(&d,%1 - %2).text
- var %url = $urldecode(%url)
- var %url = $gettok($gettok(%url,2,124),1,44)
- .remove $Class($1,file).getv
- youtube2 %url $Class($1,file).getv
- $Socket($1).delete
- }
- alias youtube2 {
- if ($regex($1-,/http://(.+)/(.+)/)) { var %host = $regml(1) }
- echo -a %host
- var %sock = $Socket(%host,80)
- var %file = $2
- if ($regex($1-,/id=([a-z0-9]+)/$)) {
- - $Socket(%sock,$1).settag
- - $Socket(%sock,OnYoutube2Close).sockclose
- - $Socket(%sock,OnYoutube2Open).sockopen
- - $Socket(%sock,OnYoutube2Read).sockread
- - $Class(%sock,file,%file).setv
- - $Class(%sock,dialog,Youtube2_ $+ %sock).setv
- - $Socket(%sock).connect
- .remove %file
- }
- }
- alias OnYoutube2Open {
- var %sw = sockwrite -n $sockname, %mark = $Socket($1).tag
- if ($regex(%mark,/http://(.+)/(.+)/)) {
- %sw GET / $+ $regml(2) HTTP/1.1
- %sw Host: $regml(1)
- %sw Connection: Close
- %sw
- }
- .timer_progress_ $+ $1 0 1 _progress $1
- }
- ;http://www.youtube.com/watch?v=9x266qylESc
- alias onYoutube2Read {
- sockread -f &1
- if ($bfind(&1,1,Content-Length:)) {
- var %s = $v1, %e = $bfind(&1,$v1,$crlf)
- var %head = $bvar(&1,%s,$calc(%e - %s)).text
- if ($regex(%head,/Content-Length: (\d+)/g)) {
- echo -a $regml(1)
- $Class($1,length,$regml(1)).setv
- if ($regml(1) > 0) { dialog -m $Class($1,dialog).getv Youtube_Download }
- }
- }
- if (!$Class($1,count).getv) { - $Class($1,count,1).setv }
- if (!$sockbr) { return }
- hinc $1 count $bvar(&1,0)
- bwrite $Class($1,file).getv -1 -1 &1
- }
- alias _progress {
- var %sock = $1
- if (!$Class(%sock,ticks).getv) { - $Class(%sock,ticks,$ticks).setv }
- var %ticks = $Class(%sock,ticks).getv
- var %speed = $int($calc(($file($Class(%sock,file).getv).size / (($ticks - %ticks) / 1000)) / 1024)) kb/s
- did -ra $Class(%sock,dialog).getv 1 Downloading: $strip($tpbar($Class(%sock,count).getv,$Class(%sock,length).getv,20,[,],|,ยท,15,4,5,2).p,c) %speed
- }
- alias OnYoutube2Close {
- deheader $Class($1,file).getv $remove($class($1,file).getv,.tmp) $+ .flv
- did -h $Class(%sock,dialog).getv 1
- did -v $Class(%sock,dialog).getv 2
- did -e $Class(%sock,dialog).getv 3
- did -b $Class(%sock,dialog).getv 4
- .timer_progress_ $+ $1 off
- $Socket($1).delete
- }
- alias deheader {
- var %in = $1, %out = $2
- bread %in 0 $file(%in).size &in
- var %find = $bfind(&in,1,$crlf $+ $crlf)
- bcopy &out 1 &in $calc(%find + 4) -1
- bwrite %out 0 -1 &out
- if ($exists(%in)) { .remove %in }
- }
- alias urlencode return $regsubex($1-,/G(.)/g,$iif(($prop && 1 !isalnum) || !$prop,$chr(37) $+ $base($asc(1),10,16),1))
- alias tpbar return $+(,$8,$4,,$9,$str($6,$calc(($1 /$2)*$3)),,$10,$str($7,$calc($3 -($1 /$2)*$3)),,$8,$5) $iif($prop == p,$+(,$8,$4,,$base($11,10,10,2),$int($calc($1 /$2 *100)),%,,$8,$5)) $+
- alias pbar return $+(,$8,$4,,$9,$str($6,$calc(($1 /$2)*$3)),,$10,$str($7,$calc($3 -($1 /$2)*$3)),,$8,$5) $iif($prop == p,$+(,$8,$4,,$base($11,10,10,2),$int($calc($1 /$2 *100)),%,,$8,$5)) $+
- dialog Youtube_Download {
- title "Download"
- size -1 -1 164 53
- option dbu
- text "Downloading:", 1, 23 14 146 11
- text "Download Complete.", 2, 60 14 146 11, hide
- button "OK", 3, 43 31 37 12, default disable
- button "Cancel", 4, 95 31 37 12
- }
- on *:dialog:Youtube2_*:sclick:3:{ dialog -x $dname }
- on *:dialog:Youtube2_*:sclick:4:{
- var %sock $token($dname,2,95)
- if ($exists($class(%sock,file).getv)) { .remove $class(%sock,file).getv }
- .timer_progress_ $+ %sock off
- - $Socket(%sock).delete
- dialog -x $dname
- }
- alias urlparse {
- if ($regex($1-,/^(?:([a-z.+-]+)://|())(?:([^@:]+)(?::(.*?)|())@|()())([a-z0-9.-]+)(?::(d+)|())(/.*)?$/i)) {
- if ($prop == proto) return $iif($regml(1) != $null,$ifmatch,http)
- elseif ($prop == user) return $regml(2)
- elseif ($prop == pass) return $regml(3)
- elseif ($prop == host) return $regml(4)
- elseif ($prop == port) return $iif($regml(5) != $null,$ifmatch,$urlport($iif($regml(1) != $null,$ifmatch,http)))
- elseif ($prop == path) return $iif($regml(6) != $null,$ifmatch,/)
- else return $true
- }
- else return $iif(!$prop,$false)
- }
- alias urlport {
- return $iif($findtok(http ftp telnet nntp https gopher,$1,32),$gettok(80 21 23 119 443 70,$ifmatch,32),0)
- }
- alias urlencode {
- var %t = $1-, %r = "", %c
- while ($len(%t)) {
- %c = $asc($left(%t,1))
- %r = %r $+ $iif((%c <= 32) || (%c >= 127),% $+ $base($ifmatch,10,16,2),$chr(%c))
- %t = $right(%t,-1)
- }
- return %r
- }
- alias urldecode {
- var %t = $replace($1-,+,$chr(32),% $+ 20,$chr(32))
- return $regsubex(%t,/%([0-9A-F]{2})/g,$chr($base(\1,16,10)))
- }
- alias urlpath {
- var %t = $iif($right($1-,1) == /,/), %r = "", %i = 0
- while (%i < $numtok($1-,47)) {
- inc %i
- if ($gettok($1-,%i,47) == .) %r = %r $+ $iif($right(%r,1) != /,/)
- elseif ($gettok($1-,%i,47) != ..) %r = $instok(%r,$ifmatch,0,47)
- else {
- if (!$numtok(%r,47)) return $null
- %r = $deltok(%r,-1,47) $+ /
- }
- }
- return $iif(%r != /,/) $+ %r $+ $iif((%r != $null) && ($right(%r,1) != /),%t)
- }
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; Description: This is a Socket class which contains ;
- ; the basic structure of a class ;
- ; Usage: $Socket(<Instance>,<Params>,..).<Method> ;
- ; Example: var %x $Socket(127.0.0.1,1337,LocalSock) ;
- ; Example: $Socket(%x,SockOpenAlias).sockopen ;
- ; Example: $Socket(%x,SockReadAlias).sockread ;
- ; Example: $Socket(%x).connect ;
- ; Example: $Socket(%x).close ;
- ; Example: $Socket(%x).delete ;
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; Public Declarations ;
- ;;;;;;;;;;;;;;;;;;;;;;;
- alias Socket.DELETE.Public
- alias Socket.PORT.Public
- alias Socket.IP.Public
- alias Socket.OPEN.Public
- alias Socket.CONNECT.Public
- alias Socket.LISTEN.Public
- alias Socket.CLOSE.Public
- alias Socket.SOCKREAD.Public
- alias Socket.SOCKOPEN.Public
- alias Socket.SOCKCLOSE.Public
- alias Socket.SOCKACCEPT.Public
- alias Socket.TAG.Public
- alias Socket.SETTAG.Public
- alias Socket.SETIP.Public
- alias Socket.SETPORT.Public
- alias Socket.WRITE.Public
- alias Socket.SOCK.Public
- ;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; Exception Declarations ;
- ;;;;;;;;;;;;;;;;;;;;;;;;;;
- alias Socket.EXCEPTION.SockErr {
- $Class($1,SockErr).getv $1-
- return $iif($bvar(&SockErr,0),$bvar(&SockErr,1-).text)
- }
- ;;;;;;;;;;;;;;;;
- ; Socket Alias ;
- ;;;;;;;;;;;;;;;;
- alias Socket {
- if (!$prop) goto init
- if ($IsPublic(Socket,$prop)) goto $prop
- return $catch($1,MemberErr, $qt($prop) is not a public member of Socket)
- :init
- return $Socket.INIT($1,$2,$3)
- :delete
- return $Socket.DELETE($1)
- :open
- :connect
- if ($sock($1).status) $catch($1,SockOpenErr,Socket $1 is already open)
- sockopen $1 $Class($1,IP).getv $Class($1,PORT).getv
- return
- :close
- if ($sock($1).status) sockclose $1
- else return $catch($1,SockCloseErr,Sock $1 was not open)
- return
- :listen
- if ($sock($1).status) $catch($1,SockOpenErr,Socket $1 is already open)
- var %x $iif($Class($1,IP).getv,$v1,127.0.0.1),%y $iif(%x != $null,-d)
- socklisten %y %x $1 $Class($1,PORT).getv
- ;socklisten $1 $Class($1,PORT).getv
- return
- :sockclose
- return $iif($2,$Class($1,CLOSE,$2).setv,$Class($1,CLOSE).getv)
- :sockopen
- return $iif($2,$Class($1,OPEN,$2).setv,$Class($1,OPEN).getv)
- :sockaccept
- return $iif($2,$Class($1,ACCEPT,$2).setv,$Class($1,ACCEPT).getv)
- :sockread
- return return $iif($2,$Class($1,READ,$2).setv,$Class($1,READ).getv)
- :settag
- $Class($1,Tag,$2).setv
- :tag
- return $Class($1,Tag).getv
- :setip
- return $Class($1,IP,$2).setv
- :setport
- return $Class($1,PORT,$2).setv
- :ip
- return $Class($1,IP).getv
- :port
- return $Class($1,PORT).getv
- :sock
- return $Class($1,Handle).getv
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; Put your own method handlers here ;
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- :write
- if ($sock($1).status != active) return $catch($1,SockErr,Socket $1 is not connected)
- sockwrite $1 $2
- return
- :MethodHandler
- }
- ;;;;;;;;;;;;;;;;;;
- ; Socket Methods ;
- ;;;;;;;;;;;;;;;;;;
- alias Socket.INIT {
- var %instance $Class
- $inheritsFrom(%instance,Socket)
- $Class(%instance,IP,$1).setv
- $Class(%instance,PORT,$2).setv
- $Class(%instance,OPEN,Socket.DefaultSockOpen).setv
- $Class(%instance,CLOSE,Socket.DefaultSockClose).setv
- $Class(%instance,ACCEPT,Socket.DefaultSockAccept).setv
- $Class(%instance,READ,Socket.DefaultSockRead).setv
- $Class(%instance,SockErr,Socket.DefaultSockErr).setv
- $Class(%instance,Tag,$iif($isinstance($3),$3,$3)).setv
- $Class(%instance,Handle,%instance $+ ).setv
- ;;;;;;;;;;;;;;;;;;;;;;;;
- ; Do Initializing here ;
- ;;;;;;;;;;;;;;;;;;;;;;;;
- return %instance
- }
- alias Socket.DELETE {
- ;;;;;;;;;;;;;;;;;;;;;;
- ; Do Destroying here ;
- ;;;;;;;;;;;;;;;;;;;;;;
- if ($sock($1).status) sockclose $1
- return $Class($1).DELETE
- }
- ;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; Default Event Handlers ;
- ;;;;;;;;;;;;;;;;;;;;;;;;;;
- alias Socket.DefaultSockOpen {
- + Successfully $iif($sock($sockname).status != Active,dis) $+ connected to $sock($sockname).ip $+ : $+ $sock($sockname).port
- }
- alias Socket.DefaultSockClose {
- + Successfully disconnected from $sock($sockname).ip $+ : $+ $sock($sockname).port
- $Socket($1).delete
- }
- alias Socket.DefaultSockRead {
- sockread -f %temp
- + Received data on socket: $1: %temp
- }
- alias Socket.DefaultSockAcceptW {
- var %x $sockname $+
- sockaccept %x
- var %a $socket($sock(%x).ip,$sock(%x).port)
- sockrename %x %a $+
- $Class($left($sockname,-1),ACCEPT).getv $left($sockname,-1) %a
- }
- alias Socket.DefaultSockAccept {
- var %socklistn $1
- var %socknew $2
- + Socket accepted from $+ $socket($2).ip $+ on port $+ $socket($2).port $+ : Handle = $2
- }
- alias Socket.DefaultSockErr {
- ;sockclose $1
- }
- alias SockErr {
- bset -t &SockErr $1-
- }
- ;;;;;;;;;;;;;;;;;;;;
- ; Trigger Handlers ;
- ;;;;;;;;;;;;;;;;;;;;
- on 1:sockopen:*: $Class($left($sockname,-1),OPEN).getv $left($sockname,-1)
- on 1:sockclose:*: $Class($left($sockname,-1),CLOSE).getv $left($sockname,-1)
- on 1:sockread:*: $Class($left($sockname,-1),READ).getv $left($sockname,-1)
- on 1:socklisten:*: Socket.DefaultSockAcceptW
- ;;;;;;;;;;;;;;
- ; End Socket ;
- ;;;;;;;;;;;;;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement