Class: AndroidFetcher

Inherits:
ReviewFetcher show all
Defined in:
lib/AndroidFetcher.rb

Instance Attribute Summary collapse

Attributes inherited from ReviewFetcher

#config, #logger, #platform, #processors

Instance Method Summary collapse

Methods inherited from ReviewFetcher

#getPlatformLatestCheckTimestamp, #isSentWelcomeMessage, #processReviews, #registerProcessor, #sendWelcomMessage, #setPlatformLatestCheckTimestamp, #setSentWelcomeMessage

Constructor Details

#initialize(config) ⇒ AndroidFetcher

Returns a new instance of AndroidFetcher.



12
13
14
15
16
17
18
19
20
# File 'lib/AndroidFetcher.rb', line 12

def initialize(config)
    @processors = []
    @config = config
    @platform = 'Android'
    @logger = ZLogger.new(config.baseExecutePath)
    @googleAPI = GoogleAPI.new(config.keyFilePath, config.baseExecutePath, ["https://www.googleapis.com/auth/androidpublisher"])

    puts "[AndroidFetcher] Init Success."
end

Instance Attribute Details

#googleAPIObject

Returns the value of attribute googleAPI.



10
11
12
# File 'lib/AndroidFetcher.rb', line 10

def googleAPI
  @googleAPI
end

#tokenObject

Returns the value of attribute token.



10
11
12
# File 'lib/AndroidFetcher.rb', line 10

def token
  @token
end

Instance Method Details

#executeObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/AndroidFetcher.rb', line 22

def execute()

    latestCheckTimestamp = getPlatformLatestCheckTimestamp()

    puts "[AndroidFetcher] Start execute(), latestCheckTimestamp: #{latestCheckTimestamp}"

    reviews = []

    reviewsInfoLink = "https://androidpublisher.googleapis.com/androidpublisher/v3/applications/#{config.packageName}/reviews"

    # Note: You can retrieve only the reviews that users have created or modified within the last week. 
    # https://developers.google.com/android-publisher/reply-to-reviews#retrieving_a_set_of_reviews

    puts "[AndroidFetcher] Fetch reviews in #{config.packageName}"

    loop do
        reviewsInfo = googleAPI.request(reviewsInfoLink)
        reviewsInfoLink = reviewsInfo&.dig("tokenPagination", "nextPageToken")

        customerReviews = reviewsInfo["reviews"]

        if customerReviews.nil?
            break
        end

        puts "[AndroidFetcher] Fetch reviews in #{config.packageName}, count: #{customerReviews.length}"
        customerReviews.each do |customerReview|

            customerReviewID = customerReview&.dig("reviewId")
            customerReviewTitle = nil
            customerReviewReviewerNickname = customerReview&.dig("authorName")
            customerReviewVersionString = "unknown"
            customerReviewPlatform = platform
            customerReviewCreatedDateTimestamp = 0
            
            comment = customerReview&.dig("comments", 0, "userComment")
            if !comment.nil?
                customerReviewBody = comment&.dig("text")
                customerReviewTerritory = comment&.dig("reviewerLanguage")

                if !customerReviewBody.nil?
                    customerReviewBody = customerReviewBody.strip
                end

                customerReviewRating = comment&.dig("starRating")
                if !customerReviewRating.nil?
                    customerReviewRating = customerReviewRating.to_i
                end

                customerReviewCreatedDateTimestamp = comment&.dig("lastModified", "seconds")
                if !customerReviewCreatedDateTimestamp.nil?
                    customerReviewCreatedDateTimestamp = customerReviewCreatedDateTimestamp.to_i
                end

                androidOSVersion = comment&.dig("androidOsVersion")
                if !androidOSVersion.nil?
                    customerReviewPlatform = "#{platform} #{androidOSVersion}"
                end

                versionString = comment&.dig("appVersionName")
                if !versionString.nil?
                    customerReviewVersionString = "#{versionString}"

                    versionCode = comment&.dig("appVersionCode")
                    if !versionCode.nil?
                        customerReviewVersionString = "#{customerReviewVersionString}(#{versionCode})"
                    end
                end

                deviceInfo = []
                manufacturer = comment&.dig("deviceMetadata", "manufacturer")
                if !manufacturer.nil?
                    deviceInfo.append(manufacturer)
                end

                productName = comment&.dig("deviceMetadata", "productName")
                if !productName.nil?
                    deviceInfo.append(productName)
                end

                if deviceInfo.length > 0
                    customerReviewReviewerNickname = "#{customerReviewReviewerNickname} - #{deviceInfo.join("/")}"
                end
            end

            if latestCheckTimestamp >= customerReviewCreatedDateTimestamp
                reviewsInfoLink = nil
                break
            else
                url = "https://play.google.com/store/apps/details?id=#{config.packageName}&reviewId=#{customerReviewID}"
                if !config.accountID.nil? && !config.appID.nil?
                    url = "https://play.google.com/console/developers/#{config.accountID}/app/#{config.appID}/user-feedback/review-details?reviewId=#{customerReviewID}"
                end
                reviews.append(Review.new(customerReviewPlatform, customerReviewID, customerReviewReviewerNickname, customerReviewRating, customerReviewTitle, customerReviewBody, customerReviewCreatedDateTimestamp, url, customerReviewVersionString, customerReviewTerritory, {}))
                
                # init first time, need first review to set as latestCheckTimestamp
                if latestCheckTimestamp == 0
                    reviewsInfoLink = nil
                    break
                end
            end
        end
        
        break if reviewsInfoLink.nil?
    end

    puts "[AndroidFetcher] Fetch reviews in #{config.packageName}, total reviews count: #{reviews.length}"

    if reviews.length > 0
        reviews = reviews.sort! { |a, b|  a.createdDateTimestamp <=> b.createdDateTimestamp }

        puts "[AndroidFetcher] latest review: #{reviews.last.body}, #{reviews.last.createdDateTimestamp}"
        setPlatformLatestCheckTimestamp(reviews.last.createdDateTimestamp)
    end


    # init first time, send welcome message
    if latestCheckTimestamp == 0 && isSentWelcomeMessage() == false
        sendWelcomMessage()
        setSentWelcomeMessage()
    elsif reviews.length > 0
        processReviews(reviews, platform)
    end
end