Skip to content

Commit

Permalink
Check ENV for credentials before attempting to serve requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
stefansundin committed May 10, 2020
1 parent 5ba061a commit 965cc65
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
end

get "/twitter" do
return [404, "Credentials not configured"] if !ENV["TWITTER_ACCESS_TOKEN"]
return [400, "Insufficient parameters"] if params[:q].empty?

if params[:q].include?("twitter.com/i/") || params[:q].include?("twitter.com/who_to_follow/")
Expand Down Expand Up @@ -132,6 +133,8 @@
end

get %r{/twitter/(?<id>\d+)/(?<username>.+)} do |id, username|
return [404, "Credentials not configured"] if !ENV["TWITTER_ACCESS_TOKEN"]

@user_id = id

response = Twitter.get("/statuses/user_timeline.json", query: {
Expand Down Expand Up @@ -172,6 +175,7 @@
end

get "/youtube" do
return [404, "Credentials not configured"] if !ENV["GOOGLE_API_KEY"]
return [400, "Insufficient parameters"] if params[:q].empty?

if /youtube\.com\/channel\/(?<channel_id>(UC|S)[^\/?#]+)(?:\/search\?query=(?<query>[^&#]+))?/ =~ params[:q]
Expand Down Expand Up @@ -243,6 +247,8 @@
end

get "/youtube/:channel_id/:username.ics" do
return [404, "Credentials not configured"] if !ENV["GOOGLE_API_KEY"]

@channel_id = params[:channel_id]
@username = params[:username]
@title = "#{@username} on YouTube"
Expand Down Expand Up @@ -289,6 +295,8 @@
end

get "/youtube/:channel_id/:username" do
return [404, "Credentials not configured"] if !ENV["GOOGLE_API_KEY"]

@channel_id = params[:channel_id]
playlist_id = "UU" + @channel_id[2..]
@username = params[:username]
Expand Down Expand Up @@ -379,8 +387,9 @@
end

get "/facebook" do
return [404, "Facebook credentials not configured"] if ENV["FACEBOOK_APP_ID"].empty? || ENV["FACEBOOK_APP_SECRET"].empty?
return [404, "Credentials not configured"] if !ENV["FACEBOOK_APP_ID"] || !ENV["FACEBOOK_APP_SECRET"]
return [400, "Insufficient parameters"] if params[:q].empty?

params[:q].gsub!("facebookcorewwwi.onion", "facebook.com") if params[:q].include?("facebookcorewwwi.onion")

if /https:\/\/www\.facebook\.com\/plugins\/.+[?&]href=(?<href>.+)$/ =~ params[:q]
Expand Down Expand Up @@ -427,6 +436,8 @@
end

get "/facebook/download" do
return [404, "Credentials not configured"] if !ENV["FACEBOOK_APP_ID"] || !ENV["FACEBOOK_APP_SECRET"]

if /\/(?<id>\d+)/ =~ params[:url]
# https://www.facebook.com/infectedmushroom/videos/10153430677732261/
# https://www.facebook.com/infectedmushroom/videos/vb.8811047260/10153371214897261/?type=2&theater
Expand Down Expand Up @@ -466,7 +477,7 @@
end

get %r{/facebook/(?<id>\d+)/(?<username>.+)} do |id, username|
return [404, "Facebook credentials not configured"] if ENV["FACEBOOK_APP_ID"].empty? || ENV["FACEBOOK_APP_SECRET"].empty?
return [404, "Credentials not configured"] if !ENV["FACEBOOK_APP_ID"] || !ENV["FACEBOOK_APP_SECRET"]

@id = id
@type = @edge = %w[videos photos live].pick(params[:type]) || "posts"
Expand Down Expand Up @@ -726,6 +737,7 @@
end

get "/soundcloud" do
return [404, "Credentials not configured"] if !ENV["SOUNDCLOUD_CLIENT_ID"]
return [400, "Insufficient parameters"] if params[:q].empty?

if /soundcloud\.com\/(?<username>[^\/?#]+)/ =~ params[:q]
Expand Down Expand Up @@ -754,6 +766,8 @@
end

get "/soundcloud/download" do
return [404, "Credentials not configured"] if !ENV["SOUNDCLOUD_CLIENT_ID"]

url = params[:url]
url = "https://#{url}" if !url.start_with?("http:", "https:")
response = Soundcloud.get("/resolve", query: { url: url })
Expand All @@ -774,6 +788,8 @@
end

get %r{/soundcloud/(?<id>\d+)/(?<username>.+)} do |id, username|
return [404, "Credentials not configured"] if !ENV["SOUNDCLOUD_CLIENT_ID"]

@id = id

response = Soundcloud.get("/users/#{id}/tracks")
Expand Down Expand Up @@ -821,6 +837,7 @@
end

get "/twitch" do
return [404, "Credentials not configured"] if !ENV["TWITCH_CLIENT_ID"]
return [400, "Insufficient parameters"] if params[:q].empty?

if /twitch\.tv\/directory\/game\/(?<game_name>[^\/?#]+)/ =~ params[:q]
Expand Down Expand Up @@ -861,6 +878,7 @@
end

get "/twitch/download" do
return [404, "Credentials not configured"] if !ENV["TWITCH_CLIENT_ID"]
return [400, "Insufficient parameters"] if params[:url].empty?

if /twitch\.tv\/[^\/]+\/clip\/(?<clip_slug>[^?&#]+)/ =~ params[:url] || /clips\.twitch\.tv\/(?:embed\?clip=)?(?<clip_slug>[^?&#]+)/ =~ params[:url]
Expand Down Expand Up @@ -915,6 +933,7 @@
end

get "/twitch/watch" do
return [404, "Credentials not configured"] if !ENV["TWITCH_CLIENT_ID"]
return [400, "Insufficient parameters"] if params[:url].empty?

if /twitch\.tv\/[^\/]+\/clip\/(?<clip_slug>[^?&#]+)/ =~ params[:url] || /clips\.twitch\.tv\/(?:embed\?clip=)?(?<clip_slug>[^?&#]+)/ =~ params[:url]
Expand Down Expand Up @@ -974,6 +993,8 @@
end

get %r{/twitch/directory/game/(?<id>\d+)/(?<game_name>.+)} do |id, game_name|
return [404, "Credentials not configured"] if !ENV["TWITCH_CLIENT_ID"]

@id = id
@type = "game"

Expand All @@ -999,6 +1020,8 @@
end

get %r{/twitch/(?<id>\d+)/(?<user>.+)\.ics} do |id, user|
return [404, "Credentials not configured"] if !ENV["TWITCH_CLIENT_ID"]

@title = "#{user} on Twitch"

type = %w[all upload archive highlight].pick(params[:type]) || "all"
Expand All @@ -1013,6 +1036,8 @@
end

get %r{/twitch/(?<id>\d+)/(?<user>.+)} do |id, user|
return [404, "Credentials not configured"] if !ENV["TWITCH_CLIENT_ID"]

@id = id
@type = "user"

Expand Down Expand Up @@ -1189,6 +1214,7 @@
end

get "/imgur" do
return [404, "Credentials not configured"] if !ENV["IMGUR_CLIENT_ID"]
return [400, "Insufficient parameters"] if params[:q].empty?

if /imgur\.com\/user\/(?<username>[a-zA-Z0-9]+)/ =~ params[:q]
Expand Down Expand Up @@ -1240,6 +1266,8 @@
end

get "/imgur/:user_id/:username" do
return [404, "Credentials not configured"] if !ENV["IMGUR_CLIENT_ID"]

if params[:user_id] == "r"
@subreddit = params[:username]
response = Imgur.get("/gallery/r/#{@subreddit}")
Expand Down

0 comments on commit 965cc65

Please sign in to comment.