|
| 1 | +import requests |
| 2 | +import json |
| 3 | +from bs4 import BeautifulSoup |
| 4 | +import random |
| 5 | +import time |
| 6 | +import traceback |
| 7 | +import os |
| 8 | +import sys |
| 9 | +import csv |
| 10 | + |
| 11 | +""" |
| 12 | +@author: Ademola Oyewale |
| 13 | +saopayne@gmail.com |
| 14 | +""" |
| 15 | + |
| 16 | + |
| 17 | +class PlayReviews: |
| 18 | + |
| 19 | + def get_reviews(self, app_id): |
| 20 | + sort_by_helpfulness = '1' |
| 21 | + reviews = [] |
| 22 | + page_num = 0 |
| 23 | + is_last_page = False |
| 24 | + |
| 25 | + while is_last_page is False: |
| 26 | + if page_num % 3 == 0: |
| 27 | + time.sleep(random.random() + 1) |
| 28 | + (reviews_html_string, is_last_page) = self.fetch_reviews(app_id, page_num, sort_by_helpfulness) |
| 29 | + if len(reviews_html_string) > 0: |
| 30 | + reviews.extend(self.parse_reviews(reviews_html_string)) |
| 31 | + page_num = page_num + 1 |
| 32 | + print('Downloading reviews on page:', page_num) |
| 33 | + return reviews |
| 34 | + |
| 35 | + def fetch_reviews(self, app_id, page_num, sort_by): |
| 36 | + url = "https://play.google.com/store/getreviews" |
| 37 | + querystring = {"authuser": "0"} |
| 38 | + payload = {'reviewType': '0', 'pageNum': page_num, 'id': app_id, 'xhr': '1', 'reviewSortOrder': sort_by} |
| 39 | + response = requests.request("POST", url, data=payload, params=querystring) |
| 40 | + |
| 41 | + if len(response.content) > 4: |
| 42 | + response = response.content[4:] |
| 43 | + try: |
| 44 | + response = json.loads(response) |
| 45 | + if len(response) > 0: |
| 46 | + if len(response[0]) == 4: |
| 47 | + reviews_html = response[0][2] |
| 48 | + if response[0][1] == 1: |
| 49 | + is_last_page = False |
| 50 | + else: |
| 51 | + is_last_page = True |
| 52 | + return reviews_html, is_last_page |
| 53 | + else: |
| 54 | + return '', True |
| 55 | + else: |
| 56 | + return '', True |
| 57 | + |
| 58 | + except ValueError as e: |
| 59 | + traceback.print_exc() |
| 60 | + print("Fetch prevented by Google") |
| 61 | + return '', True |
| 62 | + else: |
| 63 | + return '', True |
| 64 | + |
| 65 | + def parse_reviews(self, reviews_html_string): |
| 66 | + |
| 67 | + soup = BeautifulSoup(reviews_html_string, 'html.parser') |
| 68 | + review_body_list = soup.findAll("div", {"class": "review-body"}) |
| 69 | + review_author_list = soup.findAll("span", {"class": "author-name"}) |
| 70 | + review_date_list = soup.findAll("span", {"class": "review-date"}) |
| 71 | + review_title_list = soup.findAll("span", {"class": "review-title"}) |
| 72 | + review_rating_list = soup.findAll("div", {"class": "tiny-star"}) |
| 73 | + reviews = [] |
| 74 | + data_path = 'data/' |
| 75 | + if not os.path.exists(data_path): |
| 76 | + os.makedirs(data_path) |
| 77 | + |
| 78 | + writer = csv.writer(open('data/reviews.csv', 'a')) |
| 79 | + writer.writerow(['Title', 'Body:', 'Date', 'Author', 'Rating']) |
| 80 | + for i in range(len(review_body_list)): |
| 81 | + current_row = [review_title_list[i].text, review_body_list[i].text, review_date_list[i].text, |
| 82 | + review_author_list[i].text, |
| 83 | + review_rating_list[i]['aria-label']] |
| 84 | + writer.writerow(current_row) |
| 85 | + reviews.append(current_row) |
| 86 | + return reviews |
| 87 | + |
| 88 | + def fetch_and_write_reviews(self, application_id): |
| 89 | + self.get_reviews(application_id) |
| 90 | + return |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == '__main__': |
| 94 | + if len(sys.argv) > 1: |
| 95 | + app_id = sys.argv[1] |
| 96 | + plr = PlayReviews() |
| 97 | + plr.fetch_and_write_reviews(app_id) |
0 commit comments