Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Function to URL encode a string
- local function urlEncode(str)
- if str then
- str = string.gsub(str, "([^%w ])", function(c)
- return string.format("%%%02X", string.byte(c))
- end)
- str = string.gsub(str, " ", "+")
- end
- return str
- end
- -- Function to fetch search results from Google
- local function searchGoogle(query)
- local apiKey = "AIzaSyDQJ7SjasKcPq_bJhCyxuaoWiVydYTGDK0"
- local engineId = "c3619c6476b78442f"
- local encodedQuery = urlEncode(query)
- local apiUrl = "https://www.googleapis.com/customsearch/v1?key=" .. apiKey .. "&cx=" .. engineId .. "&q=" .. encodedQuery .. "&num=3"
- local response = ""
- local request = http.get(apiUrl)
- if request then
- response = request.readAll()
- request.close()
- else
- print("Failed to fetch data from Google API")
- end
- return response
- end
- -- Function to parse JSON response
- local function parseJson(jsonString)
- local obj = {}
- local stack = {}
- local curr = obj
- local key
- local inString = false
- local stringValue = ""
- for i = 1, #jsonString do
- local c = jsonString:sub(i, i)
- if c == "{" and not inString then
- local newTable = {}
- if key then
- curr[key] = newTable
- key = nil
- else
- table.insert(stack, curr)
- table.insert(stack, key)
- table.insert(stack, i)
- key = nil
- end
- curr = newTable
- elseif c == "}" and not inString then
- local startIndex = table.remove(stack)
- local key = table.remove(stack)
- local parent = table.remove(stack)
- local endIndex = i
- local value = parseJson(jsonString:sub(startIndex, endIndex))
- if key then
- parent[key] = value
- else
- table.insert(parent, value)
- end
- curr = parent
- elseif c == "\"" then
- inString = not inString
- elseif c == ":" and not inString then
- key = stringValue
- stringValue = ""
- elseif c == "," and not inString then
- if key then
- curr[key] = stringValue
- key = nil
- else
- table.insert(curr, stringValue)
- end
- stringValue = ""
- else
- stringValue = stringValue .. c
- end
- end
- return obj
- end
- -- Function to display search results in text
- local function displaySearchResults(searchData)
- if searchData and searchData.items then
- print("Search Results:")
- for i, item in ipairs(searchData.items) do
- print("Result " .. i .. ":")
- print("Title: " .. (item.title or "Unknown"))
- print("Link: " .. (item.link or "Unknown"))
- print("Snippet: " .. (item.snippet or "Unknown"))
- print("--------------------------------------")
- end
- else
- print("No search results found.")
- end
- end
- -- Main function
- local function main()
- print("..####....####....####....####...##......######.")
- print(".##......##..##..##..##..##......##......##.....")
- print(".##.###..##..##..##..##..##.###..##......####...")
- print(".##..##..##..##..##..##..##..##..##......##.....")
- print("..####....####....####....####...######..######.")
- print("................................................")
- print("Enter your search query:")
- local query = read()
- local searchResponse = searchGoogle(query)
- print("Response:")
- print(searchResponse)
- local searchData = parseJson(searchResponse)
- displaySearchResults(searchData)
- end
- -- Call the main function
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement