2014年9月11日 星期四

Android 開發筆記 - 透過 gsutil 取得 Google Play App Customer Reviews

依照官方文件簡介,對於 Android app 的評論,系統都會儲存在 Google Cloud Storage 裡頭,需要透過 gsutil 取出來使用,在此透過 pip 安裝 gsutil:

$ sudo pip install gsutil

使用前,需要先到 Google Play Developer Console -> Your android app -> 評分與評論 -> 底下最下方找到 ID,如 pubsite_prod_rev_0123456789。

接著,先設定 gsutil 存取權限:

$ gsutil config
This command will create a boto config file at /home/username/.boto
containing your credentials, based on your responses to the following
questions.
Please navigate your browser to the following URL:
https://accounts.google.com/o/oauth2/auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdevstorage.full_control&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&client_id=############.apps.googleusercontent.com&access_type=offline
In your browser you should see a page that requests you to authorize access to Google Cloud Platform APIs and Services on your behalf. After you approve, an authorization code will be displayed.

Enter the authorization code:


並透過瀏覽器得到 authorization code,輸入完後,會在詢問 Project ID,這時請記得填寫類似這串 pubsite_prod_rev_0123456789 的 ID。

之後,就可以透過 gsutil cp -r gs://pubsite_prod_rev_0123456789/reviews/reviews* 取得所有評論。

$ gsutil cp -r gs://pubsite_prod_rev_0123456789/reviews/reviews_* android_review/

這些 reviews 都是 CSV 格式,且第一列有顯示欄位資訊:

Package Name,App Version,Reviewer Language,Reviewer Hardware Model,Review Submit Date and Time,Review Submit Millis Since Epoch,Review Last Update Date and Time,Review Last Update Millis Since Epoch,Star Rating,Review Title,Review Text,Developer Reply Date and Time,Developer Reply Millis Since Epoch,Developer Reply Text,Review Link

看來只要寫隻簡易的 tool 就可以處理完畢囉:

$ file reviews_*.csv
reviews_*.csv: Little-endian UTF-16 Unicode text, with very long lines


因此,寫 php script 的話,可以先用 iconv 轉 UTF-8 後,再搭配 str_getcsv 處理:

<?php
$raw = file_get_contents(...);
$result = iconv($in_charset = 'UTF-16LE' , $out_charset = 'UTF-8' , $raw);
if( false !== $result )
$raw = $result;

$raw_lines = explode("\n", $raw);
array_shift($raw_lines); // title
foreach( $raw_lines as $line ) {
$fields = str_getcsv($line);
print_r($fields);
}

沒有留言:

張貼留言