Module:Sandbox/Jc86035/2

local p = {}

-- CHARTS TABLE – Wikidata entity IDs for each chart. local charts = { -- AUSTRALIA ['ARIA SINGLES CHART'] = 'Q30916704', ['ARIA SINGLES'] = 'Q30916704', ['AU-SINGLES'] = 'Q30916704', ['ARIA ALBUMS CHART'] = 'Q30916708', ['ARIA ALBUMS'] = 'Q30916708', ['AU-ALBUMS'] = 'Q30916708',

-- CANADA ['CANADIAN HOT 100'] = 'Q472737', ['CA100'] = 'Q472737', ['CA-SINGLES'] = 'Q472737', ['CANADIAN ALBUMS CHART'] = 'Q680355', ['CANADIAN ALBUMS'] = 'Q680355', ['CA-ALBUMS'] = 'Q680355',

-- IRELAND ['IRISH SINGLES CHART'] = 'Q1056800', ['IRISH SINGLES'] = 'Q1056800', ['IE-SINGLES'] = 'Q1056800', ['IRISH ALBUMS CHART'] = 'Q1094973', ['IRISH ALBUMS'] = 'Q1094973', ['IE-ALBUMS'] = 'Q1094973',

-- NEW ZEALAND ['NZ TOP 40 SINGLES'] = 'Q1407678', ['NZ SINGLES'] = 'Q1407678', ['NZ-SINGLES'] = 'Q1407678', ['NZ TOP 40 ALBUMS'] = 'Q55767591', ['NZ-ALBUMS'] = 'Q55767591', ['NZ-ALBUMS'] = 'Q55767591',

-- UNITED KINGDOM ['UK SINGLES CHART'] = 'Q193259', ['UK SINGLES'] = 'Q193259', ['UK-SINGLES'] = 'Q193259', ['GB-SINGLES'] = 'Q193259', ['UK ALBUMS CHART'] = 'Q719415', ['UK ALBUMS'] = 'Q719415', ['GB-ALBUMS'] = 'Q719415',

-- UNITED STATES ['BILLBOARD HOT 100'] = 'Q180072', ['BILLBOARD 100'] = 'Q180072', ['BB100'] = 'Q180072', ['US-SINGLES'] = 'Q180072', ['BILLBOARD 200'] = 'Q188819', ['BB200'] = 'Q188819', ['US-ALBUMS'] = 'Q188819', }

local getArgs = require('Module:Arguments').getArgs

local function makeInvokeFunction(funcName) -- makes a function that can be returned from #invoke, using -- Module:Arguments. return function (frame) local args = getArgs(frame, {parentFirst = true}) return p[funcName](args, frame) end end

p._main = function(args, frame) local chart = mw.ustring.upper(args[1]) local qid = args[2] if charts[chart] then chart = charts[chart] end local result = '–' local highest, references local entity = mw.wikibase.getEntity(qid) if entity and entity.claims then local data = entity.claims['P2291'] if data and data[1] then for i, v in ipairs(data) do				if v.mainsnak.datavalue.value.id == chart then local tmp = tonumber(v.qualifiers.P1352[1].datavalue.value.amount) if tmp and (not highest or tmp < highest) then highest = tmp references = i					end end end references = references and data[references].references end end result = highest and tostring(highest) or result return result end

p.main = makeInvokeFunction('_main')

return p