Skip to content

Commit a7ac3f5

Browse files
committed
Twitch: Support new /directory/category/ URLs to look up game feeds.
1 parent eef8aee commit a7ac3f5

File tree

3 files changed

+59
-18
lines changed

3 files changed

+59
-18
lines changed

app.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,9 @@
701701
if /twitch\.tv\/directory\/game\/(?<game_name>[^\/?#]+)/ =~ params[:q]
702702
# https://www.twitch.tv/directory/game/Perfect%20Dark
703703
game_name = Addressable::URI.unescape(game_name)
704+
elsif /twitch\.tv\/directory\/category\/(?<category_slug>[^\/?#]+)/ =~ params[:q]
705+
# https://www.twitch.tv/directory/category/perfect-dark-2000
706+
category_slug = Addressable::URI.unescape(category_slug)
704707
elsif /twitch\.tv\/directory/ =~ params[:q]
705708
# https://www.twitch.tv/directory/all/tags/7cefbf30-4c3e-4aa7-99cd-70aabb662f27
706709
return [404, "Unsupported url."]
@@ -728,6 +731,10 @@
728731
return [422, "Something went wrong. Try again later."] if path.nil?
729732
return [422, path] if path.start_with?("Error:")
730733
redirect Addressable::URI.new(path: "/twitch/directory/game/#{path}").normalize.to_s, 301
734+
elsif category_slug
735+
path = App::TwitchGraphQL.resolve_category_slug(category_slug)
736+
return [422, path] if path.start_with?("Error:")
737+
redirect Addressable::URI.new(path: "/twitch/directory/game/#{path}").normalize.to_s, 301
731738
elsif vod_id
732739
path, _ = App::Cache.cache("twitch.vod", vod_id, 60*60, 60) do
733740
response = App::Twitch.get("/videos", query: { id: vod_id })

app/http.rb

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,21 @@
33
module App
44
class HTTP
55
def self.get(url, options={})
6-
relative_url = (url[0] == "/")
6+
relative_url = (url.nil? || url[0] == "/")
77

8-
if defined?(self::BASE_URL) && relative_url
9-
url = self::BASE_URL+url
10-
end
8+
if url.nil?
9+
url = self::BASE_URL
10+
else
11+
if defined?(self::BASE_URL) && relative_url
12+
url = self::BASE_URL+url
13+
end
1114

12-
if defined?(self::PARAMS) && relative_url
13-
if url["?"]
14-
url += "&"+self::PARAMS
15-
else
16-
url += "?"+self::PARAMS
15+
if defined?(self::PARAMS) && relative_url
16+
if url["?"]
17+
url += "&"+self::PARAMS
18+
else
19+
url += "?"+self::PARAMS
20+
end
1721
end
1822
end
1923

@@ -46,17 +50,21 @@ def self.get(url, options={})
4650
end
4751

4852
def self.post(url, data, options={})
49-
relative_url = (url[0] == "/")
53+
relative_url = (url.nil? || url[0] == "/")
5054

51-
if defined?(self::BASE_URL) && relative_url
52-
url = self::BASE_URL+url
53-
end
55+
if url.nil?
56+
url = self::BASE_URL
57+
else
58+
if defined?(self::BASE_URL) && relative_url
59+
url = self::BASE_URL+url
60+
end
5461

55-
if defined?(self::PARAMS) && relative_url
56-
if url["?"]
57-
url += "&"+self::PARAMS
58-
else
59-
url += "?"+self::PARAMS
62+
if defined?(self::PARAMS) && relative_url
63+
if url["?"]
64+
url += "&"+self::PARAMS
65+
else
66+
url += "?"+self::PARAMS
67+
end
6068
end
6169
end
6270

app/services/twitch.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,32 @@ class TwitchToken < HTTP
1919
}
2020
ERROR_CLASS = TwitchError
2121
end
22+
23+
class TwitchGraphQL < HTTP
24+
BASE_URL = "https://gql.twitch.tv/gql"
25+
HEADERS = {
26+
"Client-ID" => ENV["TWITCHTOKEN_CLIENT_ID"],
27+
"Content-Type" => "application/json",
28+
}
29+
ERROR_CLASS = TwitchError
30+
31+
def self.resolve_category_slug(slug)
32+
path, _ = Cache.cache("twitch.category", slug, 7*24*60*60, 60*60) do
33+
response = self.post(nil,
34+
{
35+
"query": "query category($slug: String!) { game(slug: $slug) { id name } }",
36+
"variables": { "slug": slug },
37+
}.to_json,
38+
)
39+
raise(TwitchError, response) if !response.success? || !response.json
40+
data = response.json["data"]["game"]
41+
next "Error: Can't find a category with that name." if data.nil?
42+
43+
"#{data["id"]}/#{data["name"]}"
44+
end
45+
path
46+
end
47+
end
2248
end
2349

2450
error App::TwitchError do |e|

0 commit comments

Comments
 (0)