Jump to content
Sign in to follow this  
Mock

Downloader em lua!

Recommended Posts

Mock    32
Mock

Eu fiz apenas brincando isso no começo, eu nao sabia da possibilidade do io.open abrir arquivos com wb e rb e por isso quando fiz apenas fazia download te textos agora graças ao nosso amigo colex eu fiz um que baixa qualquer arquivo* :P

 

Ainda tem ums pogs aki eu fiz e nem revisei o code eu fiz testei tirei algums probleminhas e pronto ta feliz

 

E uma funçao naverdade...

 

downloadFile(IP,PORT,fileRequested,outputname[,bytes,delay,htmlVer])

 

Um exemplo:

downloadFile('img6.imageshack.us',80,'img6/7580/aaanz5.jpg','q.jpg')

Ele vai baixar o arquivo

http://img6.imageshack.us/img6/7580/aaanz5.jpg

que é nossa foto:

[spoiler=foto]aaanz5.jpg

 

e vai salva-la com nome de q.jpg :D

os params

bytes,delay,htmlVer

bytes vc pode escolher em quantos bytes pretende baixar por receive 1024, 2048 1 byte... :P

delay e caos queira colocar um delay entre cada receive e esse htmlver e pra alguns webservers que ficam de pirraça quando vc manda html 1.1 ele so aceita 1.0 ou vice e versa xP

Olha a funçao:

require 'ex'
require 'socket'
function math.percent(v, t, f)    --- By skyen hasus
if f then
	return math.floor((v/t)*100)
else
	return (v/t)*100
end
end
function downloadFile(IP,PORT,fileRequested,outputName, byte_s ,delay,htmlVer) --- Function by mock
assert(fileRequested,'fileRequested is nil!')
IP = IP or 'localhost'
PORT = PORT or 80
local client = socket.connect(IP,PORT)
print('Iiniciando download em: http//'..IP..':'..PORT..'/'..fileRequested..'/')
if client then
	client:setoption('keepalive', true)
	client:send('GET /'..fileRequested..' HTTP/'..(htmlVer or '1.0')..'\r\n\r\n')
	local byte = 0
	local findfile = false
	local star = os.clock()
	local file = io.open(outputName, "wb")
	if not file then
		print('Nao foi possivel criar '..fileRequested)
		return false
	end
	repeat
		r = client:receive(sizee)
		if r then
			if string.find(r,'Content%-Length:') then
				size = string.match(r,[[Content%-Length: (%d+)]]) or '??'
				print('Size:'..size)
			end
		end
		if r == "" then
			findfile = true
			print('Iniciando!')
		end
		if findfile == true and r then
			file:write(r)
			byte = byte+r:len()
			sizee = byte_s or 1048
			os.sleep(delay or 0.001)
			per = math.percent(byte, size or 100,1)
			if per ~= old then
				print(byte..'/'..(size or '?'),per..'%')
				old = per
			end
		end
	until not r
	file:close()
	print('\aDownload finalizado em '..(os.clock()-star)..' segundos.')
else
	print('Nao foi possivel conectar!')
	return false
end
return true
end

downloadFile('img6.imageshack.us',80,'img6/7580/aaanz5.jpg','q.jpg')

Quando executar ela vai baxar a foto do imageshack automaticamente

 

Ainda tem alguns bugs xP

Edited by Mickfern

Share this post


Link to post
Share on other sites
Gpwjhlkdcf    21
Gpwjhlkdcf

function math.percent(v, t, f)    --- By skyen hasus
if f then
	return math.floor((v/t)*100)
else
	return (v/t)*100
end
end

Nem lembrava mais dessa função. Tá muito tosca, muda ela ._.'

Edited by Mickfern

Share this post


Link to post
Share on other sites
Dark Billie    0
Dark Billie

ursinho você podia colocar umas explicações das funções da OTAL para ficar de mais fácil entendimento, fui no site lá só via coisa que nem entendia rs

Share this post


Link to post
Share on other sites
Mock    32
Mock

@Dark Billie

qq a otal tem aver com isso? ._.

Share this post


Link to post
Share on other sites
Dark Billie    0
Dark Billie

cheio de poo que eu ñ entendo

Share this post


Link to post
Share on other sites
Colex    0
Colex

faltava só uma letrinha :P

ficou legal o resultado :)

Share this post


Link to post
Share on other sites
Kaotar    5
Kaotar

A so pra lembrar, não funcionou aqui =/ que tristeza, na veradade eu não sei por que, vamo se disser quando você baixa um arquivo .rar ele baixa e etc, mas quando vai abrir diz que deu um erro inesperado, não sei bem pq, o tamanho é igual e etc. Mas tudo bem

 

Flws

 

EDIT:

 

Ops, percebi que falta algum numero de bytes, entre o baixado por isso e pelo baixado pelo Browser vo ver se resolvo

Edited by Kaotar

Share this post


Link to post
Share on other sites
Mock    32
Mock

@Kaotar

essa funçao é pog puro

nem use

Share this post


Link to post
Share on other sites
Mickfern    32
Mickfern

Ai Mock, eu sei que voce pois ali, mas gostaria de saber se realmente pode baixar qualquer coisa ? Tipo não precisa ser coisas para ot's ( que nem a otal, que voce instala no server ) e sim, se pode baixar, por exemplo, filmes, etc...

Share this post


Link to post
Share on other sites
Mock    32
Mock

@Mickfern

naoi cara isso é apenas uma tentativa de fazer um script p baxa coisas usando lua, so q é mais lento que tudo isso ai

Share this post


Link to post
Share on other sites
Fucknerds    0
Fucknerds

Poderia me esplicar mais a função dele ?

 

GRATO ;D

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×