Advertisement
nonogamer9

google for CraftOS

May 5th, 2024 (edited)
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.78 KB | Gaming | 0 0
  1. -- Function to URL encode a string
  2. local function urlEncode(str)
  3.     if str then
  4.         str = string.gsub(str, "([^%w ])", function(c)
  5.             return string.format("%%%02X", string.byte(c))
  6.         end)
  7.         str = string.gsub(str, " ", "+")
  8.     end
  9.     return str
  10. end
  11.  
  12. -- Function to fetch search results from Google
  13. local function searchGoogle(query)
  14.     local apiKey = "AIzaSyDQJ7SjasKcPq_bJhCyxuaoWiVydYTGDK0"
  15.     local engineId = "c3619c6476b78442f"
  16.  
  17.     local encodedQuery = urlEncode(query)
  18.     local apiUrl = "https://www.googleapis.com/customsearch/v1?key=" .. apiKey .. "&cx=" .. engineId .. "&q=" .. encodedQuery .. "&num=3"
  19.  
  20.     local response = ""
  21.     local request = http.get(apiUrl)
  22.     if request then
  23.         response = request.readAll()
  24.         request.close()
  25.     else
  26.         print("Failed to fetch data from Google API")
  27.     end
  28.  
  29.     return response
  30. end
  31.  
  32. -- Function to parse JSON response
  33. local function parseJson(jsonString)
  34.     local obj = {}
  35.     local stack = {}
  36.     local curr = obj
  37.     local key
  38.     local inString = false
  39.     local stringValue = ""
  40.  
  41.     for i = 1, #jsonString do
  42.         local c = jsonString:sub(i, i)
  43.  
  44.         if c == "{" and not inString then
  45.             local newTable = {}
  46.             if key then
  47.                 curr[key] = newTable
  48.                 key = nil
  49.             else
  50.                 table.insert(stack, curr)
  51.                 table.insert(stack, key)
  52.                 table.insert(stack, i)
  53.                 key = nil
  54.             end
  55.             curr = newTable
  56.         elseif c == "}" and not inString then
  57.             local startIndex = table.remove(stack)
  58.             local key = table.remove(stack)
  59.             local parent = table.remove(stack)
  60.             local endIndex = i
  61.             local value = parseJson(jsonString:sub(startIndex, endIndex))
  62.             if key then
  63.                 parent[key] = value
  64.             else
  65.                 table.insert(parent, value)
  66.             end
  67.             curr = parent
  68.         elseif c == "\"" then
  69.             inString = not inString
  70.         elseif c == ":" and not inString then
  71.             key = stringValue
  72.             stringValue = ""
  73.         elseif c == "," and not inString then
  74.             if key then
  75.                 curr[key] = stringValue
  76.                 key = nil
  77.             else
  78.                 table.insert(curr, stringValue)
  79.             end
  80.             stringValue = ""
  81.         else
  82.             stringValue = stringValue .. c
  83.         end
  84.     end
  85.  
  86.     return obj
  87. end
  88.  
  89. -- Function to display search results in text
  90. local function displaySearchResults(searchData)
  91.     if searchData and searchData.items then
  92.         print("Search Results:")
  93.         for i, item in ipairs(searchData.items) do
  94.             print("Result " .. i .. ":")
  95.             print("Title: " .. (item.title or "Unknown"))
  96.             print("Link: " .. (item.link or "Unknown"))
  97.             print("Snippet: " .. (item.snippet or "Unknown"))
  98.             print("--------------------------------------")
  99.         end
  100.     else
  101.         print("No search results found.")
  102.     end
  103. end
  104.  
  105. -- Main function
  106. local function main()
  107.     print("..####....####....####....####...##......######.")
  108.     print(".##......##..##..##..##..##......##......##.....")
  109.     print(".##.###..##..##..##..##..##.###..##......####...")
  110.     print(".##..##..##..##..##..##..##..##..##......##.....")
  111.     print("..####....####....####....####...######..######.")
  112.     print("................................................")
  113.     print("Enter your search query:")
  114.     local query = read()
  115.     local searchResponse = searchGoogle(query)
  116.     print("Response:")
  117.     print(searchResponse)
  118.     local searchData = parseJson(searchResponse)
  119.     displaySearchResults(searchData)
  120. end
  121.  
  122. -- Call the main function
  123. main()
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement