Module:JSON pull test
Appearance
Created as a general test module for the project described at User:SnowyCinema/The Next Stage of Wikisource Automation.
local p = {}
local function getFileName(pageName)
-- Split the page name to extract the file name
local fileName = mw.ustring.match(pageName, "(.+)/")
return fileName
end
function p.pullMessage(frame)
-- Get the current page name
local pageName = mw.title.getCurrentTitle().text
local fileName = getFileName(pageName)
if not fileName then
return pageName
end
-- Build the Index file path
local indexPage = "Index:" .. fileName .. "/data.json"
-- Fetch the JSON data
local indexContent = mw.title.new(indexPage):getContent()
if not indexContent then
return "Error: Unable to fetch JSON data."
end
-- Parse the JSON data
local success, data = pcall(function() return mw.text.jsonDecode(indexContent) end)
if not success or not data then
return "Error: Invalid JSON data."
end
-- Extract and return the "message" field
return data.message or "Error: 'message' field not found in JSON."
end
return p