Advertisement
nonogamer9

google for openOS

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