0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

iOSアプリ評価件数、星平均数取得pythonスクリプトでサクッと定時集計

Last updated at Posted at 2021-04-19

#概要
iOSアプリレビュー件数、星平均数取得の2点を日本と海外でそれぞれ取得するスクリプトを作成する
cronで1日1回集計をdat.csvへ出力する
フォーマット:日付、レビュー件数(日本)、レビュー件数(アメリカ)、評価件数(日本)、評価件数(アメリカ)
出力例:20200520-22:00:16,80,16,15,5
(筆者はRaspberryPiを起動しっぱなしにしてるのでそこでこのスクリプトを実行させている)
環境:Python ver 3.5.3

##コーディング
・定義ファイルでAppStoreIDを自分のアプリの数字に書き換えてください
・空のdat.csvと名付けたファイルを同じフォルダに入れておいてください

const.py
## 定義ファイル
class const:
    AppStoreID = 9999999999
    url_jp_app_json = "http://itunes.apple.com/lookup?id="+str(AppStoreID)+"&country=JP"
    url_us_app_json = "https://itunes.apple.com/lookup?id="+str(AppStoreID)
    url_jp_comment_num = "https://itunes.apple.com/jp/rss/customerreviews/id="+str(AppStoreID)+"/json"
    url_en_comment_num = "https://itunes.apple.com/rss/customerreviews/id="+str(AppStoreID)+"/json"
    dat_csv_file = "/home/pi/project/myAppReviewChecker/dat.csv"
do.py
## 実行ファイル
import download
import csvFile
## アプリのレビュー数を取得し、通知する
##日本、アメリカAppStoreレビュー数を取得する
if __name__ == "__main__":
    dl = download.Downlaod()
    ## レビュー数を取得
    dl.getReviewCountAll()  
    ## レビューコメントの数を取得する
    dl.getReviewCommentCountAll()
    ## dat.csvで前回の記録と比較しなにか違えば追記を行う
    cf = csvFile.CsvFile()
    cf.loadRecord()
    if dl.reviewCountJP != cf.reviewCountJP or dl.reviewCountUS != cf.reviewCountUS or dl.reviewCommentCountJP != cf.reviewCommentCountJP or dl.reviewCommentCountUS != cf.reviewCommentCountUS:
        cf.saveNewRecord(dl.reviewCountJP,dl.reviewCountUS,dl.reviewCommentCountJP,dl.reviewCommentCountUS)
download.py
import const
import requests
import json
from collections import OrderedDict
import pprint

class Downlaod:
    ## --------------------------------------------------------------------
    reviewCountJP = 0
    reviewCountUS = 0
    reviewCommentCountJP = 0
    reviewCommentCountUS = 0
    ## --------------------------------------------------------------------
    def __init__(self):
        pass

    def getReviewCountAll(self):
        self.reviewCountJP = self.getReviewCount(const.const.url_jp_app_json)
        self.reviewCountUS = self.getReviewCount(const.const.url_us_app_json)

    def getReviewCount(self,url):
        r = requests.get(url)
        #print(r.content)
        d = json.loads(r.text)
        pprint.pprint(d['results'][0]['userRatingCount'], width=40)
        return d['results'][0]['userRatingCount']

    def getReviewCommentCountAll(self):
        self.reviewCommentCountJP = self.getReviewCommentCount(const.const.url_jp_comment_num)
        self.reviewCommentCountUS = self.getReviewCommentCount(const.const.url_en_comment_num)

    def getReviewCommentCount(self,url):
        r = requests.get(url)
        d = json.loads(r.text)
        count = len(d['feed']['entry']) 
        print(count)
        return count

#テスト用にmainを書いておく
if __name__ == "__main__":
    dl = Downlaod()
    dl.getReviewCountAll()
    dl.getReviewCommentCountAll()
    #print("done")
csvFile.py
## dat.csvファイル出力用
import const
from csv import reader
import datetime

class CsvFile:
    ## --------------------------------------------------------------------
    newRow = None
    timeStamp = None
    reviewCountJP = 0
    reviewCountUS = 0
    reviewCommentCountJP = 0
    reviewCommentCountUS = 0
    ## --------------------------------------------------------------------
    def __init__(self):
        pass

    def loadRecord(self):
        with open(const.const.dat_csv_file, "r") as f:
            data = reader(f)
            for row in data:
                self.newRow = row
        print(self.newRow)
        self.timeStamp = row[0]
        self.reviewCountJP = int(row[1])
        self.reviewCountUS = int(row[2])
        self.reviewCommentCountJP = int(row[3])
        self.reviewCommentCountUS = int(row[4])
    
    def saveNewRecord(self,reviewCountJP,reviewCountUS,reviewCommentCountJP,reviewCommentCountUS):
        self.reviewCountJP = reviewCountJP
        self.reviewCountUS = reviewCountUS
        self.reviewCommentCountJP = reviewCommentCountJP
        self.reviewCommentCountUS = reviewCommentCountUS
        self.writeNewRecord()
        pass    

    def writeNewRecord(self):
        dt_now = datetime.datetime.now()
        self.timeStamp = dt_now.strftime('%Y%m%d-%H:%M:%S')
        newDat = str(self.timeStamp)+","+str(self.reviewCountJP)+","+str(self.reviewCountUS)+","+str(self.reviewCommentCountJP)+","+str(self.reviewCommentCountUS)
        #newDat = str(self.timeStamp)+","+self.reviewCountJP+","+self.reviewCountUS+","+self.reviewCommentCountJP+","+self.reviewCommentCountUS
        with open(const.const.dat_csv_file, "a") as f:
            data = reader(f)
            print(newDat, file=f)

if __name__ == "__main__":
    cf = CsvFile()
    cf.loadRecord()
    cf.writeNewRecord()
    pass

##cron設定
crontab内で以下のように設定し毎日22時に集計する

0 22 * * * python3 /home/pi/project/myAppReviewChecker/do.py
0
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?