Ugrás a tartalomhoz

Modul:kartographerFindData

A Wikiszótárból, a nyitott szótárból

A modult a Modul:kartographerFindData/doc lapon tudod dokumentálni

local json = require("json")
local http = require("socket.http")

-- Function to make an HTTP GET request
local function http_get(url)
    local response_body = {}
    local res, code, response_headers = http.request{
        url = url,
        sink = ltn12.sink.table(response_body),
    }
    return table.concat(response_body), code
end

-- Function to search Wikidata for a given Wiktionary page name
local function search_entity_id(page_name)
    local url = "https://www.wikidata.org/w/api.php?action=query&list=search&srsearch=" .. page_name .. "&format=json"
    local response, code = http_get(url)
    if code == 200 then
        local data = json.decode(response)
        if data and data["query"] and data["query"]["search"] and #data["query"]["search"] > 0 then
            return data["query"]["search"][1]["title"]
        end
    end
    return nil
end

-- Example usage
local wiktionary_page_name = "Apple"  -- Example Wiktionary page name
local entity_id = search_entity_id(wiktionary_page_name)
if entity_id then
    print("Entity ID for '" .. wiktionary_page_name .. "': " .. entity_id)
else
    print("Entity ID not found for '" .. wiktionary_page_name .. "'")
end