ExTwitter 

Twitter client library for elixir. It uses <a href=”https://github.com/tim/erlang-oauth/“ target=”_blank”>erlang-oauth</a> to call Twitter’s REST API.
It only supports very limited set of functions yet. Refer to <a href=”https://github.com/parroty/extwitter/blob/master/lib/extwitter.ex” target=”_blank”>lib/extwitter.ex</a> and <a href=”https://github.com/parroty/extwitter/blob/master/test/extwitter_test.exs” target=”_blank”>test/extwitter_test.exs</a> for available functions and examples.
Documentation
Usage
-
Add
extwitterto deps section in themix.exs. -
Use
ExTwitter.configureto setup Twitter’s OAuth authentication paramters. Refer to https://dev.twitter.com/docs for the detail. -
Call functions in ExTwitter module (ex.
ExTwitter.search("test")).
Configuration
The default behaviour is to configure using the application environment:
In config/config.exs, add:
config :ex_twitter, :oauth, [
consumer_key: "",
consumer_secret: "",
access_token: "",
access_token_secret: ""
]Or manually at runtime:
ExTwitter.configure([consumer_key: "", ...])You can also configure the current process only:
ExTwitter.configure(:process, [consumer_key: "", ...])mix.exs
defp deps do
[
{:oauth, github: "tim/erlang-oauth"},
{:extwitter, "~> 0.1"}
]
endSample
Sample execution on iex.
$ iex -S mix
Interactive Elixir - press Ctrl+C to exit (type h() ENTER for help)ExTwitter.configure(
consumer_key: System.get_env("TWITTER_CONSUMER_KEY"),
consumer_secret: System.get_env("TWITTER_CONSUMER_SECRET"),
access_token: System.get_env("TWITTER_ACCESS_TOKEN"),
access_token_secret: System.get_env("TWITTER_ACCESS_SECRET")
)
:okExample for normal API.
ExTwitter.search("elixir-lang", [count: 5]) |>
Enum.map(fn(tweet) -> tweet.text end) |>
Enum.join("\n-----\n") |>
IO.puts
@xxxx have you tried this yet?
-----
@yyyy You mean this? http://t.co/xxxx That had sailed below my radar thus far.
-----
@zzzz #elixir-lang. I'm jadams
-----
Akala ko 100 nalang kulang ko sa dark elixir para sa Barb King summoner level.
-----
@aaaa usually kasi magbbuzz lang yan pag luma na string. talaga ang elixir.
:okExample for streaming API.
stream = ExTwitter.stream_filter(track: "apple") |>
Stream.map(fn(x) -> x.text end) |>
Stream.map(fn(x) -> IO.puts "#{x}\n---------------\n" end)
Enum.to_list(stream)
Apple 'iWatch' rumour round-up
---------------
Apple iPhone 4s 16GB Black Verizon - Cracked Screen, WORKS PERFECTLY!
---------------
Apple iPod nano 7th Generation (PRODUCT) RED (16 GB) (Latest Model) - Full read by
---------------
...
...
The ExTwitter.stream_control method allows to send a message to stop the stream.
# An example to stop receiving stream after 5 seconds passed.
pid = spawn(fn ->
stream = ExTwitter.stream_filter(track: "apple")
for tweet <- stream do
IO.puts tweet.text
end
end)
:timer.sleep(5000)
ExTwitter.stream_control(pid, :stop)
Twitter returns several message types (<a href=”https://dev.twitter.com/streaming/overview/messages-types” target=”_blank”>dev.twitter.com - Streaming message types</a>). These messages are returned when receive_messages option is specified.
stream = ExTwitter.stream_sample(receive_messages: true)
for message <- stream do
case message do
tweet = %ExTwitter.Model.Tweet{} ->
IO.puts "tweet = #{tweet.text}"
deleted_tweet = %ExTwitter.Model.DeletedTweet{} ->
IO.puts "deleted tweet = #{deleted_tweet.status["id"]}"
limit = %ExTwitter.Model.Limit{} ->
IO.puts "limit = #{limit.track}"
stall_warning = %ExTwitter.Model.StallWarning{} ->
IO.puts "stall warning = #{stall_warning.code}"
_ ->
IO.inspect message
end
endNotes
run_iex.sh launches iex, with initially calling ExTwitter.configure defined as iex/dot.iex.
$ ./run_iex.sh
Erlang R16B03 (erts-5.10.4) [source] [64-bit] [smp:4:4] [async-threads:10]...
Interactive Elixir (0.12.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> (ExTwitter.search("josevalim") |> List.first).text
...