Skip to content

Commit

Permalink
[v0.0.2.1] add score filtering feature
Browse files Browse the repository at this point in the history
  • Loading branch information
JoMingyu committed Feb 26, 2020
1 parent 76d1a69 commit 0a30bec
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@
## v0.0.2.0
> Added `reviews` feature
- `reviews` feature released with dirty codes.

## v0.0.2.1
> Added `filter_score_with` parameter to `reviews` feature
- New feature `reviews filtering with score` released.
11 changes: 6 additions & 5 deletions google_play_scraper/constants/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,22 @@ def build(self, lang, country):
return self.URL_FORMAT.format(lang=lang, country=country)

class _ReviewPayload(URLFormat):
PAYLOAD_FORMAT_FOR_FIRST_PAGE = "f.req=%5B%5B%5B%22UsvDTd%22%2C%22%5Bnull%2Cnull%2C%5B2%2C{sort}%2C%5B{count}%2Cnull%2Cnull%5D%2Cnull%2C%5B%5D%5D%2C%5B%5C%22{app_id}%5C%22%2C7%5D%5D%22%2Cnull%2C%22generic%22%5D%5D%5D"
PAYLOAD_FORMAT_FOR_PAGINATED_PAGE = "f.req=%5B%5B%5B%22UsvDTd%22%2C%22%5Bnull%2Cnull%2C%5B2%2C{sort}%2C%5B{count}%2Cnull%2C%5C%22{pagination_token}%5C%22%5D%2Cnull%2C%5B%5D%5D%2C%5B%5C%22{app_id}%5C%22%2C7%5D%5D%22%2Cnull%2C%22generic%22%5D%5D%5D"
PAYLOAD_FORMAT_FOR_FIRST_PAGE = "f.req=%5B%5B%5B%22UsvDTd%22%2C%22%5Bnull%2Cnull%2C%5B2%2C{sort}%2C%5B{count}%2Cnull%2Cnull%5D%2Cnull%2C%5Bnull%2C{score}%5D%5D%2C%5B%5C%22{app_id}%5C%22%2C7%5D%5D%22%2Cnull%2C%22generic%22%5D%5D%5D"
PAYLOAD_FORMAT_FOR_PAGINATED_PAGE = "f.req=%5B%5B%5B%22UsvDTd%22%2C%22%5Bnull%2Cnull%2C%5B2%2C{sort}%2C%5B{count}%2Cnull%2C%5C%22{pagination_token}%5C%22%5D%2Cnull%2C%5Bnull%2C{score}%5D%5D%2C%5B%5C%22{app_id}%5C%22%2C7%5D%5D%22%2Cnull%2C%22generic%22%5D%5D%5D"

def build(self, app_id, sort, count, pagination_token):
# type: (str, Sort, int, str) -> bytes
def build(self, app_id, sort, count, filter_score_with, pagination_token):
# type: (str, Sort, int, int, str) -> bytes
if pagination_token is not None:
result = self.PAYLOAD_FORMAT_FOR_PAGINATED_PAGE.format(
app_id=app_id,
sort=sort,
count=count,
score=filter_score_with,
pagination_token=pagination_token,
)
else:
result = self.PAYLOAD_FORMAT_FOR_FIRST_PAGE.format(
app_id=app_id, sort=sort, count=count
app_id=app_id, sort=sort, score=filter_score_with, count=count
)

return result.encode()
Expand Down
20 changes: 14 additions & 6 deletions google_play_scraper/features/reviews.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from typing import Optional

from google_play_scraper import Sort
from google_play_scraper.constants.element import ElementSpecs
Expand All @@ -7,10 +8,16 @@
from google_play_scraper.utils.request import post


def _fetch_review_items(url, app_id, sort, count, pagination_token):
def _fetch_review_items(url, app_id, sort, count, filter_score_with, pagination_token):
dom = post(
url,
Formats.ReviewPayload.build(app_id, sort, count, pagination_token),
Formats.ReviewPayload.build(
app_id,
sort,
count,
"null" if filter_score_with is None else filter_score_with,
pagination_token,
),
{"content-type": "application/x-www-form-urlencoded"},
)

Expand All @@ -19,9 +26,10 @@ def _fetch_review_items(url, app_id, sort, count, pagination_token):
return json.loads(match[0][2])[0], json.loads(match[0][2])[-1][-1]


def reviews(app_id, lang="en", country="us", sort=Sort.NEWEST, count=100):
# type: (str, str, str, Sort, int) -> list
# TODO filtering with rating
def reviews(
app_id, lang="en", country="us", sort=Sort.NEWEST, count=100, filter_score_with=None
):
# type: (str, str, str, Sort, int, Optional[int]) -> list
# TODO filtering with device model
# TODO reply data
# TODO refactoring
Expand All @@ -39,7 +47,7 @@ def reviews(app_id, lang="en", country="us", sort=Sort.NEWEST, count=100):

while True:
review_items, pagination_token = _fetch_review_items(
url, app_id, sort, _count, pagination_token
url, app_id, sort, _count, filter_score_with, pagination_token
)

for review in review_items:
Expand Down
11 changes: 11 additions & 0 deletions tests/e2e_tests/test_reviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,14 @@ def test_e2e_scenario_3(self):

for r in result:
self.assertEqual(5, r["score"])

def test_e2e_scenario_4(self):
for score in {1, 2, 3, 4, 5}:
result = reviews(
"com.mojang.minecraftpe",
sort=Sort.NEWEST,
count=300,
filter_score_with=score,
)

self.assertEqual(score * 300, sum([r["score"] for r in result]))

0 comments on commit 0a30bec

Please sign in to comment.