Skip to content

Instantly share code, notes, and snippets.

@NodusCursorius
Last active February 21, 2020 17:28
Show Gist options
  • Save NodusCursorius/b77134a14fa9b032577c7644efe7d01a to your computer and use it in GitHub Desktop.
Save NodusCursorius/b77134a14fa9b032577c7644efe7d01a to your computer and use it in GitHub Desktop.
lua script for parsing faction names out of the journal (unnecessary comments version)
local json = require('json')
local fs = require('fs')
local results_t = {}
-- the options we're parsing for:
--[[
rootTags_t
"Factions":[ { (LOTS OF STUFF) } ]
"Rewards":[ { "Faction":"89 Leonis Republic Party", "Reward":132097 } ]
"StationFaction":{ "Name":"89 Leonis Republic Party" }
"SystemFaction":{ "Name":"89 Leonis Republic Party" }
"VictimFaction":"89 Leonis Republic Party"
]]--
local rootTags_t = {'Factions', 'Rewards', 'StationFaction', 'SystemFaction', 'VictimFaction'}
-- subTags_t are just tags used within rootTags_t, e.g., "StationFaction":{ "Name":
local subTags_t = {'Name', 'Faction'}
local function tableScan_f(input_t, nest_s)
-- nest_s flag: trigger set when a rootTags_t is detected
if type(input_t) == 'table' and nest_s then
for _, v in pairs(input_t) do
for _, b in pairs(subTags_t) do -- iterate over subTags_t options
if v[b] then
results_t[v[b]] = '' -- bingo, add the faction name
end
end
end
elseif type(input_t) == 'table' then
for _, v in pairs(rootTags_t) do -- iterate over rootTags_t options
if input_t[v] then
tableScan_f(input_t[v], v) -- found a rootTags_t, activate nest_s so we can scan recursively
end
end
else
results_t[input_t] = '' -- bingo, whatever survives is a faction name
end
return
end
-- main execution; parse the received journal file listing
local function parse_f(files_t)
for _, file in pairs(files_t) do
local f = io.open(file, 'r')
for line in f:lines() do -- read the file one line at a time
local scrub_t = json.decode(line) -- attempt to convert line to JSON
if scrub_t then -- if successful, continue (error handling)
tableScan_f(scrub_t)
end
end -- so many ends... never meet your heroes
f:close()
end
-- spit it all out into the file '_factionNames.txt'
local f = io.open('_factionNames.txt', "wb")
for k, _ in pairs(results_t) do
f:write(k .. '\n')
end
f:close()
end
-- initial execution; scan the current directory for all files
local files_t = {}
fs.readdir('.', function(_, v)
for _, filename in pairs(v) do
-- store the filename if begins with 'Journal' and ends with '.log'
if string.match(filename, '^Journal.+%log$') then files_t[#files_t + 1] = filename end
end
-- pass off to main execution
parse_f(files_t)
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment