extwitter 0.4.0

Twitter client library for elixir.

Platform: Hex

Language: Elixir

License: MIT

Repository: https://github.com/parroty/extwitter

View on registry: https://hex.pm/packages/extwitter/

Documentation: http://hexdocs.pm/extwitter/0.4.0


ExTwitter Build Status Coverage Status Inline docs

Twitter client library for elixir. It uses erlang-oauth to call Twitter's REST API.

It only supports very limited set of functions yet. Refer to lib/extwitter.ex and test/extwitter_test.exs for available functions and examples.

Documentation

Usage

  1. Add extwitter to deps section in the mix.exs.
  2. Use ExTwitter.configure to setup Twitter's OAuth authentication paramters. Refer to https://dev.twitter.com/docs for the detail.
  3. 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.2"}
 ]
end

Sample

Sample execution on iex.

configure

$ 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")
)
:ok

search

Example for normal API.

ExTwitter.search("elixir-lang", [count: 5]) |>
 Enum.map(fn(tweet) -> tweet.text end) |>
 Enum.join("\n-----\n") |>
 IO.puts
# => Tweets will be displayed in the console as follows.
@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.
:ok

streaming

Example 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)
# => Tweets will be displayed in the console as follows.
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 (dev.twitter.com - Streaming message types). 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
end

cursor

Some of Twtitter API have paging capability for retrieving large number of items through cursor. The following is an example to iteratively call the API to fetch all the items.

defmodule Retriever do
 def follower_ids(screen_name, acc \\ [], cursor \\ -1) do
 cursor = fetch_next(screen_name, cursor)
 if Enum.count(cursor.items) == 0 do
 List.flatten(acc)
 else
 follower_ids(screen_name, [cursor.items|acc], cursor.next_cursor)
 end
 end
 defp fetch_next(screen_name, cursor) do
 try do
 ExTwitter.follower_ids(screen_name, cursor: cursor)
 rescue
 e in ExTwitter.RateLimitExceededError ->
 :timer.sleep ((e.reset_in + 1) * 1000)
 fetch_next(screen_name, cursor)
 end
 end
end
ids = Retriever.follower_ids("TwitterDev")
IO.puts "Follower count for TwitterDev is #{Enum.count(ids)}."
# => Follower count for TwitterDev is 38469.

Notes

run_iex.sh launches iex, with initially calling ExTwitter.configure defined as iex/dot.iex.

$ ./run_iex.sh
Erlang/OTP 17 [erts-6.3] [source] [64-bit] [smp:4:4] [async-threads:10]...
Interactive Elixir (1.0.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> (ExTwitter.search("elixir") |> List.first).text
...
веселые картинки развлекательные гифки интресные факты смешные видео смешные истории из соцсетей

Dependencies


GitHub Repository

parroty/extwitter parroty/extwitter

Twitter client library for elixir.

Language: Elixir

Created: March 05, 2014 15:07

Last updated: March 07, 2015 06:00

Last pushed: March 07, 2015 06:05

Size: 737 KB

Stars: 38

Forks: 6

Watchers: 1

Open issues: 3

Top Contributors

parroty Matthew Rothenberg Jordan niku

Releases

  • 0.4.0 - March 07, 2015 06:05
  • 0.3.0 - January 23, 2015 14:21
  • 0.2.1 - December 30, 2014 06:10
  • 0.2.0 - December 30, 2014 05:27
  • 0.1.6 - November 30, 2014 12:54
  • 0.1.5 - September 11, 2014 15:27
  • 0.1.4 - September 07, 2014 12:04
  • 0.1.3 - August 07, 2014 13:42
  • 0.1.2 - July 21, 2014 15:07
  • 0.1.1 - July 06, 2014 12:21
See all 11 releases

Related Projects

excheck 0.2.1
Property-based testing library for Elixir (QuickCheck style).
Hex - Elixir - MIT - Updated 2 months ago - 37 stars
excoveralls 0.3.8
Coverage report tool for Elixir with coveralls.io integration.
Hex - Elixir - Updated 3 months ago - 21 stars
exfirebase 0.2.0
An elixir library for accessing the Firebase REST API.
Hex - Elixir - MIT - Updated 7 months ago - 7 stars
exirc 0.9.1
An IRC client library for Elixir.
Hex - Elixir - MIT - Updated 4 months ago - 18 stars
exlingr 0.0.1
Lingr client library for elixir.
Hex - CSS - WTFPL - Published 4 months ago - 1 stars
веселые картинки развлекательные гифки интресные факты смешные видео смешные истории из соцсетей