2
0

More than 3 years have passed since last update.

Pythonを使い、iOSアプリのレビューを取得して、Lineで知る

Last updated at Posted at 2020-02-18

自身の出したアプリのレビュー件数を取って毎日Lineで知りたい時ないですか?
自分にはありました。
そこで、作成。

今回はiOS用に作成したものになります。

使用ライブラリ

  • requests
    • APIを叩くのに
  • urllib3
    • レスポンスをJSONに分解
  • line-bot-sdk
    • LineBOT

コマンド

pip install line-bot-sdk
pip install requests
pip install urllib3

実際のコード

GetReview.py

from linebot import LineBotApi
from linebot.models import TextSendMessage
from linebot.exceptions import LineBotApiError
import requests
import json
import urllib.request
import urllib.parse

#LINEでメッセージを送るためのキー
LINE_ACSESS_KEY = "LINE_ACSESS_KEY"
#送信先のLINEのID
LINE_USER_KEY = "LINE_USER_KEY"

def push_info(json_input, context):
    line_bot_api = LineBotApi(LINE_ACSESS_KEY)
    text_message = get_text_message()
    try:
        line_bot_api.push_message(LINE_USER_KEY, TextSendMessage(text=text_message))
    except LineBotApiError as e:
        print(e)

def get_text_message():
    review_url = "https://itunes.apple.com/jp/rss/customerreviews/id=アプリID/json"
    headers = {
        "Content-Type":"application/json",
    }
    response_obj = urllib.request.Request(review_url, headers=headers)
    #tryでエラーハンドリング
    try:
       with urllib.request.urlopen(response_obj) as response:
        response_body = response.read().decode("utf-8")
        result_objs = json.loads(response_body)
        result_obj = result_objs["feed"]
        review_count = len(result_obj["entry"])
        messages = "レビュー数は" + str(review_count) + "件です"
        return messages
    except urllib.error.HTTPError as e:
        if e.code >= 400:
            messages = e.reason
            return messages
        else:
            return e

これを圧縮して、AWSLambdaなどに設置してCloudWatchで定期実行すれば、レビューが全件取れます。
Lambdaにあげる時は、作業ディレクトリに移動(コマンド「cd」で)後、ライブラリのインストール時にオプションで「-t」 をつけてください。

注意

その時の全件(コメントのついたレビュー)しか取れないので、差分だけ欲しいなどの時は、取得したデータをDBなどに保存して差分を出す必要があります。

github

2
0
3

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
2
0