Created
December 31, 2020 03:07
-
-
Save AMD-NICK/ea2bd29d9db782fadd456865e4ea770c to your computer and use it in GitHub Desktop.
Скрипт-конвертер лога Python скрипта с поста https://blog.amd-nick.me/telegram-online-chart/ в csv формат для Google Data Studio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local function parseRecord(str) | |
local psc = {str:match("^.(%d%d%d%d%-%d%d%-%d%d) @ (%d%d:%d%d:%d%d): User went (%w+)(.*)%.")} | |
if psc[4] then | |
local ts = dateToStamp(psc[1] .. " " .. psc[2]) + 3600 * 2 | |
local online = psc[3] == "online" | |
local quick = #psc[4] > 0 | |
return ts, online, quick | |
end | |
end | |
local QUICK_SESSION_TIME = 10 -- avg 10 sec. Зашел-вышел | |
local function log_to_csv(log) | |
local newlog = [["date_start", "session_time"]] | |
local last_online = 0 | |
for line in log:lines() do | |
local ts, online, quick_online = parseRecord(line) | |
if quick_online then -- зашел-вышел | |
last_online = ts - QUICK_SESSION_TIME | |
newlog = newlog .. "\n" .. DateTime(last_online) .. ", " .. QUICK_SESSION_TIME | |
elseif online then -- зашел в сеть | |
last_online = ts | |
else -- offline. End of session | |
newlog = newlog .. "\n" .. DateTime(last_online) .. ", " .. ts - last_online | |
end | |
end | |
return newlog | |
end | |
local log = [[ | |
~2020-12-21 @ 21:14:36: User went online. | |
=2020-12-21 @ 21:14:56: User went offline. | |
=2020-12-21 @ 21:16:34: User went offline after being online for short time. | |
]] | |
local newlog = log_to_csv(log) | |
file.Write("bodya_log.txt", newlog) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment