All Projects → leafo → lua-twitter

leafo / lua-twitter

Licence: other
A Lua twitter library that works with OpenResty or LuaSocket

Programming Languages

lua
6591 projects
MoonScript
45 projects
Makefile
30231 projects

Projects that are alternatives of or similar to lua-twitter

docker-lapis
Dockerized Lapis
Stars: ✭ 20 (-31.03%)
Mutual labels:  moonscript, openresty
lua-mailgun
Lua bindings to Mailgun HTTP API
Stars: ✭ 25 (-13.79%)
Mutual labels:  moonscript, openresty
Lapis
A web framework for Lua and OpenResty written in MoonScript
Stars: ✭ 2,621 (+8937.93%)
Mutual labels:  moonscript, openresty
dnmp
docker-compose部署LNMP环境 Nginx/Openresty、MySQL(5.7、8.0、8.1)、PHP7.4(8.0、5.6)、Redis5.0、PHPMyAdmin、Xdebug、RabbitMQ、Nacos
Stars: ✭ 138 (+375.86%)
Mutual labels:  openresty
grasp
Essential NLP & ML, short & fast pure Python code
Stars: ✭ 58 (+100%)
Mutual labels:  twitter-api
lua-resty-http2
The HTTP/2 Protocol (Client Side) Implementation for OpenResty.
Stars: ✭ 73 (+151.72%)
Mutual labels:  openresty
nitter scraper
Scrape Twitter API without authentication using Nitter.
Stars: ✭ 31 (+6.9%)
Mutual labels:  twitter-api
twitter-stream-rs
A Rust library for listening on Twitter Streaming API.
Stars: ✭ 66 (+127.59%)
Mutual labels:  twitter-api
twpy
Twitter High level scraper for humans.
Stars: ✭ 58 (+100%)
Mutual labels:  twitter-api
kuon
🐦 [WIP] Twitter Client Library written in Rust 🦀
Stars: ✭ 47 (+62.07%)
Mutual labels:  twitter-api
spark-twitter-sentiment-analysis
Sentiment Analysis of a Twitter Topic with Spark Structured Streaming
Stars: ✭ 55 (+89.66%)
Mutual labels:  twitter-api
lua-http-digest
Client side HTTP Digest Authentication for Lua
Stars: ✭ 18 (-37.93%)
Mutual labels:  luasocket
kafka-twitter-spark-streaming
Counting Tweets Per User in Real-Time
Stars: ✭ 38 (+31.03%)
Mutual labels:  twitter-api
wired-vpn
WireGuard behind OIDC
Stars: ✭ 21 (-27.59%)
Mutual labels:  openresty
defnet
Defold networking examples
Stars: ✭ 52 (+79.31%)
Mutual labels:  luasocket
Suravi
Suravi is a small distribution of Ravi/Lua 5.3 with batteries such as cjson, lpeglabel, luasocket, penlight, torch7, luv, luaossl
Stars: ✭ 56 (+93.1%)
Mutual labels:  luasocket
spacer
🚀Serverless function platform for Lua
Stars: ✭ 50 (+72.41%)
Mutual labels:  openresty
lapis-bayes
Naive Bayes classifier for use in Lua
Stars: ✭ 26 (-10.34%)
Mutual labels:  moonscript
casper
Yelp's internal caching proxy, powered by Nginx and OpenResty at its core
Stars: ✭ 81 (+179.31%)
Mutual labels:  openresty
stweet
Advanced python library to scrap Twitter (tweets, users) from unofficial API
Stars: ✭ 287 (+889.66%)
Mutual labels:  twitter-api

twitter

test

A Lua library for working with the Twitter API.

This library is designed to work with either LuaSocket or OpenResty's co-sockets via Lapis. If ngx is not in scope then the library will fall back to LuaSocket for network.

luasec is required when using LuaSocket for https communication.

Install

luarocks install https://luarocks.org/manifests/leafo/twitter-dev-1.rockspec

Reference

Before getting started you'll need a consumer key and consumer secret. To get those you'll need to create an app at https://apps.twitter.com.

Application only authentication

There are a few ways to authenticate with the Twitter API. The easiest way is to request without a user context using the cosnumer keys:

local Twitter = require("twitter").Twitter

local twitter = Twitter({
  consumer_key = "XXXXXXX",
  consumer_secret = "ABCABCABACABACACB"
})

This will allow you to run read-only API calls for public information.

If you want to do things like post statuses and upload images using the account that owns the consumer keys then you can generate a access token and secret from https://apps.twitter.com and use them like so:

local Twitter = require("twitter").Twitter

local twitter = Twitter({
  consumer_key = "XXXXXXX",
  consumer_secret = "ABCABCABACABACACB",
  access_token = "abcdefg-123456",
  access_token_secret = "awkeftSECretgwKWARKW"
})

3-Legged OAuth

If you want to add "Sign in with Twitter" then you can use this approach.

Create a new client with your consumer key and secret:

local Twitter = require("twitter").Twitter

local twitter = Twitter({
  consumer_key = "XXXXXXX",
  consumer_secret = "ABCABCABACABACACB"
})

Generate the URL to redirect the user to. (You should redirect their browser to this URL)

local url = twitter:sign_in_with_twitter_url()

After the user completes the sign in at the URL, they'll be redirect to the callback URL on your website that you provided on the Twitter developers page.

The callback URL also some two query parameters attached to it: oauth_token and oauth_verifier. You can use this information to create an access token for the user:

local result = twitter:verify_sign_in_token(oauth_token, oauth_verifier)

The result is an object that looks approximately like this:

{
  "oauth_token": "XXXXXXXX-XXXXXXXXXXXX",
  "oauth_token_secret": "XXXXXXXXXXXXXX",
  "user_id": "123434",
  "screen_name": "itchio",
  "x_auth_expires": "0"
}

You can now use the oauth_token and oauth_token_secret to create a new Twitter client to make requests on their behalf. Use the same consumer_key and consumer_secret as before:

local user_client = Twitter({
  access_token = result.oauth_token,
  access_token_secret = result.oauth_token_secret,

  consumer_key = "XXXXXXX",
  consumer_secret = "ABCABCABACABACACB"
})

user_client:post_status({ status = "Hello world!" })

Methods

All of the following methods are available on an instance of the Twitter class provided by the twitter module.

get_user(opts={})

https://dev.twitter.com/rest/reference/get/users/show

Get information about a user

local user = twitter:get_user({
  screen_name = "moonscript"
})

get_user_timeline(opts={})

https://dev.twitter.com/rest/reference/get/statuses/user_timeline

Get a page of tweets for a user's timeline.

local tweets = twitter:get_user_timeline({
  screen_name = "moonscript",
  include_rts = "0",
})

user_timeline_each_tweet(opts={})

Returns an iterator to get every Tweet available in the API. Calls get_user_timeline repeatedly, updating max_id accordingly.

Will set count to 200 if not specified.

for tweet in twitter:user_timeline_each_tweet({ screen_name = "moonscript" }) do
  print(tweet.text)
end

post_status(opts={})

https://dev.twitter.com/rest/reference/post/statuses/update

Creates a new tweet for the user.

This requires authentication with a user context. You can get an access token for your own account from https://apps.twitter.com/.

local user = assert(twitter:post_status({
  status = "Hello, this my tweet"
}))

post_media_upload(opts={})

Uploads a image or video to Twitter, returns the media object. You can attach the media object to a status update by including the id returned by this call in the post_status method.

local media = assert(twitter:post_media_upload({
  filename = "mything.gif"
}))

assert(twitter:post_status {
  status = "feeling itchy pt. 3",
  media_ids = media.media_id_string
})

Uploading from URL

You can also upload an image directly from URL by passing in the URL of the image to url.

local media = assert(twitter:post_media_upload({
  url: "http://leafo.net/hi.png"
}))

Contact

Author: Leaf Corcoran (leafo) (@moonscript)
Email: [email protected]
Homepage: http://leafo.net
License: MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].