Module:YouTubeSubscribers: Difference between revisions

Content deleted Content added
arr error
m Improve readability of long, compound if-statements
 
(45 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 list of statements find the newest one with a matching qual
function newestMatching(claims, chanId)
function newestMatchingStatement(statements, qual, targetQualValue)
--if v['qualifiers'] and v['qualifiers'][POINT_IN_TIME_PID] then
local newestStatement = nil
local newestStatementYr = nil
local newestStatementMo = nil
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
if #quals == 1 then
local qual = quals[1]
if qual['datavalue'] and qual['datavalue']['value'] then
local qualValue = qual['datavalue']['value']
if qualValue == targetQualValue then
local targetYr, targetMo, targetDay = getClaimDate(v)
if targetYr then
local older = datePrecedesDate(targetYr, targetMo, targetDay, newestStatementYr, newestStatementMo, newestStatementDay)
if older == nil or not older then
newestStatementYr, newestStatementMo, newestStatementDay = targetYr, targetMo, targetDay
newestStatement = v
end
end
end
end
end
end
end
return newestStatement
end
 
-- for a given property and qualifier pair returns the newest statement that matches
function p.date( frame )
function newestMatching(e, prop, qual, targetQualValue)
local qid = frame.args["qid"]
-- first check the best statements
if not qid then
local statements = e:getBestStatements(prop)
qid = mw.wikibase.getEntityIdForCurrentPage()
local newestStatement = newestMatchingStatement(statements, qual, targetQualValue)
if newestStatement then
return newestStatement
end
-- try again with all statements if nothing so far
local e = mw.wikibase.getEntity(qid)
statements = e:getAllStatements(prop)
local chanIds = e:getBestStatements(YT_CHAN_ID_PID)
newestStatement = newestMatchingStatement(statements, qual, targetQualValue)
local firstChanId = chanIds[1]["mainsnak"]["datavalue"]["value"]
if newestStatement then
return newestStatement
local subCounts = e:getBestStatements(SUB_COUNT_PID)
end
local subCount = nil
return nil
for k, v in pairs(subCounts) do
if v['qualifiers'] and v['qualifiers'][YT_CHAN_ID_PID] then
local yt_qualifier = v['qualifiers'][YT_CHAN_ID_PID]
return getClaimDate(v)
end
end
end
 
function p.subCountgetEntity ( frame )
local qid = nil
if frame.args["qid"] then
qid = frame.args["qid"]
end
if not qid then
qid = mw.wikibase.getEntityIdForCurrentPage()
end
if not qid then
local e = nil
return e
end
local e = mw.wikibase.getEntity(qid)
assert(e, "No such item found: " .. qid)
local subCount = nil
return e
end
 
-- find the channel ID we are going to be getting the sub counts for
function getBestYtChanId(e)
local chanIds = e:getBestStatements(YT_CHAN_ID_PID)
if #chanIds == 1 then
local firstChanIdchan = chanIds[1]["mainsnak"]["datavalue"]["value"]
if chan and
chan["mainsnak"] and
local subCounts = e:getBestStatements(SUB_COUNT_PID)
chan["mainsnak"]["datavalue"] and
chan["mainsnak"]["datavalue"]["value"]
then
return chan["mainsnak"]["datavalue"]["value"]
end
end
return nil
end
 
function returnError(frame, eMessage)
for k, v in pairs(subCounts) do
return frame:expandTemplate{ title = 'error', args = { eMessage } } .. "[[Category:Pages with YouTubeSubscribers module errors]]"
if v['qualifiers'] and v['qualifiers'][YT_CHAN_ID_PID] then
end
local yt_qualifier = v['qualifiers'][YT_CHAN_ID_PID]
 
if yt_qualifier[1]['datavalue']['value'] == firstChanId then
-- the date of the current YT subscriber count
subCount = v["mainsnak"]["datavalue"]["value"]["amount"]
function p.date( frame )
end
local e = getEntity(frame)
end
assert(e, "No qid found for page. Please make a Wikidata item for this article")
end
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 s = newestMatching(e, SUB_COUNT_PID, YT_CHAN_ID_PID, chanId)
if s then
local yt_year, yt_month, yt_day = getClaimDate(s)
if not yt_year then
return nil
end
local dateString = yt_year .. "|"
-- construct YYYY|mm|dd date string
if yt_month and yt_month ~= 0 then
dateString = dateString .. yt_month .. "|"
-- truncate the day of month
--if yt_day and yt_day ~= 0 then
-- dateString = dateString .. yt_day
--end
end
return frame:expandTemplate{title="Format date", args = {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 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
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
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
end
if subCount then
return tonumertonumber(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