Модуль:Песочница/IEPCBM

Материал из Википедии — свободной энциклопедии
Перейти к навигации Перейти к поиску
Документация

Examples[править код]

Code Result
{{#invoke:Песочница/IEPCBM|extract|PewDiePie|timestamp}}
1563760525
{{#invoke:Песочница/IEPCBM|extract|PewDiePie|subs}}
97932938
{{#invoke:Песочница/IEPCBM|extract|PewDiePie|views}}
22331413804
{{#invoke:Песочница/IEPCBM|extract|PewDiePie|timestamp|processed|%B %Y}}
июль 2019
{{#invoke:Песочница/IEPCBM|extract|PewDiePie|timestamp|processed|%b %Y}}
июл 2019
{{#invoke:Песочница/IEPCBM|extract|PewDiePie|timestamp|processed|%Y-%m-%d %H:%M:%S}}
2019-07-22 01:55:25
{{#invoke:Песочница/IEPCBM|extract|PewDiePie|timestamp|processed}}
Mon Jul 22 01:55:25 2019
{{#invoke:Песочница/IEPCBM|extract|PewDiePie|subs|processed}}
97,932 млн
{{#invoke:Песочница/IEPCBM|extract|PewDiePie|views|processed}}
22,331 млрд
-- Config data
---- Source base
local dataPath = "Sandbox/IEPCBM Bot/test channel data/"

---- Postfixes
local pfThousands = " тыс."
local pfMillions  = " млн"
local pfBillions  = " млрд"

---- Decimal separator
local decSeparator = ","

---- Round range
local roundD = 3

---- Localization
local localization = true

---- Has localization language two byted chars. Only 1 byted or 2 byted chars :P
local has2BytedChars = true

---- Months in localization language
local monthsLoc = {
	"январь",
	"февраль",
	"март",
	"апрель",
	"май",
	"июнь",
	"июль",
	"август",
	"сентябрь",
	"октябрь",
	"ноябрь",
	"декабрь"
}

function locDate (strFormat, ts)
	local monthN = 0
	local monthStr = ""
	if strFormat ~= nil then
		if strFormat:find("%%B") or strFormat:find("%%b") then
			strFormat = strFormat:gsub("%%B", "%%%%B")
			strFormat = strFormat:gsub("%%b", "%%%%b")
			monthN = tonumber(os.date("%m", ts))
			monthStr = monthsLoc[monthN]
		end
	end
	return tostring(os.date(strFormat, ts):gsub("%%B", monthStr):gsub("%%b", monthStr:sub(1,3*(has2BytedChars and 2 or 1))))
end

local M = {}

function M.extract(frame)
	local table = mw.ext.data.get(dataPath .. frame.args[1] .. ".tab")
	local rawCount = 0
	local roundedCount = 0
	local postfix = ""
	if (frame.args[2] == "timestamp") then
		rawCount = table["data"][#table["data"]][1]
	elseif (frame.args[2] == "subs") then
		rawCount = table["data"][#table["data"]][2]
    elseif (frame.args[2] == "views") then
		rawCount = table["data"][#table["data"]][3]
	end
	if frame.args[3] == "processed" then
		if frame.args[2] == "timestamp" then
			return localization and locDate(frame.args[4], rawCount) or os.date(frame.args[4], rawCount)
		end
		if rawCount>10^9 then
			postfix = pfBillions
			roundedCount = rawCount / (10^(9-roundD))
		elseif rawCount>10^6 then
			postfix = pfMillions
			roundedCount = rawCount / (10^(6-roundD))
		elseif rawCount>10^3 then
			postfix = pfThousands
			viewCount = rawCount / (10^(3-roundD))
		end
		roundedCount = math.floor(roundedCount)
		roundedCount = roundedCount / 10^roundD
		return tostring(roundedCount):gsub("%.", decSeparator) .. postfix
	else
		return rawCount
	end
	return 0
end
return M