Skip to content

Commit

Permalink
Add review fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
Billiam committed Apr 3, 2022
1 parent 85e700f commit 1f84d1c
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 1 deletion.
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,27 @@ client.game(12345).css
client.game(12345).css = "body { background-color: green; }"
```

### Reviews

Fetch current reviews

```ruby
reviews = client.game(12345).reviews.list
#=>
[
#<Itch::Review:0x0000557aa98c7930
@id="123456789",
@user_name="Billiam",
@user_id="billiam",
@date=<DateTime: 2022-04-03T10:02:00+00:00>,
@stars=4,
@review=[
"I really like this game",
"So much that I've added a second paragraph"
],
]
```

### Rewards

Fetch reward CSV data
Expand Down Expand Up @@ -252,7 +273,8 @@ filtered_rewards = rewards.select do |reward|
end
client.game(12345).rewards.save(filtered_rewards)
```
### Bundles

## Bundles

Fetch current bundles

Expand Down
5 changes: 5 additions & 0 deletions lib/itch/game.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require_relative "reviews"
require_relative "rewards"
require_relative "reward"
require_relative "require_auth"
Expand Down Expand Up @@ -48,6 +49,10 @@ def rewards
Rewards.new(@agent, @id)
end

def reviews
Reviews.new(@agent, @id)
end

def reward(id)
rewards.find { |reward| reward.id == id }
end
Expand Down
23 changes: 23 additions & 0 deletions lib/itch/review.rb
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
74 changes: 74 additions & 0 deletions lib/itch/reviews.rb
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
1 change: 1 addition & 0 deletions lib/itch_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module URL
LOGIN = "https://itch.io/login"
MONTH_PURCHASES_CSV = "https://itch.io/export-purchases/by-date/%<month>d-%<year>d"
PURCHASES_CSV = "https://itch.io/export-purchases/all"
REVIEWS = "https://itch.io/game/ratings/%<id>d?page=%<page>d"
REWARD_CSV = "https://itch.io/game/rewards/%<id>d/claimed?format=csv"
REWARDS = "https://itch.io/game/rewards/%<id>d"
TOTP_FRAGMENT = "https://itch.io/totp/verify/"
Expand Down

0 comments on commit 1f84d1c

Please sign in to comment.