write the program

This commit is contained in:
Izaya 2020-10-30 19:03:07 +11:00
parent 9524ec33f3
commit 2db6448e78
2 changed files with 66 additions and 1 deletions

View File

@ -1,3 +1,13 @@
# mhwnews
Script to convert Monster Hunter World's news.json into an RSS feed
## Dependencies
- lua
- lua-cjson
- curl
## Usage
lua mhwnews.lua output.rss.xml

55
mhwnews.lua Normal file
View File

@ -0,0 +1,55 @@
#!/usr/bin/env lua
local json = require "cjson"
local tA = {...}
local mhwurl = tA[2] or "https://www.monsterhunter.com/world-iceborne/pc/assets/data/us/news.json"
local title = "Unofficial Monster Hunter World News"
local jsf = os.tmpname()
os.execute(string.format("curl -so '%s' '%s'",jsf,mhwurl))
local f = io.open(jsf,"rb")
if not f then error("unable to open file for reading: "..jsf) end
local c = f:read("*a")
f:close()
os.execute(string.format("rm '%s'",jsf))
local jt = json.decode(c)
local f = io.open(tA[1],"wb")
if not f then error("unable to open file for writing: "..tA[1]) end
f:write(string.format([[<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>%s</title>
<link>https://www.monsterhunter.com/world-iceborne/pc/us/</link>
<lastBuildDate>%s</lastBuildDate>
<ttl>1440</ttl>
]], title, os.date("%a, %d %b %Y %H:%M:%S %z")))
for k,v in pairs(jt.topics) do
if v.link:sub(1,8) ~= "https://" then
v.link = "https://www.monsterhunter.com/" .. v.link
end
local p = io.popen(string.format("curl -sI '%s' | grep last-modified",v.link))
local c = p:read("*a")
p:close()
mdate = c:match(": ([^\r\n]+)")
if mdate then
f:write(string.format([[ <item>
<title>%s</title>
<description><img src="https://www.monsterhunter.com/world-iceborne/pc/%s"></img></description>
<link>%s</link>
<guid>%s</guid>
<pubDate>%s</pubDate>
</item>
]], v.text, v.img, v.link, v.link, mdate))
else
f:write(string.format([[ <item>
<title>%s</title>
<description><img src="https://www.monsterhunter.com/world-iceborne/pc/%s"></img></description>
<link>%s</link>
<guid>%s</guid>
</item>
]], v.text, v.img, v.link, v.link))
end
end
f:write([[</channel>
</rss>]])
f:close()