JPiolho

[Minecraft][CC] Steam API

Mar 5th, 2012
1,274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.77 KB | None | 0 0
  1. -- Steam API
  2. -- Version 1.0
  3. -- By JPiolho
  4. -- Feel free to copy and edit as long you credit the original author
  5.  
  6. local function trim (s)
  7.    return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
  8. end
  9.  
  10. local function IsPrivate(html)
  11.     local s,e = string.find(html,"<p class=\"errorPrivate\">")
  12.  
  13.     if s == nil or e == nil then return false end
  14.  
  15.     return true
  16. end
  17.  
  18. function getGameList(userid)
  19.     if http == nil then return nil end
  20.  
  21.     local tbl = {}
  22.  
  23.     local html = http.get("http://steamcommunity.com/" .. userid .. "/games?tab=all")
  24.     if html == nil then return nil end
  25.  
  26.     html = html:readAll()
  27.  
  28.     if IsPrivate(html) then return nil end
  29.  
  30.     local s = 0
  31.     local e = 0
  32.     while true do
  33.         s,e = string.find(html,"<div class=\"gameListRowItem\"",e+1)
  34.  
  35.         if s == nil or e == nil then break end
  36.  
  37.  
  38.  
  39.         local s2,e2 = string.find(html,"<h4>",e)
  40.         local s3,e3 = string.find(html,"</h4>",e2)
  41.  
  42.         local gamename = string.sub(html,e2+1,s3-1)
  43.  
  44.         local s4,e4 = string.find(html,"<h5>",e3)
  45.  
  46.         if s4 ~= nil and e4 ~= nil then
  47.             local s5,e5 = string.find(html,"hrs on record</h5>",e4)
  48.  
  49.             if s5 ~= nil and e5 ~= nil then
  50.                 local hours = trim(string.sub(html,e4+1,s5-1))
  51.                 tbl[gamename] = tonumber(hours)
  52.             end
  53.         else
  54.             tbl[gamename] = 0
  55.         end
  56.  
  57.     end
  58.  
  59.     return tbl
  60. end
  61.  
  62. function getStatus(userid)
  63.     if http == nil then return nil end
  64.  
  65.     local tbl = {}
  66.  
  67.     local html = http.get("http://steamcommunity.com/" .. userid)
  68.     if html == nil then return nil end
  69.  
  70.     html = html:readAll()
  71.  
  72.     if IsPrivate(html) then return nil end
  73.  
  74.  
  75.  
  76.     local s,e = string.find(html,"<div id=\"OnlineStatus\">")
  77.  
  78.     local s2,e2 = string.find(html,"<p id=\"statusOfflineText\">",e)
  79.     if s2 ~= nil and e2 ~= nil then return "Offline" end
  80.  
  81.     s2,e2 = string.find(html,"<p id=\"statusOnlineHeader\">",e)
  82.     if s2 ~= nil and e2 ~= nil then return "Online" end
  83.  
  84.     s2,e2 = string.find(html,"<p id=\"statusInGameText\">",e)
  85.     if s2 ~= nil and e2 ~= nil then
  86.         local s3,e3 = string.find(html,"<br />",e2)
  87.  
  88.         return trim(string.sub(html,e2+1,s3-1))
  89.     end
  90.  
  91.     return nil
  92. end
  93.  
  94. function userID(method,id)
  95.     return method .. "/" .. id
  96. end
  97.  
  98. function getFriends(userid)
  99.     if http == nil then return nil end
  100.  
  101.     local tbl = {}
  102.  
  103.     local html = http.get("http://steamcommunity.com/" .. userid .. "/friends")
  104.     if html == nil then return nil end
  105.  
  106.     html = html:readAll()
  107.  
  108.     if IsPrivate(html) then return nil end
  109.  
  110.     local s,e = string.find(html,"<div id=\"memberList\">")
  111.  
  112.     if s == nil or e == nil then return nil end
  113.  
  114.  
  115.     local tbl = {}
  116.  
  117.     local es,ee = string.find(html,"footer",e)
  118.  
  119.     local s = 0
  120.     local e = 0
  121.     while true do
  122.         s,e = string.find(html,"<p>",e+1)
  123.  
  124.         if s == nil or s >= es or e >= ee then break end
  125.  
  126.         local s2,e2 = string.find(html,"</p>",e)
  127.  
  128.  
  129.  
  130.         local s3,e3 = string.find(html,"<a class=\"linkFriend_in-game\" href=\"http://steamcommunity.com/",e)
  131.  
  132.         if s3 == nil or s3 > s2 or e3 > s2 then s3,e3 = string.find(html,"<a class=\"linkFriend_online\" href=\"http://steamcommunity.com/",e) end
  133.         if s3 == nil or s3 > s2 or e3 > s2 then s3,e3 = string.find(html,"<a class=\"linkFriend_offline\" href=\"http://steamcommunity.com/",e) end
  134.  
  135.         if s3 ~= nil and s3 < s2 and e3 < s2 then
  136.  
  137.  
  138.             local s4,e4 = string.find(html,"\">",e3)
  139.  
  140.             local userid = string.sub(html,e3+1,s4-1)
  141.  
  142.             local s5,e5 = string.find(html,"</a>",e4)
  143.  
  144.             local username = string.sub(html,e4+1,s5-1)
  145.  
  146.             tbl[userid] = username
  147.         end
  148.  
  149.  
  150.  
  151.     end
  152.  
  153.     return tbl
  154. end
  155.  
  156. function isGoldenPotato(userid)
  157.     if http == nil then return nil end
  158.  
  159.     local tbl = {}
  160.  
  161.     local html = http.get("http://steamcommunity.com/" .. userid)
  162.     if html == nil then return nil end
  163.  
  164.     html = html:readAll()
  165.  
  166.     if IsPrivate(html) then return nil end
  167.  
  168.     local s,e = string.find(html,"title=\"Golden Potato\">")
  169.  
  170.     if s == nil or e == nil then return false end
  171.  
  172.     return true
  173. end
Advertisement
Add Comment
Please, Sign In to add comment