PtokaX forum

Development Section => Your Developing Problems => Topic started by: bastya_elvtars on 05 September, 2004, 16:35:24

Title: LUA and internet connection
Post by: bastya_elvtars on 05 September, 2004, 16:35:24
Suppose I wanna DL a web page and apply pattern matching to it... The DLing part is what i am interested in... If anyone helped me id be grateful
Title:
Post by: Herodes on 05 September, 2004, 20:29:09
Look in this thread (http://board.univ-angers.fr/thread.php?threadid=2565&boardid=11) similar problem ....

I am happy to see you back here ;)
I hope you had a good summertime ..
Title:
Post by: bastya_elvtars on 05 September, 2004, 20:42:41
Thank you, mate, but i wanted someone to EXPLAIN me this stuff, cause if i do not understand what it is, i cannot (and are not willing to) write a script using it. I hope you understand me.
Title:
Post by: Herodes on 05 September, 2004, 20:45:26
Like how do you mean ..a sciprt that streams a code from a html page into a file ?
Title:
Post by: bastya_elvtars on 05 September, 2004, 20:57:31
OK

So, i need a bit info on how lua can made 2 connect 2 internet and download a page. After the page is downloaded, i need no help, i can do pattern matching alone hehe :P
Title:
Post by: bastya_elvtars on 06 September, 2004, 16:57:11
Sorry but i havent found this part in lua4 book... am i st00pid or it really is not there? ?(
Title: Example
Post by: bastya_elvtars on 06 September, 2004, 17:45:26
Let's get this script, i wanna know what are these socket, err  and other stuff. If u explain me what is the process then i can go on.

-- IMDBBOT By [NL]Pur and GoldMember
-- last updated: 8 juni 2003


MainBotName = "[bot]Security" -- Rename to match your Main hub bot
BotName = "[bot]iMDb"
TimeSpanInMinutes = 30

-- Commands --
cmd1 = "?search"             --
cmd2 = "?topmovies"          -- no arguments
cmd4 = "?restrict"           -- arguments
cmd5 = "?settings"           -- no arguments
cmd6 = "?sort"               -- arguments  - best search matches first
                             --            - alphabetically
                             --           - chronologically
cmd7 = "?type"               -- arguments - Regular expression
                             --            - fuzzy search
                             --             - word(s)
cmd8 = "?fromyear"           -- argument search from which year
cmd9 = "?toyear"             -- argument search to which year
cmda = "?help"

-- Default Settings --
RegisterBot = "PM"           -- PM = Results in PM , MainChat = Results in Main Chat
usetimer = "0"               -- 1 = Updates of Top15 with use of the timer , 0 = Updates when command ?topmovies is used
searchfor = '&tv=only'
sorts = '&sort=smart'
type = '&type=substring'
fromyear = '&from_year1890'
toyear = '&to_year2010'


function Main()
  if (RegisterBot=="PM") then
    frmHub:RegBot(BotName)
  else
    frmHub:UnregBot(BotName)
  end

  SendPmToOps(MainBotName,date("iMDb Activated [%d %B %Y  l %X] ("..RegisterBot.." Mode)"))
  SetTimer(TimeSpanInMinutes*60000)
  StartTimer()
end

function OnTimer()
  if (usetimer=="1") then
    getNews1()
  end
end


function Get1Arg(data)  
  s,e,cmd,arg = strfind( data, "%b<>%s+(%S+)%s+(.+)" )
  return arg
end

function wr(target,text)
  target:SendPM(BotName,text)
end

function DataArrival(curUser,data)
if (strsub(data, 1, 1) == "<") then
    data = strsub(data,1,strlen(data)-1)

    if (strfind(strlower(data), strlower(cmd1))) then
      arg = Get1Arg(data)
      if (arg==nil) then      
      else
        sp = strchar(32)
        arg = gsub(arg, sp, '+')
        findNews(curUser,arg..sorts..type..searchfor..fromyear..toyear)
      end
    end

    if (strfind(strlower(data), strlower(cmd2))) then
      if (usetimer=="0") then
         getNews1()
      end
      headpm(curUser)
      readfile(curUser,"top15.txt")
    end
 
    if (strfind(strlower(data), strlower(cmda))) then
      readfile(curUser,"helpimdb.txt")
    end
 
    if (getcmd(data,cmd4)==1) and (curUser.bOperator) then
       if (arg=='movie') then searchfor='&tv=off' end
       if (arg=='both') then searchfor='&tv=both' end
       if (arg=='tv') then searchfor='&tv=only' end
    end

    if (getcmd(data,cmd6)==1) and (curUser.bOperator)then
        if (arg=='smart') then sorts='&sort=smart' end
        if (arg=='alpha') then sorts='&sort=alpha' end
        if (arg=='chrono') then sorts='&sort=chrono' end
    end  

    if (getcmd(data,cmd7)==1) and (curUser.bOperator) then
      if (arg=='regexp') then type='&type=regexp' end
      if (arg=='fuzzy') then type='&type=fuzzy' end
      if (arg=='word') then type='&type=substring' end
    end

    if (getcmd(data,cmd8)==1) and (curUser.bOperator) then
      toyear = "&from_year"..arg
    end

    if (getcmd(data,cmd9)==1) and (curUser.bOperator) then
      toyear = "&to_year"..arg
    end
   
    if (strfind(strlower(data), strlower(cmd5))) then sett(curUser) end  
end
end

function getcmd(data,md)
  getmd=0
  if (strfind(strlower(data), strlower(md))) then
      arg = Get1Arg(data)
      if (arg==nil) then      
      else    
        getmd=1
        return getmd
      end
    end
end

function sett(target)
     readfile(target,"settings.txt")

     if (searchfor=='&tv=off') then
       res = " Movie"
     elseif (searchfor=='&tv=both') then
       res = " Both"
     elseif (searchfor=='&tv=only') then
       res = " Tv"
     end
     wr(target,"Restrict is set for: "..res)

     if (sorts=='&sort=smart') then
       sort = " Best search matches first"
     elseif (sorts=='&sort=alpha') then
       sort = " Alphabetically"
     elseif (sorts=='&sort=chrono') then
       sort = " Chronologically"
     end
     wr(target,"Sort method is set for: "..sort)

     if (type=='&type=regexp') then
       typed = " Regular expression"
     elseif (type=='&type=fuzzy') then
       typed = " Fuzzy search"
     elseif (type=='&type=substring') then
       typed = " Word(s)"
     end
     wr(target,"Type of search is: "..typed)
     
      fyear = gsub(fromyear, '&from_year','')
      tyear = gsub(toyear, '&to_year','')
     wr(target, "Bot searches from the year: "..fyear.."  until  "..tyear)
     wr(target,'')
     wr(target,"ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ")
end


send = " HTTP/1.1"..strchar(13, 10).."Host: us.imdb.com"..strchar(13, 10).."Connection: Close"..strchar(13, 10)..
       "User-Agent: Mozilla/4.0 (blah blah blah whatever blah)"..strchar(13, 10)..strchar(13, 10)

function findNews(target,find)
wr(target,"_____________")
wr(target," Find something!")
wr(target,"ŻŻŻŻŻŻŻŻŻŻŻŻŻ")
wr(target,"Results for: "..find)
wr(target,"ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ")

file = ""..find..".txt"
if (openfile("imdb/"..file,"r")==nil) then
  local line
  local socket, err = connect("www.us.imdb.com",80)
  writedonce=0
  tob=nil
    if not err then
      socket:timeout(2)
      err = socket:send("GET [URL]http://us.imdb.com/Tfuzzy?type=fuzzy&...rono&title=[/URL]"..find..send)
        if not err then
          writeto("imdb/"..find..".txt")
          while not(line == "") do
          line, err = socket:receive("*l")
           if err then break end
           s,e = strfind(line,"
  1.            tob=nil
               end
            end
            writeto()
          end
        end
        socket:close()
    end

    if (writedonce==1 or openfile("imdb/"..file,"r")~=nil) then
    --if (checkfile(file) == 1) then
      readfile(target,file)
      readfile(target,"endresult.txt")
    --end
    end

    end
     

    function strip(tob,num)
    tob = strsub(tob,num)
    tob = gsub(tob, '
    ','')
    tob = gsub(tob, '','')
    tob = gsub(tob, '
    ','')
    tob = gsub(tob, '
    ','')
    tob = gsub(tob, '
  2. ','')
    tob = gsub(tob, '','')
    tob = gsub(tob, '
    ','')
    tob = gsub(tob, '&#[i];','')
    tob = gsub(tob, '','')
    tob = gsub(tob, '
    ','')
    tob = gsub(tob, '','')
    tob = gsub(tob, '','')
    tob = gsub(tob, '','')

    for i = 160, 255 do
    n=stripchar(i)
    tob = gsub(tob, n,'')
    end

    return tob
    end

    function stripchar(num)
    n = '&#'..num..';'
    return n
    end

    function headpm(target)
    wr(target,"_________________")
    wr(target,"IMDb Top 15 Movies!")
    wr(target,"ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ")
    wr(target,"Rank was Title (YEAR)")
    wr(target,"ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ")
    end


    function getNews1()
    local line
    local socket, err = connect("www.us.imdb.com",80)
      if not err then
        socket:timeout(2)
        err = socket:send("GET [URL]http://www.us.imdb.com/Charts/top-titles-week.html[/URL]"..send)
          if not err then
            writeto("imdb/top15.txt")
            while not(line == "") do
            line, err = socket:receive("*l")
            s,e = strfind(line,"ALIGN=CENTER>")
            if (s~=nil) then tob = strip(line,33)end
            o,p = strfind(line,"ALIGN=CENTER>")
            if (o~=nil) then toc = strip(line,30)end

            c,v = strfind(line,"/Title?")
            if (c~=nil) then

            url = strsub(line,c,c+13)


    top = strip(line,38)
              if strfind(top,'?">') then
                top = strip(line,41)          
              else
                top = strip(line,48)          
              end
            end

            if (tob~=nil) and (toc~=nil) and (top~=nil) then
              url =  "http://www.us.imdb.com"..url

      write(" "..tob.." "..toc.." [ "..url.." ] "..top..strchar(13)..strchar(10))                                      
           
              tob=nil
              toc=nil
              top=nil
            end
          end
          writeto()
        end
      end
      socket:close()
    end

    function checkfile(file)
     readfrom ("imdb/"..file)
     n=1
    while 1 do
         local line = read()    
        e,g = strfind(line,"rating")
        if (e ~= nil) then
          n = 1
        else
          n = 0
          break
        end
        return n
    end
      readfrom()

    end

    function readfile(target,file)
      readfrom("imdb/"..file)
      while 1 do
        local line = read()    
        if (line ~= nil) then
          wr(target,line)
        else
          break
        end
      end
      readfrom()
    end
Title:
Post by: bastya_elvtars on 07 September, 2004, 09:00:16
Many thx, i will try it.
Title:
Post by: BottledHate on 07 September, 2004, 21:17:11
thanks for the info Mutor! this is great stuff!

-BH