Created
February 11, 2021 08:17
-
-
Save diazvictor/66837a007ea60c9209aeb06e6257d29c to your computer and use it in GitHub Desktop.
How to download RSS files with and display their content with Lua
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
-- This is a port of GistsScrapper.go <https://gist.github.com/M1que4s/b92073803b658c207fbdfaebc10a1512> | |
local curl = require('cURL') -- <https://github.com/Lua-cURL/Lua-cURLv3> | |
local feedparser = require('feedparser') -- <https://github.com/slact/lua-feedparser> | |
file_exist = function (file) | |
local file_found = io.open(file, "r") | |
if (file_found == nil) then | |
return false | |
end | |
return true | |
end | |
function GetGists(user) | |
local url = ('https://gist.github.com/%s.atom'):format(user) | |
local filename = ('/tmp/%s.atom'):format(user) | |
if not file_exist(filename) then | |
-- HTTP Get | |
local get_gists = curl.easy ({ | |
url = url, | |
httpheader = {}, | |
}) | |
get_gists:perform({writefunction = assert(io.open(filename, "w+b"))}) | |
get_gists:close() | |
end | |
local gists = io.open(filename) | |
local res = gists:read('*a') | |
gists:close() | |
if res == 'Not Found' then | |
print('No gists found, this can happen due to connection problems.') | |
return false | |
end | |
-- @TODO: There is a way to do this without downloading with cURL, but I don't have the library at the moment. | |
local res = assert(feedparser.parse(res)) | |
if #res.entries ~= 0 then | |
for i, post in pairs(res.entries) do | |
print(('0%d. \27[1mDescription:\27[0m %s'):format( | |
i, post.title | |
)) | |
end | |
else | |
print(('User \'%s\' has no gists.'):format(user)) | |
end | |
end | |
GetGists(arg[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment