Module:YouTubeSubscribers: Difference between revisions

Content deleted Content added
No edit summary
m Improve readability of long, compound if-statements
 
(20 intermediate revisions by 4 users not shown)
Line 37:
if i then
-- year
parts[index] = tonumber(mw.ustring.gsub(dateStr:sub(ptr, i-1), "^\%+(.+)$", "%1"), 10) -- remove '+' sign (explicitly give base 10 to prevent error)
 
if parts[index] == -0 then
Line 87:
if aY < bY then
return true
ifelseif aY > bY then
end
 
if aY > bY then
return false
ifelseif aM < bM then
end
 
if aM < bM then
return true
ifelseif aM > bM then
end
 
if aM > bM then
return false
ifelseif aD < bD then
end
 
if aD < bD then
return true
end
Line 113 ⟶ 105:
if #pointsInTime ~= 1 then
-- be conservative in what we accept
error("Encountered a statement with zero or multiple point in time (P85) qualifiers. Please add or remove point in time information. so each statement has exactly one")
return nil
end
local pointInTime = pointsInTime[1]
if pointInTime and
pointInTime['datavalue'] and
pointInTime['datavalue']['value'] and
pointInTime['datavalue']['value']['time']
then
return parseDate(pointInTime['datavalue']['value']['time'])
end
end
return nil
Line 152 ⟶ 146:
end
end
end
return newestStatement
end
Line 182 ⟶ 176:
end
if not qid then
local e = nil
error("No qid found for page. Please make a Wikidata item for this article")
return e
end
local e = mw.wikibase.getEntity(qid)
error assert(e, "No such item found: " .. qid)
if not e then
error("No such item found: " .. qid)
end
return e
end
Line 196 ⟶ 189:
if #chanIds == 1 then
local chan = chanIds[1]
if chan and
chan["mainsnak"] and
chan["mainsnak"]["datavalue"] and
chan["mainsnak"]["datavalue"]["value"]
then
return chan["mainsnak"]["datavalue"]["value"]
end
end
return nil
end
 
function returnError(frame, eMessage)
return frame:expandTemplate{ title = 'error', args = { eMessage } } .. "[[Category:Pages with YouTubeSubscribers module errors]]"
end
 
Line 206 ⟶ 207:
function p.date( frame )
local e = getEntity(frame)
error assert(e, "No qid found for page. Please make a Wikidata item for this article")
local chanId = getBestYtChanId(e)
error assert(chanId, "Could not find a single best YouTube channel ID for this item. Add a YouTube channel ID or set the rank of one channel ID to be preferred")
if not chanId then
error("Could not find a single best YouTube channel ID for this item. Add a YouTube channel ID or set the rank of one channel ID to be preferred")
end
local s = newestMatching(e, SUB_COUNT_PID, YT_CHAN_ID_PID, chanId)
if s then
Line 225:
--end
end
return "{frame:expandTemplate{title="Format date|", ..dateStringargs ..= "{yt_year, yt_month, yd_day}}"
end
error("Could not find a date for YouTube subscriber information. Is there a social media followers statement (P8687) qualified with good values for P585 and P2397?")
end
 
function p.dateNice( frame )
local status, obj = pcall(p.date, frame)
if not chanIdstatus then
return obj
else
return returnError(frame, obj)
end
error("Could not find a date for YouTube subscriber information.")
end
 
-- the most up to date number of subscribers
function p.subCount( frame )
local e = getEntity(frame)
local subCount = nil
local e = getEntity(frame)
if not e then
subCount = -424
return tonumber(subCount)
end
local chanId = getBestYtChanId(e)
if chanId then
local s = newestMatching(e, SUB_COUNT_PID, YT_CHAN_ID_PID, chanId)
if s and
if s and s["mainsnak"] and s['mainsnak']["datavalue"] and s['mainsnak']["datavalue"]["value"] and s['mainsnak']["datavalue"]['value']['amount'] then
s["mainsnak"] and
s['mainsnak']["datavalue"] and
s['mainsnak']["datavalue"]["value"] and
s['mainsnak']["datavalue"]['value']['amount']
then
subCount = s['mainsnak']["datavalue"]['value']['amount']
end
else
subCount = -404
error("Could not find a single best YouTube channel ID for this item. Add a YouTube channel ID or set the rank of one channel ID to be preferred")
end
if subCount then
return tonumber(subCount)
else
subCount = -412
error("Found an associated YouTube channel ID but could not find a most recent value for social media followers (i.e. P8687 qualified with P585 and P2397)")
return tonumber(subCount)
end
end
 
function p.subCountNice( frame )
local subCountstatus, obj = pcall(p.subCount(, frame)
if status then
return "{{Format price|" .. subCount .. "}}"
if obj >= 0 then
return frame:expandTemplate{title="Format price", args = {obj}}
else
return nilobj
end
else
return returnError(frame, obj)
end
end
 
return p