From 7a0b08540aec456ac0a8432d605c536c5c8ed526 Mon Sep 17 00:00:00 2001 From: Stefan Sundin Date: Fri, 12 Jul 2019 12:59:18 -0700 Subject: [PATCH] Add custom error for Instagram 429 errors.. In the week, I have started seeing tens of thousands of these errors per day. There is obviously a moron abusing the service right now. I will start blocking IPs real soon. --- app/instagram.rb | 9 +++++++++ lib/http.rb | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/app/instagram.rb b/app/instagram.rb index 6e676fd..4ef78d2 100644 --- a/app/instagram.rb +++ b/app/instagram.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true class InstagramError < HTTPError; end +class InstagramRatelimitError < HTTPError; end class InstagramTokenError < InstagramError; end class Instagram < HTTP @@ -29,6 +30,9 @@ def self.get(url, options={headers: {}}, tokens={csrftoken: nil}) if response.code == 403 raise(InstagramTokenError, response) end + if response.code == 429 + raise(InstagramRatelimitError) + end response end @@ -62,3 +66,8 @@ def self.get_post(id, opts={}, tokens={}) status 503 "There was a problem talking to Instagram. Please try again in a moment." end + +error InstagramRatelimitError do |e| + status 429 + "There are too many requests going to Instagram right now. Someone is probably abusing this service. PLEASE SLOW DOWN!" +end diff --git a/lib/http.rb b/lib/http.rb index 70b54fb..d68a9bf 100644 --- a/lib/http.rb +++ b/lib/http.rb @@ -109,7 +109,7 @@ def redirect_same_origin? end class HTTPError < StandardError - def initialize(obj) + def initialize(obj=nil) @obj = obj end