How Can We Help?
You are here:
< Back
require('strict')
local p = {}
local Date = require('Module:Date')._Date
local mm = require('Module:Math')

local list = require('Module:Sandbox/Jts1882/Covid/data')
local list2 = {
				{ "Austria", "Q86847911", "Q40"},
				{ "Belgium", "Q84446340", "Q31",}, 
                { "Bahrain", "Q87070999", "Q398" },
                { "Bangladesh", "Q87540454", "Q902" },
                { "Czech Republic", "Q86919406", "Q213" },
			--	{ "Poland", "Q87250695", "Q36" },
			    { "Spain", "Q84166704", "Q29" },

}

p.main = function(frame)
	local output = "{|\n"
	local getData = false
	local output = '{| class="wikitable sortable" \n'
                .. "! Country !! Cases !! Deaths !! Population !! Cases/million pop !! Deaths/million pop "
	for k,v in pairs(list) do
		-- code for getting population data in blocks
		--if v[1] == "Afghanistan" then getData = true end -- start from country X
--		if v[1] == "Germany" then break end           -- end with country Y
		getData = true
		if getData then
			local qid = v[2] -- "Q84446340" -- Belgium
	    	
	    	--output = output .. v[1] .. ": " .. p.getWikidata(v) .. "<br/>"
	    	output = output   .. p.getWikidata(v) 
	    	
	    	-- line to get population from wikidata in blocks
	    	--output = output .. '{ "' .. v[1] .. '", "' .. v[2] .. '", "' .. v[3] .. '", ' .. p.getWikidataPopulation(v[3]).. ' },<br/>'
    	end
	end
		output = output .. "\n|}"
	return output	
end
p.getWikidata =function(v)
	
	local countryName = v[1]
	local covidQid    = v[2]
	local countryQid  = v[3]
	local population  = v[4]
	
    local value = "EMPTY"
    local item

	local wd = {}
	local covidCases
	local covidDeaths
	local dateCases
	local dateDeaths

if 1==1 then
	if covidQid and mw.wikibase.isValidEntityId( covidQid ) then -- valid id
    	
    	item = mw.wikibase.getEntity(covidQid)
        if not item then return "Covid Qid not found" end 

 
    	local i= 1
    	-- get cases 
    	while item:getBestStatements('P1603')[i]  do
    		local timeStamp 
    	    local statements = item:getBestStatements('P1603')[i]
    	    covidCases = statements.mainsnak.datavalue.value.amount 
    	    if statements.qualifiers and statements.qualifiers['P585'] and statements.qualifiers['P585'][1]['datavalue'] then
	   	    	timeStamp = statements.qualifiers['P585'][1]['datavalue']['value']['time'] --point in time
    	    end
    	    --output = covidCases
    	    
    	    dateCases = "missing"
    	    if timeStamp  then 
    	    	dateCases = Date(timeStamp):text("dmy") or "missing"
    	    end	
    	    i=i+1
    	end
        --output =  (covidCases or "n/a") ..' (' .. (dateCases or "missing date") .. ')' 
    	
    	-- get deaths
    	i=1
    	while item:getBestStatements('P1120')[i]  do
    		local timeStamp 
    	    local statements = item:getBestStatements('P1120')[i]
    	    covidDeaths = statements.mainsnak.datavalue.value.amount 
    	    if statements.qualifiers and statements.qualifiers['P585'] and statements.qualifiers['P585'][1]['datavalue'] then
	   	    	timeStamp = statements.qualifiers['P585'][1]['datavalue']['value']['time'] --point in time
    	    end
    	    
    	    
    	    if timeStamp  then 
    	    	dateDeaths = Date(timeStamp):text("dmy") or "missing"
    	    end
    	    i=i+1
    	end
     	--output = output .. " | " .. (covidDeaths or "n/a") .. ' (' .. (dateCases or "missing date") .. ')'
	end
end	
if 1==2 then 
    local statements -- don't want a lot of large local objects
    if countryQid and mw.wikibase.isValidEntityId( countryQid ) then -- valid id

    	--item = mw.wikibase.getEntity(countryQid)
        --if not item then return "Country Qid not found" end 

    	-- get population 
    	local i=1
    	statements = nil
     	statements  = mw.wikibase.getBestStatements( countryQid, 'P1082' )
    	i = 1 --#statements 
    	
    	while statements[i]  do
    	--while item:getBestStatements('P1082')[i]  do
    		local timeStamp 
    	    local statement = statements[i] --item:getBestStatements('P1082')[i]
    	    population = statement.mainsnak.datavalue.value.amount
    	    if statement.qualifiers and statement.qualifiers['P585'] and statement.qualifiers['P585'][1]['datavalue'] then
	   	    	timeStamp = statement.qualifiers['P585'][1]['datavalue']['value']['time'] --point in time
    	    end

    	    datePopulation = "missing"
    	    if timeStamp  then 
    	    	datePopulation = Date(timeStamp):text("dmy") or "missing"
    	    end	
    	    i=i+1
    	end
    end    
end        
        
    --[[  
    local output = "Cases: "      .. (covidCases or "n/a")  .. ' (' .. (dateCases or "missing date") .. ')' 
              .. ". Deaths: "     .. (covidDeaths or "n/a") .. ' (' .. (dateDeaths or "missing date") .. ')'
              .. ". Population: " .. (population or "n/a")  -- .. ' (' .. (datePopulation or "missing date") .. ')' 
    ]]
    
    
    local output = '\n|-\n|' .. countryName 
                .. '||' .. (covidCases or "n/a")  .. ' (' .. (dateCases or "missing date") .. ')'
                .. '||' .. (covidDeaths or "n/a") .. ' (' .. (dateDeaths or "missing date") .. ')'
                .. '||' .. (population or "n/a")

    if covidCases == nil then covidCases = 0.001 end
    if covidDeaths == nil then covidDeaths = 0.001 end
    
    local output = output
                .. '||' .. mm._precision_format(covidCases/population*1000000 ,0)  
                .. '||' .. mm._precision_format(covidDeaths/population*1000000 ,0) 
    




    return output or "no value found"
        
end
p.getWikidataPopulation =function(countryQid)

	local population
	local datePopulation
    local statements -- don't want a lot of large local objects
    if countryQid and mw.wikibase.isValidEntityId( countryQid ) then -- valid id

    	--item = mw.wikibase.getEntity(countryQid)
        --if not item then return "Country Qid not found" end 

    	-- get population 
    	local i=1
    	statements = nil
     	statements  = mw.wikibase.getBestStatements( countryQid, 'P1082' )
    	i = 1 --#statements 
    	
    	while statements[i]  do
    	--while item:getBestStatements('P1082')[i]  do
    		local timeStamp 
    	    local statement = statements[i] --item:getBestStatements('P1082')[i]
    	    population = statement.mainsnak.datavalue.value.amount
    	    if statement.qualifiers and statement.qualifiers['P585'] and statement.qualifiers['P585'][1]['datavalue'] then
	   	    	timeStamp = statement.qualifiers['P585'][1]['datavalue']['value']['time'] --point in time
    	    end

    	    datePopulation = "missing"
    	    if timeStamp  then 
    	    	datePopulation = Date(timeStamp):text("dmy") or "missing"
    	    end	
    	    i=i+1
    	end
    end    
    return (population or "n/a")  

end

return p
Categories
Table of Contents