-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
126 additions
and
1 deletion.
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
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "simple_inspect" | ||
|
||
module Itch | ||
# Data container for single review | ||
class Review | ||
include SimpleInspect | ||
|
||
attr_reader :id, :user_name, :user_id, :stars, :date, :review | ||
|
||
# rubocop:disable Metrics/ParameterLists | ||
def initialize(user_name:, user_id:, stars:, date:, review:, id: nil) | ||
@id = id | ||
@user_name = user_name | ||
@user_id = user_id | ||
@stars = stars | ||
@date = date | ||
@review = review | ||
end | ||
# rubocop:enable Metrics/ParameterLists | ||
end | ||
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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# frozen_string_literal: true | ||
|
||
require "json" | ||
|
||
require_relative "require_auth" | ||
require_relative "simple_inspect" | ||
require_relative "review" | ||
require_relative "request" | ||
|
||
module Itch | ||
# Fetch reviews | ||
class Reviews | ||
include SimpleInspect | ||
include RequireAuth | ||
include Request | ||
|
||
def initialize(agent, game_id) | ||
@agent = agent | ||
@game_id = game_id | ||
end | ||
|
||
def list | ||
all_reviews = [] | ||
|
||
page_number = 1 | ||
loop do | ||
page = with_login do | ||
@agent.get(review_url(page_number)) | ||
end | ||
|
||
raise Error, "Could not find game id #{@game_id} rewards" unless page.code == "200" | ||
|
||
page_reviews = page.css(".content_column .rating").map do |row| | ||
parse_row row | ||
end | ||
|
||
break if page_reviews.length == 0 | ||
all_reviews += page_reviews | ||
page_number += 1 | ||
sleep 0.5 | ||
end | ||
|
||
all_reviews | ||
end | ||
|
||
protected | ||
|
||
def parse_row(row) | ||
id = row.at_css('button[data-lightbox_url]')['data-lightbox_url'].split('/').last | ||
stars = row.css('.star_picker .icon-star').length | ||
header = row.at_css('.row_header') | ||
date = DateTime.parse header.at_css('abbr')['title'].strip | ||
|
||
user_link = header.at_css('.user_link') | ||
user_name = user_link.search('./text()').text.strip | ||
user_id = user_link['href'].split('/').last | ||
|
||
text = row.css('.blurb p').map(&:text) | ||
|
||
Review.new( | ||
id: id, | ||
user_name: user_name, | ||
user_id: user_id, | ||
stars: stars, | ||
date: date, | ||
review: text | ||
) | ||
end | ||
|
||
def review_url(page = 1) | ||
format(Itch::URL::REVIEWS, id: @game_id, page: page) | ||
end | ||
end | ||
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