Class: HowLongToBeat::HTMLRequests

Inherits:
Object
  • Object
show all
Defined in:
lib/howlongtobeat/html_requests.rb

Defined Under Namespace

Classes: SearchInfo, SearchModifiers

Constant Summary collapse

BASE_URL =
'https://howlongtobeat.com'
REFERER_HEADER =
BASE_URL
GAME_URL =
"#{BASE_URL}/game"
SEARCH_URL =
"#{BASE_URL}/api/search"

Class Method Summary collapse

Class Method Details

.get_game_title(game_id) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/howlongtobeat/html_requests.rb', line 128

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) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/howlongtobeat/html_requests.rb', line 74

def get_search_request_data(game_name, search_modifiers = SearchModifiers::NONE, page = 1, search_info = 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

  payload.to_json
end

.get_search_request_headersObject



64
65
66
67
68
69
70
71
72
# File 'lib/howlongtobeat/html_requests.rb', line 64

def get_search_request_headers
  {
    'content-type' => 'application/json',
    'accept' => '*/*',
    'User-Agent' => random_user_agent,
    'referer' => REFERER_HEADER,
    'origin' => BASE_URL
  }
end

.send_web_request(game_name, search_modifiers = SearchModifiers::NONE, page = 1) ⇒ Object



119
120
121
122
123
124
125
126
# File 'lib/howlongtobeat/html_requests.rb', line 119

def send_web_request(game_name, search_modifiers = SearchModifiers::NONE, page = 1)
  token = fetch_search_token
  return nil unless token

  headers = get_search_request_headers.merge('x-auth-token' => token)
  payload = get_search_request_data(game_name, search_modifiers, page)
  make_request(SEARCH_URL, headers, payload)
end