Skip to content

Commit

Permalink
Add facebook feed since they stopped supporting their own.
Browse files Browse the repository at this point in the history
  • Loading branch information
stefansundin committed Jun 29, 2015
1 parent 23d2aa6 commit d641bb1
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 6 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ gem "airbrake"
gem "rack-ssl-enforcer"
gem "clogger"
gem "heroku-env"
gem "activesupport"

group :development do
gem "rake"
Expand Down
12 changes: 12 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
GEM
remote: https://rubygems.org/
specs:
activesupport (4.2.3)
i18n (~> 0.7)
json (~> 1.7, >= 1.7.7)
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
airbrake (4.3.0)
builder
multi_json
Expand All @@ -24,9 +30,11 @@ GEM
httparty (0.13.5)
json (~> 1.8)
multi_xml (>= 0.5.2)
i18n (0.7.0)
json (1.8.3)
kgio (2.9.3)
method_source (0.8.2)
minitest (5.7.0)
multi_json (1.11.1)
multi_xml (0.5.5)
powder (0.3.0)
Expand Down Expand Up @@ -64,7 +72,10 @@ GEM
sinatra-contrib
slop (3.6.0)
thor (0.19.1)
thread_safe (0.3.5)
tilt (2.0.1)
tzinfo (1.2.2)
thread_safe (~> 0.1)
unicorn (4.9.0)
kgio (~> 2.6)
rack
Expand All @@ -74,6 +85,7 @@ PLATFORMS
ruby

DEPENDENCIES
activesupport
airbrake
better_errors
binding_of_caller
Expand Down
8 changes: 8 additions & 0 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
"description": "(Optional) Your Google API key for YouTube queries.",
"required": false
},
"FACEBOOK_APP_ID": {
"description": "(Optional) Your Facebook app id.",
"required": false
},
"FACEBOOK_APP_SECRET": {
"description": "(Optional) Your Facebook app secret.",
"required": false
},
"INSTAGRAM_CLIENT_ID": {
"description": "(Optional) Your Instagram client id.",
"required": false
Expand Down
24 changes: 18 additions & 6 deletions app.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "sinatra"
require "./config/application"
require "erb"
require "active_support/core_ext/string"

class YoutubeException < Exception; end
class FacebookException < Exception; end
Expand Down Expand Up @@ -66,16 +67,27 @@ def httparty_error(r)
return "That doesn't look like a facebook url. Sorry."
end

response = HTTParty.get("https://graph.facebook.com/#{name}")
response = HTTParty.get("https://graph.facebook.com/v2.3/#{name}?access_token=#{ENV["FACEBOOK_APP_ID"]}|#{ENV["FACEBOOK_APP_SECRET"]}")
if response.code == 404
return "Can't find a page with that name. Sorry."
end
if !response.success?
raise FacebookException, response
end
facebook_id = response.parsed_response["id"]
raise FacebookException, response if !response.success?

data = response.parsed_response
redirect "/facebook/#{data["id"]}/#{data["username"]}"
end

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

redirect "https://www.facebook.com/feeds/page.php?format=rss20&id=#{facebook_id}"
response = HTTParty.get("https://graph.facebook.com/v2.3/#{id}/posts?access_token=#{ENV["FACEBOOK_APP_ID"]}|#{ENV["FACEBOOK_APP_SECRET"]}")
raise FacebookException, response if !response.success?

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

headers "Content-Type" => "application/atom+xml;charset=utf-8"
erb :facebook_feed
end

get "/instagram/auth" do
Expand Down
19 changes: 19 additions & 0 deletions views/facebook_feed.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<id>facebook:<%= @id %></id>
<title><%= @user %> on Facebook</title>
<icon>https://www.facebook.com/favicon.ico</icon>
<link href="<%= request.url %>" rel="self" />
<updated><%= @data[0]["updated_time"] if @data[0] %></updated>
<%- @data.each do |post| -%>

<entry>
<id>facebook:post:<%= post["id"] %>:<%= post["updated_time"] %><%= ":#{params[:cachebuster]}" if params[:cachebuster] %></id>
<title><%= post["message"] ? post["message"].truncate(120) : post["type"] %></title>
<link href="<%= post["link"] || "https://www.facebook.com/#{post["id"]}" %>" />
<updated><%= post["updated_time"] %></updated>
<author><name><%= post["from"]["name"] %></name></author>
<content><%= post["message"] %></content>
</entry>
<%- end -%>
</feed>
2 changes: 2 additions & 0 deletions views/index.erb
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@
</span>
</form>

<% if ENV["FACEBOOK_APP_ID"] and ENV["FACEBOOK_APP_SECRET"] %>
<form class="input-group" method="get" action="/facebook" target="_self">
<div class="input-group-addon"><label for="facebook_q">Facebook</label></div>
<input class="form-control" type="search" name="q" id="facebook_q" placeholder="Enter url to a Facebook page.">
<span class="input-group-btn">
<input class="btn btn-primary" type="submit" value="Get RSS Feed">
</span>
</form>
<% end %>
<% if ENV["INSTAGRAM_ACCESS_TOKEN"] %>
<form class="input-group" method="get" action="/instagram" target="_self">
Expand Down

0 comments on commit d641bb1

Please sign in to comment.