Module:YouTubeSubscribers: Difference between revisions
NeonWabbit (talk | contribs) Created page with "POINT_IN_TIME_PID = "P585" YT_CHAN_ID_PID= "P2397" SUB_COUNT_PID = "P8687" local p = {} -- taken from https://en.wikipedia.org/wiki/Module:Wd function parseDate(dateStr, precision) precision = precision or "d" local i, j, index, ptr local parts = {nil, nil, nil} if dateStr == nil then return parts[1], parts[2], parts[3] -- year, month, day end -- 'T' for snak values, '/' for outputs with '/Julian' attached i, j = dateStr:find("[T/]") if i then dateStr..." |
NeonWabbit (talk | contribs) No edit summary |
||
| Line 37: | Line 37: | ||
if i then | if i then | ||
-- year | -- year | ||
parts[index] = tonumber(mw.ustring.gsub(dateStr:sub(ptr, i-1), "^ | 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 | if parts[index] == -0 then | ||
| Line 87: | Line 87: | ||
if aY < bY then | if aY < bY then | ||
return true | return true | ||
elseif aY > bY then | |||
return false | return false | ||
elseif aM < bM then | |||
return true | return true | ||
elseif aM > bM then | |||
return false | return false | ||
elseif aD < bD then | |||
return true | return true | ||
end | end | ||
| Line 116: | Line 108: | ||
end | end | ||
local pointInTime = pointsInTime[1] | local pointInTime = pointsInTime[1] | ||
if pointInTime and pointInTime['datavalue'] and pointInTime['datavalue']['value'] and pointInTime['datavalue']['value']['time'] then | if pointInTime and | ||
pointInTime['datavalue'] and | |||
pointInTime['datavalue']['value'] and | |||
pointInTime['datavalue']['value']['time'] | |||
then | |||
return parseDate(pointInTime['datavalue']['value']['time']) | return parseDate(pointInTime['datavalue']['value']['time']) | ||
end | end | ||
| Line 180: | Line 176: | ||
end | end | ||
if not qid then | if not qid then | ||
local e = nil | |||
return e | |||
end | end | ||
local e = mw.wikibase.getEntity(qid) | local e = mw.wikibase.getEntity(qid) | ||
assert(e, "No such item found: " .. qid) | |||
return e | return e | ||
end | end | ||
| Line 194: | Line 189: | ||
if #chanIds == 1 then | if #chanIds == 1 then | ||
local chan = chanIds[1] | local chan = chanIds[1] | ||
if chan and chan["mainsnak"] and chan["mainsnak"]["datavalue"] and chan["mainsnak"]["datavalue"]["value"] then | if chan and | ||
chan["mainsnak"] and | |||
chan["mainsnak"]["datavalue"] and | |||
chan["mainsnak"]["datavalue"]["value"] | |||
then | |||
return chan["mainsnak"]["datavalue"]["value"] | return chan["mainsnak"]["datavalue"]["value"] | ||
end | end | ||
| Line 208: | Line 207: | ||
function p.date( frame ) | function p.date( frame ) | ||
local e = getEntity(frame) | local e = getEntity(frame) | ||
assert(e, "No qid found for page. Please make a Wikidata item for this article") | |||
local chanId = getBestYtChanId(e) | 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) | local s = newestMatching(e, SUB_COUNT_PID, YT_CHAN_ID_PID, chanId) | ||
if s then | if s then | ||
| Line 243: | Line 241: | ||
-- the most up to date number of subscribers | -- the most up to date number of subscribers | ||
function p.subCount( frame ) | function p.subCount( frame ) | ||
local subCount = nil | |||
local e = getEntity(frame) | local e = getEntity(frame) | ||
if not e then | |||
subCount = -424 | |||
return tonumber(subCount) | |||
end | |||
local chanId = getBestYtChanId(e) | local chanId = getBestYtChanId(e) | ||
if chanId then | if chanId then | ||
local s = newestMatching(e, SUB_COUNT_PID, YT_CHAN_ID_PID, chanId) | 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 | 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'] | subCount = s['mainsnak']["datavalue"]['value']['amount'] | ||
end | end | ||
else | else | ||
subCount = -404 | |||
end | end | ||
if subCount then | if subCount then | ||
return tonumber(subCount) | return tonumber(subCount) | ||
else | else | ||
subCount = -412 | |||
return tonumber(subCount) | |||
end | end | ||
end | end | ||
| Line 264: | Line 272: | ||
local status, obj = pcall(p.subCount, frame) | local status, obj = pcall(p.subCount, frame) | ||
if status then | if status then | ||
return frame:expandTemplate{title="Format price", args = {obj}} | if obj >= 0 then | ||
return frame:expandTemplate{title="Format price", args = {obj}} | |||
else | |||
return obj | |||
end | |||
else | else | ||
return returnError(frame, obj) | return returnError(frame, obj) | ||