Module:Sandbox/Jc86035/2

From blackwiki
< Module:Sandbox
Revision as of 07:41, 14 August 2018 by blackwiki>Jc86035 (ce)
Jump to navigation Jump to search

Documentation for this module may be created at Module:Sandbox/Jc86035/2/doc

local p = {}

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

	-- 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