Skip to content

Commit

Permalink
Add Twitter.
Browse files Browse the repository at this point in the history
  • Loading branch information
stefansundin committed Sep 4, 2016
1 parent 94fb991 commit 6a1d62f
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
REDIS_URL=redis://localhost:6379/3

TWITTER_ACCESS_TOKEN=

GOOGLE_API_KEY=

VIMEO_ACCESS_TOKEN=
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ You need to get API keys for the respective services and populate the environmen

A couple of services do not have official APIs, or do not require API keys, so they will work without any keys.

#### Twitter

Go to [Twitter Application Management](https://apps.twitter.com/) and create a new app.

Once you have the consumer key and consumer secret, run the following to get the bearer token.

```
curl -X POST -d grant_type=client_credentials -u CONSUMER_KEY:CONSUMER_SECRET https://api.twitter.com/oauth2/token
```

Copy the `access_token` and put it in the config.

#### Google

Go to the [Google Developer Console](https://console.developers.google.com/), create a project and a server key. Copy the server key.
Expand Down
37 changes: 37 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,43 @@
end
end

get "/twitter" do
return "Insufficient parameters" if params[:q].empty?

if /twitter\.com\/i\// =~ params[:q] or /twitter\.com\/who_to_follow\// =~ params[:q]
return "Unsupported url. Sorry."
elsif /twitter\.com\/hashtag\// =~ params[:q]
return "This app does not support hashtags. Sorry."
elsif /twitter\.com\/(?:#!\/)?(?<user>[^\/?#]+)/ =~ params[:q]
# https://twitter.com/#!/infected
# https://twitter.com/infected
else
# it's probably a username
user = params[:q]
end

response = TwitterParty.get("/users/lookup.json", query: { screen_name: user })
return "Can't find a user with that name. Sorry." if response.code == 404
raise TwitterError.new(response) if !response.success?

user_id = response.parsed_response[0]["id_str"]
screen_name = response.parsed_response[0]["screen_name"]
redirect "/twitter/#{user_id}/#{screen_name}"
end

get %r{/twitter/(?<id>\d+)(/(?<username>.+))?} do |id, username|
@user_id = id

response = TwitterParty.get("/statuses/user_timeline.json", query: { user_id: id, count: 200, include_rts: "1" })
raise TwitterError.new(response) if !response.success?

@data = response.parsed_response
@username = @data[0]["user"]["screen_name"] rescue username

content_type :atom
erb :twitter_feed
end

get "/youtube" do
return "Insufficient parameters" if params[:q].empty?

Expand Down
15 changes: 15 additions & 0 deletions app/twitter_party.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# https://dev.twitter.com/rest/reference/get/statuses/user_timeline

class TwitterParty
include HTTParty
base_uri "https://api.twitter.com/1.1"
headers Authorization: "Bearer #{ENV["TWITTER_ACCESS_TOKEN"]}"
format :json
end

class TwitterError < PartyError; end

error TwitterError do |e|
status 503
"There was a problem talking to Twitter."
end
10 changes: 10 additions & 0 deletions views/index.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@
</header>

<div class="container">
<% if ENV["TWITTER_ACCESS_TOKEN"] %>
<form class="input-group" method="get" action="twitter">
<div class="input-group-addon"><label for="twitter_q">Twitter</label></div>
<input class="form-control" type="search" name="q" id="twitter_q" placeholder="Enter a Twitter handle or a url." required>
<span class="input-group-btn">
<input class="btn btn-primary" type="submit" value="Get RSS Feed">
</span>
</form>
<% end %>
<% if ENV["GOOGLE_API_KEY"] %>
<form class="input-group" method="get" action="youtube">
<div class="input-group-addon"><label for="youtube_q">YouTube</label></div>
Expand Down
22 changes: 22 additions & 0 deletions views/twitter_feed.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<id>twitter:<%= @user_id %></id>
<title><%= @username %> on Twitter</title>
<icon>https://abs.twimg.com/favicons/favicon.ico</icon>
<link href="<%= request.original_url.esc %>" rel="self" />
<link href="https://twitter.com/<%= @username %>" rel="alternate" />
<updated><%= Time.parse(@data[0]["created_at"]) if @data[0] %></updated>
<%- @data.each do |tweet| -%>

<entry>
<id>twitter:tweet:<%= tweet["id_str"] %><%= ":#{params[:cachebuster]}" if params[:cachebuster] %></id>
<title><%= tweet["text"].to_line.esc %></title>
<link href="https://twitter.com/<%= @username %>/status/<%= tweet["id_str"] %>" />
<updated><%= Time.parse(tweet["created_at"]) %></updated>
<author><name><%= @username %></name></author>
<content type="html">
<%= tweet["text"].esc %>
</content>
</entry>
<%- end -%>
</feed>

0 comments on commit 6a1d62f

Please sign in to comment.