Module:Bluesky RSS Feed Test: Difference between revisions

From NOISZ Wiki
No edit summary
No edit summary
 
(65 intermediate revisions by the same user not shown)
Line 2: Line 2:
local capiunto = require 'capiunto'
local capiunto = require 'capiunto'
local p = {}
local p = {}
function dedupe( list )
local seen = {}
for k, v in ipairs(list) do
if seen[item] then
table.remove(list, k)
end
end
end


function p.main(frame)
function p.main(frame)
Line 19: Line 9:
local links = {}
local links = {}
local searchTerms = {}
local searchTerms = {}
local itemBefore= {}
itemBefore["pubDate"] = ""


-- These are the search terms as defined in the template.
if args.term1 or args.term2 or args.term3 then
if args.term1 or args.term2 or args.term3 then
searchTerms[1] = args.term1
searchTerms[1] = args.term1
searchTerms[2] = args.term2
searchTerms[2] = args.term2
searchTerms[3] = args.term3
searchTerms[3] = args.term3
-- If no search terms, then the search terms array is left blank so it'll match everything.
else  
else  
searchTerms = {""}
searchTerms = {""}
end
end


-- We use externaldata.getWebDate to obtain XML format data from the RSS feed at the URL defined by the user.
local rss = mw.ext.externaldata.getWebData {
local rss = mw.ext.externaldata.getWebData {
     url = args.url
     url = args.url
Line 33: Line 28:
   , format = 'xml'
   , format = 'xml'
}
}
-- We iterate over every post found in the RSS feed, in chronological order - hence ipairs. We need description, publish date AND link to know that it's a post. Additionally, k has to be more than 1 as the very first post to pop up in an RSS feed is the user bio which is not an actual post. The "links" array is a directory of links that have been processed; if it's already been processed, then it won't be added again. This helps for if there's more than one search term.


for k, v in ipairs(rss) do
for k, v in ipairs(rss) do
if v["description"] and v["pubDate"] and v["link"] then
if v["description"] and v["pubDate"] and v["link"] then
links[v] = false
 
-- Search over every search term in the search terms to make sure a post contains that term. If so, we bundle up the information into template form via the frame:expandTemplate method - the "Bluesky RSS Feed Row" template, to be precise - and put it into an incremental array which will later be expanded in full.
 
for a, b in pairs(searchTerms) do
for a, b in pairs(searchTerms) do
if string.find(v["description"],b) and not links[v] then
if string.find(v["description"],b) and links[v["link"]] ~= true then  
table.insert(posts, frame:expandTemplate{title = 'Bluesky RSS Feed Row', args = {description = v["description"], pubDate = v["pubDate"], link=v["link"]}})
-- Here we assign a new key to the links table, so that it can be accessed directly by the above conditions. The key is the post link, which is the one accessible unique attribute of each post.
if k==1 then
itemBefore["pubDate"] = v["pubDate"]
end
links[v["link"]] = true
table.insert(posts, frame:expandTemplate{title = 'Bluesky RSS Feed Row', args = {description = v["description"], pubDate = itemBefore["pubDate"], link=v["link"]}})
end
end
end
end
itemBefore["pubDate"] = v["pubDate"]
end
end
end
end


dedupe(posts)
-- table.concat to convert it from Lua table form to a form the wikitext can use - hence why it was frame:expand and not just raw data being put in.


return table.concat(posts,"")
return table.concat(posts,"")


end
end


return p
return p

Latest revision as of 12:35, 22 November 2025

Documentation for this module may be created at Module:Bluesky RSS Feed Test/doc


local capiunto = require 'capiunto'
local p = {}

function p.main(frame)

local args = frame:getParent().args
local posts = {}
local links = {}
local searchTerms = {}
local itemBefore= {}
itemBefore["pubDate"] = ""

-- These are the search terms as defined in the template.
if args.term1 or args.term2 or args.term3 then
	searchTerms[1] = args.term1
	searchTerms[2] = args.term2
	searchTerms[3] = args.term3
-- If no search terms, then the search terms array is left blank so it'll match everything.
else 
	searchTerms = {""}
end

-- We use externaldata.getWebDate to obtain XML format data from the RSS feed at the URL defined by the user.
local rss = mw.ext.externaldata.getWebData {
    url = args.url
  , data = {pubDate = 'pubDate', description = 'description', link= 'link'}
  , format = 'xml'
}

-- We iterate over every post found in the RSS feed, in chronological order - hence ipairs. We need description, publish date AND link to know that it's a post. Additionally, k has to be more than 1 as the very first post to pop up in an RSS feed is the user bio which is not an actual post. The "links" array is a directory of links that have been processed; if it's already been processed, then it won't be added again. This helps for if there's more than one search term.


for k, v in ipairs(rss) do
	if v["description"] and v["pubDate"] and v["link"] then

-- Search over every search term in the search terms to make sure a post contains that term. If so, we bundle up the information into template form via the frame:expandTemplate method - the "Bluesky RSS Feed Row" template, to be precise - and put it into an incremental array which will later be expanded in full.

		for a, b in pairs(searchTerms) do
			if string.find(v["description"],b) and links[v["link"]] ~= true then 
-- Here we assign a new key to the links table, so that it can be accessed directly by the above conditions. The key is the post link, which is the one accessible unique attribute of each post.
				if k==1 then
					itemBefore["pubDate"] = v["pubDate"]
				end
				links[v["link"]] = true
				table.insert(posts, frame:expandTemplate{title = 'Bluesky RSS Feed Row', args = {description = v["description"], pubDate = itemBefore["pubDate"], link=v["link"]}})
				
			end	
		end
		itemBefore["pubDate"] = v["pubDate"]
	end
end

-- table.concat to convert it from Lua table form to a form the wikitext can use - hence why it was frame:expand and not just raw data being put in.

return table.concat(posts,"")

end

return p