Skip to content

Commit

Permalink
Put all of my classes in an App module to avoid clashing with gems (e…
Browse files Browse the repository at this point in the history
….g. the http gem).
  • Loading branch information
stefansundin committed Jan 13, 2021
1 parent 22190cf commit aab5094
Show file tree
Hide file tree
Showing 18 changed files with 555 additions and 528 deletions.
226 changes: 113 additions & 113 deletions app.rb

Large diffs are not rendered by default.

12 changes: 7 additions & 5 deletions app/dailymotion.rb
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
15 changes: 8 additions & 7 deletions app/google.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# frozen_string_literal: true
# https://developers.google.com/youtube/v3/docs/
# https://developers.google.com/+/web/api/rest/

class GoogleError < HTTPError; end
module App
class GoogleError < HTTPError; end

class Google < HTTP
BASE_URL = "https://www.googleapis.com"
PARAMS = "key=#{ENV["GOOGLE_API_KEY"]}"
ERROR_CLASS = GoogleError
class Google < HTTP
BASE_URL = "https://www.googleapis.com"
PARAMS = "key=#{ENV["GOOGLE_API_KEY"]}"
ERROR_CLASS = GoogleError
end
end

error GoogleError do |e|
error App::GoogleError do |e|
status 503
if (e.data["error"]["errors"][0]["reason"] == "accessNotConfigured" rescue false)
"Please enable the appropriate API for this project in the Google Developer Console."
Expand Down
18 changes: 10 additions & 8 deletions app/imgur.rb
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
88 changes: 45 additions & 43 deletions app/instagram.rb
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
12 changes: 7 additions & 5 deletions app/mixcloud.rb
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
46 changes: 24 additions & 22 deletions app/periscope.rb
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
14 changes: 8 additions & 6 deletions app/soundcloud.rb
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
80 changes: 41 additions & 39 deletions app/speedrun.rb
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
Loading

0 comments on commit aab5094

Please sign in to comment.