Class: HowLongToBeat::HTMLRequests
- Inherits:
-
Object
- Object
- HowLongToBeat::HTMLRequests
- Defined in:
- lib/howlongtobeat/html_requests.rb
Defined Under Namespace
Classes: AuthStruct, SearchInfo, SearchModifiers
Constant Summary collapse
- BASE_URL =
'https://howlongtobeat.com'- REFERER_HEADER =
BASE_URL- GAME_URL =
"#{BASE_URL}/game"- SEARCH_URL =
HLTB renames this endpoint periodically (most recent: /api/find -> /api/finder -> /api/bleed). The runtime discovery in ‘send_website_request_getcode` is the source of truth; this constant is the fallback when discovery fails.
"#{BASE_URL}/api/bleed"
Class Method Summary collapse
- .get_game_title(game_id) ⇒ Object
- .get_search_request_data(game_name, search_modifiers = SearchModifiers::NONE, page = 1, search_info = nil, auth_struct = nil) ⇒ Object
- .get_search_request_headers(auth_struct = nil) ⇒ Object
- .send_web_request(game_name, search_modifiers = SearchModifiers::NONE, page = 1) ⇒ Object
Class Method Details
.get_game_title(game_id) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/howlongtobeat/html_requests.rb', line 166 def get_game_title(game_id) url = "#{GAME_URL}/#{game_id}" headers = get_title_request_headers contents = make_get_request(url, headers) return nil unless contents doc = Nokogiri::HTML(contents) title_tag = doc.title return nil unless title_tag title_text = title_tag title_text[12...-17]&.strip end |
.get_search_request_data(game_name, search_modifiers = SearchModifiers::NONE, page = 1, search_info = nil, auth_struct = nil) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/howlongtobeat/html_requests.rb', line 98 def get_search_request_data(game_name, search_modifiers = SearchModifiers::NONE, page = 1, search_info = nil, auth_struct = nil) payload = { searchType: 'games', searchTerms: game_name.split, searchPage: page, size: 20, searchOptions: { games: { userId: 0, platform: '', sortCategory: 'popular', rangeCategory: 'main', rangeTime: { min: 0, max: 0 }, gameplay: { perspective: '', flow: '', genre: '', difficulty: '' }, rangeYear: { max: '', min: '' }, modifier: search_modifiers }, users: { sortCategory: 'postcount' }, lists: { sortCategory: 'follows' }, filter: '', sort: 0, randomizer: 0 }, useCache: true } if search_info&.api_key payload[:searchOptions][:users][:id] = search_info.api_key end if auth_struct&.auth_key && auth_struct&.auth_value payload[auth_struct.auth_key] = auth_struct.auth_value end payload.to_json end |
.get_search_request_headers(auth_struct = nil) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/howlongtobeat/html_requests.rb', line 80 def get_search_request_headers(auth_struct = nil) headers = { 'content-type' => 'application/json', 'accept' => '*/*', 'User-Agent' => random_user_agent, 'Referer' => REFERER_HEADER, 'Origin' => BASE_URL } if auth_struct headers['x-auth-token'] = auth_struct.auth_token.to_s if auth_struct.auth_token headers['x-hp-key'] = auth_struct.auth_key.to_s if auth_struct.auth_key headers['x-hp-val'] = auth_struct.auth_value.to_s if auth_struct.auth_value end headers end |
.send_web_request(game_name, search_modifiers = SearchModifiers::NONE, page = 1) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/howlongtobeat/html_requests.rb', line 147 def send_web_request(game_name, search_modifiers = SearchModifiers::NONE, page = 1) search_info = send_website_request_getcode endpoint_candidates = build_endpoint_candidates(search_info&.search_url) endpoint_candidates.each do |endpoint| auth_struct = fetch_search_token(endpoint) next unless auth_struct headers = get_search_request_headers(auth_struct) payload = get_search_request_data(game_name, search_modifiers, page, search_info, auth_struct) search_url = "#{BASE_URL}#{endpoint}" response = make_request(search_url, headers, payload) return response if response end nil end |