Accessing the web using Lua
So there are a couple of tools I used to access the web from Lua that I wanna go through first.
- LuaRocks LuaRocks is defined as a tool that allows you to install Lua modules as self-contained packages called "rocks", which also contain version dependency information (see: LuaRocks.org). After installation it's as easy as
- LuaSocket According to the website LuaSocket.luaforge.net "LuaSocket is a Lua extension library that is composed by two parts: a C core that provides support for the TCP and UDP transport layers, and a set of Lua modules that add support for functionality commonly needed by applications that deal with the Internet." Sound perfect for the use case of accessing the web from a Lua script.
- Twitter API Twitter is a famous tool to exchange 140 characters long messages. I offers a nice API that will be useful for testing the web access. In particular the paragrph about using A command line is all you need to use the Twitter API is of interest.
luarocks install [name of luarock]. There are more functions that will be listed after just typing
luarockson the command line.
Use LuaRocks to install LuaSocket
LuaRocks makes this step really easy. All you need to do is type
on the command line to install the LuaSocket module to your system. To test if everything worked, use the lua shell and type
luarocks install luasocket
-- Use the luarocks module to load from the 'rocks' module
-- Load the specific rock: 'socket'
Use LuaSocket to access the Twitter API
Now that LuaSocket is installed properly we can use it to access the web. For that we first have to load the required modules:
-- use luarocks to load the modules
-- load the http module
local io = require("io")
local http = require("socket.http")
local ltn12 = require("ltn12")
httpnamespace which makes it easier to use later.
As for every access to a web page you need an URL, we need to construct one. The URL is a simple string that will be handed over to the http module. We just have to get the user login information in order to access the timeline of the user's friends:
print("Enter username: ")
username = io.read()
print("Enter password: ")
password = io.read()
authentication = username .. ":" .. password
myurl = "http://" .. authentication .. "@api.twitter.com/1/statuses/friends_timeline.xml"
All there is left to do now is to hand over the URL to the http module to perform the request:
url = myurl,
sink = ltn12.sink.file(io.stdout)
This should give you the output of your timeline in form of a XML file. There are more formats available, such as JSON or RSS. I commited the lua file to my Git repository. I hope this will help others to get started using Lua on the web and / or using the Twitter API. There are a lot of chances in both of them. My plan is to use this tiny lua script and extend it to use it as a "full" twitter client ;-) ... Well a command line twitter client using Lua. Next step is to send a statud update from lua to twitter as so far this is only a request to retreive data. I also need to get into more depth using the LuaSocket module. Stay tuned if you are interested and please do leave a comment and / or feedback. Thanks!