-
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Put all of my classes in an App module to avoid clashing with gems (e…
….g. the http gem).
- Loading branch information
1 parent
22190cf
commit aab5094
Showing
18 changed files
with
555 additions
and
528 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
# frozen_string_literal: true | ||
# https://developer.dailymotion.com/api | ||
|
||
class DailymotionError < HTTPError; end | ||
module App | ||
class DailymotionError < HTTPError; end | ||
|
||
class Dailymotion < HTTP | ||
BASE_URL = "https://api.dailymotion.com" | ||
ERROR_CLASS = DailymotionError | ||
class Dailymotion < HTTP | ||
BASE_URL = "https://api.dailymotion.com" | ||
ERROR_CLASS = DailymotionError | ||
end | ||
end | ||
|
||
error DailymotionError do |e| | ||
error App::DailymotionError do |e| | ||
status 503 | ||
"There was a problem talking to Dailymotion. Please try again in a moment." | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,19 @@ | ||
# frozen_string_literal: true | ||
# https://api.imgur.com/endpoints | ||
|
||
class ImgurError < HTTPError; end | ||
module App | ||
class ImgurError < HTTPError; end | ||
|
||
class Imgur < HTTP | ||
BASE_URL = "https://api.imgur.com/3" | ||
HEADERS = { | ||
"Authorization" => "Client-ID #{ENV["IMGUR_CLIENT_ID"]}", | ||
} | ||
ERROR_CLASS = ImgurError | ||
class Imgur < HTTP | ||
BASE_URL = "https://api.imgur.com/3" | ||
HEADERS = { | ||
"Authorization" => "Client-ID #{ENV["IMGUR_CLIENT_ID"]}", | ||
} | ||
ERROR_CLASS = ImgurError | ||
end | ||
end | ||
|
||
error ImgurError do |e| | ||
error App::ImgurError do |e| | ||
status 503 | ||
"There was a problem talking to Imgur. Please try again in a moment." | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,66 @@ | ||
# frozen_string_literal: true | ||
|
||
class InstagramError < HTTPError; end | ||
class InstagramRatelimitError < HTTPError; end | ||
class InstagramTokenError < InstagramError; end | ||
module App | ||
class InstagramError < HTTPError; end | ||
class InstagramRatelimitError < HTTPError; end | ||
class InstagramTokenError < InstagramError; end | ||
|
||
class Instagram < HTTP | ||
BASE_URL = "https://www.instagram.com" | ||
PARAMS = "__a=1" | ||
HEADERS = { | ||
"User-Agent" => "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:80.0) Gecko/20100101 Firefox/80.0", | ||
"Cookie" => "ig_cb=1", | ||
} | ||
ERROR_CLASS = InstagramError | ||
class Instagram < HTTP | ||
BASE_URL = "https://www.instagram.com" | ||
PARAMS = "__a=1" | ||
HEADERS = { | ||
"User-Agent" => "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:80.0) Gecko/20100101 Firefox/80.0", | ||
"Cookie" => "ig_cb=1", | ||
} | ||
ERROR_CLASS = InstagramError | ||
|
||
@@cache = {} | ||
@@cache = {} | ||
|
||
def self.get(url, options={headers: {}}) | ||
options ||= {} | ||
options[:headers] ||= {} | ||
response = super(url, options) | ||
if response.code == 403 | ||
raise(InstagramTokenError, response) | ||
elsif response.code == 429 | ||
raise(InstagramRatelimitError, response) | ||
def self.get(url, options={headers: {}}) | ||
options ||= {} | ||
options[:headers] ||= {} | ||
response = super(url, options) | ||
if response.code == 403 | ||
raise(InstagramTokenError, response) | ||
elsif response.code == 429 | ||
raise(InstagramRatelimitError, response) | ||
end | ||
response | ||
end | ||
response | ||
end | ||
|
||
def self.get_post(id, opts={}) | ||
return @@cache[id] if @@cache[id] | ||
value = $redis.get("instagram:#{id}") | ||
if value | ||
@@cache[id] = JSON.parse(value) | ||
return @@cache[id] | ||
end | ||
def self.get_post(id, opts={}) | ||
return @@cache[id] if @@cache[id] | ||
value = $redis.get("instagram:#{id}") | ||
if value | ||
@@cache[id] = JSON.parse(value) | ||
return @@cache[id] | ||
end | ||
|
||
response = Instagram.get("/p/#{id}/", opts) | ||
raise(InstagramError, response) if !response.success? || !response.json | ||
post = response.json["graphql"]["shortcode_media"] | ||
response = Instagram.get("/p/#{id}/", opts) | ||
raise(InstagramError, response) if !response.success? || !response.json | ||
post = response.json["graphql"]["shortcode_media"] | ||
|
||
@@cache[id] = if post["__typename"] == "GraphSidecar" | ||
post["edge_sidecar_to_children"]["edges"].map do |edge| | ||
edge["node"].slice("is_video", "display_url", "video_url") | ||
@@cache[id] = if post["__typename"] == "GraphSidecar" | ||
post["edge_sidecar_to_children"]["edges"].map do |edge| | ||
edge["node"].slice("is_video", "display_url", "video_url") | ||
end | ||
else | ||
# This isn't really used | ||
post.slice("is_video", "display_url", "video_url") | ||
end | ||
else | ||
# This isn't really used | ||
post.slice("is_video", "display_url", "video_url") | ||
end | ||
|
||
$redis.set("instagram:#{id}", @@cache[id].to_json) | ||
return @@cache[id] | ||
$redis.set("instagram:#{id}", @@cache[id].to_json) | ||
return @@cache[id] | ||
end | ||
end | ||
end | ||
|
||
error InstagramError do |e| | ||
error App::InstagramError do |e| | ||
status 503 | ||
"There was a problem talking to Instagram. Please try again in a moment." | ||
end | ||
|
||
error InstagramRatelimitError do |e| | ||
error App::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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
# frozen_string_literal: true | ||
# https://www.mixcloud.com/developers/ | ||
|
||
class MixcloudError < HTTPError; end | ||
module App | ||
class MixcloudError < HTTPError; end | ||
|
||
class Mixcloud < HTTP | ||
BASE_URL = "https://api.mixcloud.com" | ||
ERROR_CLASS = MixcloudError | ||
class Mixcloud < HTTP | ||
BASE_URL = "https://api.mixcloud.com" | ||
ERROR_CLASS = MixcloudError | ||
end | ||
end | ||
|
||
error MixcloudError do |e| | ||
error App::MixcloudError do |e| | ||
status 503 | ||
"There was a problem talking to Mixcloud. Please try again in a moment." | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,38 @@ | ||
# frozen_string_literal: true | ||
# No public API documentation | ||
|
||
class PeriscopeError < HTTPError; end | ||
module App | ||
class PeriscopeError < HTTPError; end | ||
|
||
class Periscope < HTTP | ||
BASE_URL = "https://api.periscope.tv/api/v2" | ||
HEADERS = { | ||
"User-Agent" => "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:80.0) Gecko/20100101 Firefox/80.0", | ||
} | ||
ERROR_CLASS = PeriscopeError | ||
class Periscope < HTTP | ||
BASE_URL = "https://api.periscope.tv/api/v2" | ||
HEADERS = { | ||
"User-Agent" => "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:80.0) Gecko/20100101 Firefox/80.0", | ||
} | ||
ERROR_CLASS = PeriscopeError | ||
|
||
# The session_id is valid for one hour | ||
@@session_id = nil | ||
# The session_id is valid for one hour | ||
@@session_id = nil | ||
|
||
def self.get_broadcasts(user_id) | ||
if @@session_id | ||
response = get("/getUserBroadcastsPublic", query: { user_id: user_id, session_id: @@session_id }) | ||
if response.success? | ||
return response | ||
def self.get_broadcasts(user_id) | ||
if @@session_id | ||
response = get("/getUserBroadcastsPublic", query: { user_id: user_id, session_id: @@session_id }) | ||
if response.success? | ||
return response | ||
end | ||
end | ||
response = get("https://www.periscope.tv/cnn") | ||
raise(ERROR_CLASS, response) if !response.success? | ||
doc = Nokogiri::HTML(response.body) | ||
data = doc.at("div#page-container")["data-store"] | ||
json = JSON.parse(data) | ||
@@session_id = json["SessionToken"]["public"]["broadcastHistory"]["token"]["session_id"] | ||
get("/getUserBroadcastsPublic", query: { user_id: user_id, session_id: @@session_id }) | ||
end | ||
response = get("https://www.periscope.tv/cnn") | ||
raise(ERROR_CLASS, response) if !response.success? | ||
doc = Nokogiri::HTML(response.body) | ||
data = doc.at("div#page-container")["data-store"] | ||
json = JSON.parse(data) | ||
@@session_id = json["SessionToken"]["public"]["broadcastHistory"]["token"]["session_id"] | ||
get("/getUserBroadcastsPublic", query: { user_id: user_id, session_id: @@session_id }) | ||
end | ||
end | ||
|
||
error PeriscopeError do |e| | ||
error App::PeriscopeError do |e| | ||
status 503 | ||
"There was a problem talking to Periscope. Please try again in a moment." | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,17 @@ | ||
# frozen_string_literal: true | ||
# https://developers.soundcloud.com/docs/api/reference | ||
|
||
class SoundcloudError < HTTPError; end | ||
module App | ||
class SoundcloudError < HTTPError; end | ||
|
||
class Soundcloud < HTTP | ||
BASE_URL = "https://api-v2.soundcloud.com" | ||
PARAMS = "client_id=#{ENV["SOUNDCLOUD_CLIENT_ID"]}" | ||
ERROR_CLASS = SoundcloudError | ||
class Soundcloud < HTTP | ||
BASE_URL = "https://api-v2.soundcloud.com" | ||
PARAMS = "client_id=#{ENV["SOUNDCLOUD_CLIENT_ID"]}" | ||
ERROR_CLASS = SoundcloudError | ||
end | ||
end | ||
|
||
error SoundcloudError do |e| | ||
error App::SoundcloudError do |e| | ||
status 503 | ||
"There was a problem talking to SoundCloud. Please try again in a moment." | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,57 @@ | ||
# frozen_string_literal: true | ||
# https://github.com/speedruncom/api/tree/master/version1 | ||
|
||
class SpeedrunError < HTTPError; end | ||
module App | ||
class SpeedrunError < HTTPError; end | ||
|
||
class Speedrun < HTTP | ||
BASE_URL = "https://www.speedrun.com/api/v1" | ||
HEADERS = { | ||
"User-Agent" => "rssbox", | ||
} | ||
ERROR_CLASS = SpeedrunError | ||
class Speedrun < HTTP | ||
BASE_URL = "https://www.speedrun.com/api/v1" | ||
HEADERS = { | ||
"User-Agent" => "rssbox", | ||
} | ||
ERROR_CLASS = SpeedrunError | ||
|
||
@@cache = {} | ||
@@cache = {} | ||
|
||
def self.resolve_id(type, id) | ||
@@cache[type] ||= {} | ||
return @@cache[type][id] if @@cache[type][id] | ||
value = $redis.get("speedrun:#{type}:#{id}") | ||
if value | ||
@@cache[type][id] = if type == "level-subcategories" | ||
JSON.parse(value) | ||
else | ||
value | ||
def self.resolve_id(type, id) | ||
@@cache[type] ||= {} | ||
return @@cache[type][id] if @@cache[type][id] | ||
value = $redis.get("speedrun:#{type}:#{id}") | ||
if value | ||
@@cache[type][id] = if type == "level-subcategories" | ||
JSON.parse(value) | ||
else | ||
value | ||
end | ||
return @@cache[type][id] | ||
end | ||
return @@cache[type][id] | ||
end | ||
|
||
if type == "game" | ||
response = Speedrun.get("/games/#{id}") | ||
raise(SpeedrunError, response) if !response.success? | ||
redis_value = value = response.json["data"]["names"]["international"] | ||
elsif type == "level-subcategories" | ||
response = Speedrun.get("/levels/#{id}/variables") | ||
raise(SpeedrunError, response) if !response.success? | ||
value = response.json["data"].select { |var| var["is-subcategory"] }.to_h do |var| | ||
[ | ||
var["id"], | ||
var["values"]["values"].to_h do |id, val| | ||
[id, val["label"]] | ||
end | ||
] | ||
if type == "game" | ||
response = Speedrun.get("/games/#{id}") | ||
raise(SpeedrunError, response) if !response.success? | ||
redis_value = value = response.json["data"]["names"]["international"] | ||
elsif type == "level-subcategories" | ||
response = Speedrun.get("/levels/#{id}/variables") | ||
raise(SpeedrunError, response) if !response.success? | ||
value = response.json["data"].select { |var| var["is-subcategory"] }.to_h do |var| | ||
[ | ||
var["id"], | ||
var["values"]["values"].to_h do |id, val| | ||
[id, val["label"]] | ||
end | ||
] | ||
end | ||
redis_value = value.to_json | ||
end | ||
redis_value = value.to_json | ||
end | ||
|
||
$redis.set("speedrun:#{type}:#{id}", redis_value) | ||
@@cache[type][id] = value | ||
return value | ||
$redis.set("speedrun:#{type}:#{id}", redis_value) | ||
@@cache[type][id] = value | ||
return value | ||
end | ||
end | ||
end | ||
|
||
error SpeedrunError do |e| | ||
error App::SpeedrunError do |e| | ||
status 503 | ||
"There was a problem talking to speedrun.com. Please try again in a moment." | ||
end |
Oops, something went wrong.