Module:YouTubeSubscribers: Difference between revisions

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..."
 
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), "^\+(.+)$", "%1"), 10)  -- remove '+' sign (explicitly give base 10 to prevent error)
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
end
elseif aY > bY then
 
if aY > bY then
return false
return false
end
elseif aM < bM then
 
if aM < bM then
return true
return true
end
elseif aM > bM then
 
if aM > bM then
return false
return false
end
elseif aD < bD then
 
if 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
error("No qid found for page. Please make a Wikidata item for this article")
local e = nil
return e
end
end
local e = mw.wikibase.getEntity(qid)
local e = mw.wikibase.getEntity(qid)
if not e then
assert(e, "No such item found: " .. qid)
error("No such item found: " .. qid)
end
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)
if not chanId then
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")
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)
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)
local subCount = nil
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  
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")
subCount = -404
end
end
     if subCount then
     if subCount then
     return tonumber(subCount)
     return tonumber(subCount)
     else
     else
     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)")
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)