Advertisement
Guest User

pb2.lua

a guest
Aug 19th, 2015
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.68 KB | None | 0 0
  1. local component = require("component")
  2. local internet = require("internet")
  3. local event = require("event")
  4. local projector = component.pbProjector
  5. local bridge = openperipheral_bridge
  6.  
  7. local function UnHTMLEncode(s)
  8.     -- Warning: Improper
  9.     s = s:gsub("&lt;", "<")
  10.     s = s:gsub("&gt;", ">")
  11.     s = s:gsub("&quot;", "\"")
  12.     s = s:gsub("&#34;", "'")
  13.     s = s:gsub("&amp;", "&")
  14.     return s
  15. end
  16.  
  17. local function URLEncode(s)
  18.     s = tostring(s)
  19.     local new = ""
  20.  
  21.     for i = 1, #s do
  22.         local c = s:sub(i, i)
  23.         local b = c:byte()
  24.         if (b >= 65 and b <= 90) or (b >= 97 and b <= 122) or
  25.             (b >= 48 and b <= 57) or
  26.             c == "_" or c == "." or c == "~" then
  27.             new = new .. c
  28.         else
  29.             new = new .. string.format("%%%X", b)
  30.         end
  31.     end
  32.  
  33.     return new
  34. end
  35.  
  36. local function URLEncodeTable(vars)
  37.     local str = ""
  38.  
  39.     for k, v in pairs(vars) do
  40.         str = str .. URLEncode(k) .. "=" .. URLEncode(v) .. "&"
  41.     end
  42.  
  43.     return str:sub(1, -2)
  44. end
  45.  
  46. local function httpFetch(url,params,callback)
  47.     local body = ""
  48.     for chunk in internet.request(url,params) do
  49.         body = body..chunk
  50.     end
  51.     callback(body)
  52. end
  53.  
  54. --SearchYouTube(string query,function success,function failure)
  55. local function SearchYouTube(q, successF, failureF)
  56.     local vars = {
  57.         ["q"] = q,
  58.         ["orderby"] = "relevance",
  59.         ["fields"] = "items(id,snippet(title,thumbnails(default(url))))",
  60.         ["maxResults"] = "1",
  61.         -- ["format"] = "5", -- We can now play embedded videos!
  62.         ["part"] = "snippet",
  63.         ["key"] = "AIzaSyC8boYCJH5S5Z5pTuwx6HIUWYGeRZh-MOs"
  64.     }
  65.     local url = "https://www.googleapis.com/youtube/v3/search"
  66.  
  67.     httpFetch(url,vars, function(result)
  68.         local searchTable = util.JSONToTable(result)
  69.  
  70.         if(searchTable.items) then
  71.             if(searchTable.items[1]) then
  72.                 if(searchTable.items[1].id) then
  73.                     successF(searchTable.items[1].id.videoId,searchTable.items[1].snippet.title)
  74.                 else
  75.                     failureF("An error occurred while querying YouTube.")
  76.                 end
  77.             else
  78.                 failureF("An error occurred while querying YouTube.")
  79.             end
  80.         else
  81.             failureF("An error occurred while querying YouTube.")
  82.         end
  83.     end)
  84. end
  85.  
  86. while true do
  87.     local _, _, username, _, command = event.pull("glasses_chat_command")
  88. print(username, command)
  89.     local base = command:match("(.-) .+")
  90.     local the_rest = command:match(".- (.+)")
  91. print(base, the_rest)
  92.  
  93.     if(base == "ytplay") then
  94.       SearchYouTube(the_rest,function(id,title)
  95.           projector.setURL("http://youtube.com/watch?v="..id)
  96.       end,print)
  97.   end
  98. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement