Module:YouTubeSubscribers: Difference between revisions

Content deleted Content added
factor out logic into newestMatching
m Improve readability of long, compound if-statements
 
(38 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
elseif aY > bY then
end
 
if aY > bY then
return false
elseif aM < bM then
end
 
if aM < bM then
return true
elseif aM > bM then
end
 
if aM > bM then
return false
elseif aD < bD then
end
 
if aD < bD then
return true
end
 
return false
end
 
 
function serializeTable(val, name, skipnewlines, depth)
skipnewlines = skipnewlines or false
depth = depth or 0
 
local tmp = string.rep(" ", depth)
 
if name then tmp = tmp .. name .. " = " end
 
if type(val) == "table" then
tmp = tmp .. "{" .. (not skipnewlines and "\n" or "")
 
for k, v in pairs(val) do
tmp = tmp .. serializeTable(v, k, skipnewlines, depth + 1) .. "," .. (not skipnewlines and "\n" or "")
end
 
tmp = tmp .. string.rep(" ", depth) .. "}"
elseif type(val) == "number" then
tmp = tmp .. tostring(val)
elseif type(val) == "string" then
tmp = tmp .. string.format("%q", val)
elseif type(val) == "boolean" then
tmp = tmp .. (val and "true" or "false")
else
tmp = tmp .. "\"[inserializeable datatype:" .. type(val) .. "]\""
end
 
return tmp
end
 
Line 143 ⟶ 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
end
 
-- for a given propertylist andof qualifierstatements pair returnsfind the newest statementone with a thatmatching matchesqual
function newestMatchingnewestMatchingStatement(e, propstatements, qual, targetQualValue)
local statements = e:getBestStatements(prop)
local newestStatement = nil
local newestStatementYr = nil
Line 162 ⟶ 126:
local newestStatementDay = nil
for k, v in pairs(statements) do
if v['rank'] ~= "deprecated" and v['qualifiers'] and v['qualifiers'][qual] then
local quals = v['qualifiers'][qual]
-- should only have one instance of the qualifier on a statement
Line 173 ⟶ 137:
if targetYr then
local older = datePrecedesDate(targetYr, targetMo, targetDay, newestStatementYr, newestStatementMo, newestStatementDay)
mw.logObject(qual)
 
if older == nil or not older then
mw.logObject(v)
 
newestStatementYr, newestStatementMo, newestStatementDay = targetYr, targetMo, targetDay
newestStatement = v
Line 186 ⟶ 146:
end
end
end
return newestStatement
end
 
-- for a given property and qualifier pair returns the newest statement that matches
function newestMatching(e, prop, qual, targetQualValue)
-- first check the best statements
local statements = e:getBestStatements(prop)
local newestStatement = newestMatchingStatement(statements, qual, targetQualValue)
if newestStatement then
return newestStatement
end
-- try again with all statements if nothing so far
statements = e:getAllStatements(prop)
newestStatement = newestMatchingStatement(statements, qual, targetQualValue)
if newestStatement then
return newestStatement
Line 202 ⟶ 176:
end
if not qid then
local e = nil
error("No qid found for page")
return e
end
local e = mw.wikibase.getEntity(qid)
assert(e, "No such item found: " .. qid)
if not e then
error("No such item found: " .. qid)
end
return e
end
Line 216 ⟶ 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
Line 223 ⟶ 200:
end
 
function returnError(frame, eMessage)
return frame:expandTemplate{ title = 'error', args = { eMessage } } .. "[[Category:Pages with YouTubeSubscribers module errors]]"
end
 
-- the date of the current YT subscriber count
function p.date( frame )
local e = getEntity(frame)
assert(e, "No qid found for page. Please make a Wikidata item for this article")
local chanId = getBestYtChanId(e)
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")
local subCountss = e:getBestStatementsnewestMatching(e, SUB_COUNT_PID, YT_CHAN_ID_PID, chanId)
if s then
local subCount = nil
local yt_year, yt_month, yt_day = getClaimDate(s)
for k, v in pairs(subCounts) do
if not yt_year then
if v['qualifiers'] and v['qualifiers'][YT_CHAN_ID_PID] then
return nil
local yt_qualifier = v['qualifiers'][YT_CHAN_ID_PID]
end
local yt_year = nil
local yt_monthdateString = nilyt_year .. "|"
-- construct YYYY|mm|dd date string
local yt_day = nil
yt_year,if yt_month, yt_dayand yt_month ~= getClaimDate(v)0 then
dateString = dateString .. yt_month .. "|"
if not yt_year then
-- truncate the day of month
return nil
--if yt_day and yt_day ~= 0 then
end
local -- dateString = yt_yeardateString .. "|"yt_day
--end
-- construct YYYY|mm|dd date string
end
if yt_month then
return frame:expandTemplate{title="Format date", args = {yt_year, yt_month, yd_day}}
dateString = dateString .. yt_month .. "|"
end
if yt_day then
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?")
dateString = dateString .. yt_day
end
end
return "{{Format date|" ..dateString .. "}}"
end
end
end
 
function p.dateNice( frame )
local status, obj = pcall(p.date, frame)
if status then
return obj
else
return returnError(frame, obj)
end
end
 
-- the most up to date number of subscribers
function p.subCount( frame )
local subCount = nil
local e = getEntity(frame)
if not e then
local subCount = nil
subCount = -424
return tonumber(subCount)
end
local chanId = getBestYtChanId(e)
if chanId then
local bestStatements = newestMatching(e, SUB_COUNT_PID, YT_CHAN_ID_PID, chanId)
if s and
mw.logObject(bestStatement)
s["mainsnak"] and
local subCounts = e:getBestStatements(SUB_COUNT_PID)
s['mainsnak']["datavalue"] and
for k, v in pairs(subCounts) do
if vs['qualifiersmainsnak'] and v['qualifiers'"datavalue"][YT_CHAN_ID_PID"value"] thenand
s['mainsnak']["datavalue"]['value']['amount']
local yt_qualifier = v['qualifiers'][YT_CHAN_ID_PID]
then
if yt_qualifier[1]['datavalue']['value'] == chanId then
subCount = vs["'mainsnak"']["datavalue"]["'value"']["'amount"']
end
else
end
subCount = -404
end
end
if subCount then
return tonumber(subCount)
else
subCount = -412
error("could not find sub count")
return "?"tonumber(subCount)
end
end
 
function p.subCountNice( frame )
local status, obj = pcall(p.subCount, frame)
if status then
if obj >= 0 then
return frame:expandTemplate{title="Format price", args = {obj}}
else
return obj
end
else
return returnError(frame, obj)
end
end