Skip to content

Commit

Permalink
Don't require a url.
Browse files Browse the repository at this point in the history
  • Loading branch information
stefansundin committed Jun 29, 2015
1 parent d641bb1 commit a4f4305
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
27 changes: 19 additions & 8 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def httparty_error(r)
end

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

if /youtube\.com\/channel\/(?<channel_id>UC[^\/\?#]+)/ =~ params[:q]
# https://www.youtube.com/channel/UC4a-Gbdw7vOaccHmFo40b9g/videos
elsif /youtube\.com\/user\/(?<user>[^\/\?#]+)/ =~ params[:q]
Expand All @@ -29,8 +31,11 @@ def httparty_error(r)
# https://www.youtube.com/khanacademy
elsif /youtu\.be\/(?<video_id>[^\?#]+)/ =~ params[:q]
# https://youtu.be/vVXbgbMp0oY?t=1s
elsif /(?<channel_id>UC[^\/\?#]+)/ =~ params[:q]
# it's a channel id
else
return "That doesn't look like a youtube url. Sorry."
# it's probably a channel name
user = params[:q]
end

if user
Expand All @@ -56,21 +61,21 @@ def httparty_error(r)
if channel_id
redirect "https://www.youtube.com/feeds/videos.xml?channel_id=#{channel_id}"
else
"Could not figure out channel id from url. Sorry."
"Could not find the channel. Sorry."
end
end

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

if /facebook\.com\/(?<name>[^\/\?#]+)/ =~ params[:q]
# https://www.facebook.com/celldweller/info?tab=overview
else
return "That doesn't look like a facebook url. Sorry."
name = params[:q]
end

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
return "Can't find a page with that name. Sorry." if response.code == 404
raise FacebookException, response if !response.success?

data = response.parsed_response
Expand All @@ -92,6 +97,7 @@ def httparty_error(r)

get "/instagram/auth" do
return "Already authed" if ENV["INSTAGRAM_ACCESS_TOKEN"]

if params[:code]
response = HTTParty.post("https://api.instagram.com/oauth/access_token", body: {
client_id: ENV["INSTAGRAM_CLIENT_ID"],
Expand All @@ -109,18 +115,23 @@ def httparty_error(r)
end

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

if /instagram\.com\/p\/(?<post_id>[^\/\?#]+)/ =~ params[:q]
# https://instagram.com/p/4KaPsKSjni/
response = HTTParty.get("https://api.instagram.com/v1/media/shortcode/#{post_id}?access_token=#{ENV["INSTAGRAM_ACCESS_TOKEN"]}")
return response.parsed_response["meta"]["error_message"] if !response.success?
user = response.parsed_response["data"]["user"]
elsif /instagram\.com\/(?<name>[^\/\?#]+)/ =~ params[:q]
# https://instagram.com/infectedmushroom/
else
name = params[:q]
end

if name
response = HTTParty.get("https://api.instagram.com/v1/users/search?q=#{name}&access_token=#{ENV["INSTAGRAM_ACCESS_TOKEN"]}")
raise InstagramException, response if !response.success?
user = response.parsed_response["data"].find { |user| user["username"] == name }
else
return "That doesn't look like an instagram url. Sorry."
end

if user
Expand Down
5 changes: 5 additions & 0 deletions config/initializers/05-nil.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class NilClass
def empty?
true
end
end
8 changes: 5 additions & 3 deletions views/index.erb
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,21 @@
</hgroup>
</header>

<% if ENV["GOOGLE_API_KEY"] %>
<div class="container">
<form class="input-group" method="get" action="/youtube" target="_self">
<div class="input-group-addon"><label for="youtube_q">YouTube</label></div>
<input class="form-control" type="search" name="q" id="youtube_q" placeholder="Enter url to a YouTube video or channel.">
<input class="form-control" type="search" name="q" id="youtube_q" placeholder="Enter a YouTube channel name, channel id, or a url to a channel or video.">
<span class="input-group-btn">
<input class="btn btn-primary" type="submit" value="Get RSS Feed">
</span>
</form>
<% end %>
<% 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.">
<input class="form-control" type="search" name="q" id="facebook_q" placeholder="Enter a Facebook page name or its url.">
<span class="input-group-btn">
<input class="btn btn-primary" type="submit" value="Get RSS Feed">
</span>
Expand All @@ -55,7 +57,7 @@
<% if ENV["INSTAGRAM_ACCESS_TOKEN"] %>
<form class="input-group" method="get" action="/instagram" target="_self">
<div class="input-group-addon"><label for="instagram_q">Instagram</label></div>
<input class="form-control" type="search" name="q" id="instagram_q" placeholder="Enter url to an Instagram post or user.">
<input class="form-control" type="search" name="q" id="instagram_q" placeholder="Enter an Instagram username or a url to a user or post.">
<span class="input-group-btn">
<input class="btn btn-primary" type="submit" value="Get RSS Feed">
</span>
Expand Down

0 comments on commit a4f4305

Please sign in to comment.