Go Back   The PokéCommunity Forums > Creative Discussions > Game Development > Pokémon Essentials
Reload this Page [Misc.] http functions in Essentials

Notices
For all updates, view the main page.

Pokémon Essentials All questions and discussion about Pokémon Essentials, the Pokémon game kit for RPG Maker XP, go in here. Also contains links to the latest downloads and the Essentials Wiki.



Closed Thread
 
Thread Tools
  #1    
Old February 9th, 2012 (09:42 AM).
Ho-oh 112's Avatar
Ho-oh 112 Ho-oh 112 is offline
Advance Scripter
 
Join Date: Feb 2011
Age: 19
Gender: Male
Nature: Hasty
Posts: 311
Right so I was going through the Sockets scripts and I FOUND AN ARRAY OF "http functions" allowing you to download/post data (I knew about the download data but the posting?)

I bet people could use this for trades as an alternative from mySQL....
I'm pretty sure Poccil made this....
Code:
 
#############################
#
# HTTP utility functions
#
#############################
def pbPostData(url, postdata, filename=nil, depth=0)
  userAgent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.14) Gecko/2009082707 Firefox/3.0.14"
  if url[/^http:\/\/([^\/]+)(.*)$/]
    host=$1
    path=$2
    path="/" if path.length==0
    body = postdata.map {|key, value|
       keyString=key.to_s
       valueString=value.to_s
       keyString.gsub!(/[^a-zA-Z0-9_\.\-]/n) {|s| sprintf('%%%02x', s[0]) }
       valueString.gsub!(/[^a-zA-Z0-9_\.\-]/n) {|s| sprintf('%%%02x', s[0]) }
       next "#{keyString}=#{valueString}"
    }.join('&')
    request="POST #{path} HTTP/1.1\nUser-Agent: #{userAgent}\nPragma: no-cache\nHost: #{host}\nProxy-Connection: Close\n"
    request+="Content-Type: application/x-www-form-urlencoded\n"
    request+="Content-Length: #{body.length}\n"
    request+="\n"
    request+=body
    return pbHttpRequest(host, request, filename, depth)
  end
  return ""
end
def pbDownloadData(url, filename=nil, depth=0)
  userAgent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.14) Gecko/2009082707 Firefox/3.0.14"
  if depth>10
    raise "Redirection level too deep"
  end
  if url[/^http:\/\/([^\/]+)(.*)$/]
    host=$1
    path=$2
    path="/" if path.length==0
    request="GET #{path} HTTP/1.1\nUser-Agent: #{userAgent}\nPragma: no-cache\nHost: #{host}\nProxy-Connection: Close\n\n"
    return pbHttpRequest(host, request, filename, depth)
  end
  return ""
end
def pbHttpRequest(host, request, filename=nil, depth=0)
  if depth>10
    raise "Redirection level too deep"
  end
  socket=::TCPSocket.new(host, 80)
  time=Time.now.to_i
  begin
    socket.send(request)
    result=socket.gets
    data=""
    # Get the HTTP result
    if result[/^HTTP\/1\.[01] (\d+).*/]
      errorcode=$1.to_i
      if errorcode>=400 && errorcode<500
        raise "HTTP Error #{errorcode}"
      end
      headers={}
      # Get the response headers
      while true
        result=socket.gets.sub(/\r$/,"")
        break if result==""
        if result[/^([^:]+):\s*(.*)/]
          headers[$1]=$2
        end
      end
      length=-1
      chunked=false
      if headers["Content-Length"]
        length=headers["Content-Length"].to_i
      end
      if headers["Transfer-Encoding"]=="chunked"
        chunked=true
      end
      if headers["Location"] && errorcode >= 300 && errorcode < 400
        socket.close rescue socket=nil
        return pbDownloadData(headers["Location"],filename,depth+1)
      end
      if chunked==true
        # Chunked content
        while true
          lengthline=socket.gets.sub(/\r$/,"")
          length=lengthline.to_i(16)
          break if length==0
          while Time.now.to_i-time>=5 || socket.select(10)==0
            time=Time.now.to_i
            Graphics.update
          end
          data+=socket.recv(length)
          socket.gets
        end
      elsif length==-1
        # No content length specified
        while true
          if socket.select(500)==0
            break
          else
            while Time.now.to_i-time>=5 || socket.select(10)==0
              time=Time.now.to_i
              Graphics.update
            end
            data+=socket.recv(1)
          end
        end
      else
        # Content length specified
        while length>0
          chunk=[length,4096].min
          while Time.now.to_i-time>=5 || socket.select(10)==0
            time=Time.now.to_i
            Graphics.update
          end
          data+=socket.recv(chunk)
          length-=chunk
        end
      end
    end
    if filename
      File.open(filename,"wb"){|f|
         f.write(data)
      }
    else
      return data
    end
    ensure
    socket.close rescue socket=nil
  end
  return ""
end
def pbDownloadToString(url)
  begin
    data=pbDownloadData(url)
    return data
    rescue
    return ""
  end 
end
def pbDownloadToFile(url, file)
  begin
    pbDownloadData(url,file)
    rescue
  end
end
def pbPostToString(url, postdata)
  begin
    data=pbPostData(url, postdata)
    return data
    rescue
    return ""
  end 
end
def pbPostToFile(url, postdata, file)
  begin
    pbPostData(url, postdata,file)
    rescue
  end
end
HOW COULD I MISS THIS???
__________________
Scripting Level: Advance

RMXP Poke-Essentials MySQL Trading System Link:
http://www.pokecommunity.com/showthread.php?p=7077162#post7077162

RMXP Poke-Essentials Mystery Gift Link:
http://www.pokecommunity.com/showthread.php?t=270687

RMXP Poke-Essentials Award System Link:
http://www.pokecommunity.com/showthread.php?p=7111566
  #2    
Old February 9th, 2012 (02:22 PM).
seeker's Avatar
seeker seeker is online now
adventure seeker
Platinum Tier
 
Join Date: Apr 2009
Location: Ireland
Posts: 10,661
I'd like you to clarify the topic of this discussion and post it again. All you've done is post a script available in essentials and say what it might be useful for, but that's not enough to go on. I don't know why you've entitled this with the "Misc" prefix. What is this thread for? I have no idea. You need to actually have a discussion topic, you shouldn't just post scripts and throw out an extremely vague topic around it.

Please make a new thread with a topic and a direction to go in. If you're hoping to convert this into a trade script, then state it in the discussion and ask how people could approach this. Then use the prefix [Discussion].

Locked.
__________________

| | |
Closed Thread
Quick Reply

Sponsored Links

You may also like.. (Beta)
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are UTC -8. The time now is 01:50 PM.