-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_appstore_reviews.py
228 lines (176 loc) · 5.3 KB
/
get_appstore_reviews.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import requests
import xml.etree.ElementTree as et
import csv
import sys
countries = {
'ar', # Argentina
'au', # Australia
'be', # Belgium
'br', # Brazil
'ca', # Canada
'cl', # Chile
'cn', # China
'co', # Colombia
'cr', # Costa Rica
'hr', # Croatia
'cz', # Czech Republic
'dk', # Denmark
'de', # Germany
'sv', # El Salvador
'es', # Spain
'fi', # Finland
'fr', # France
'gr', # Greece
'gt', # Guatemala
'hk', # Hong Kong
'hu', # Hungary
'in', # India
'id', # Indonesia
'ie', # Ireland
'il', # Israel
'it', # Italia
'kr', # Korea
'kw', # Kuwait
'lb', # Lebanon
'lu', # Luxembourg
'my', # Malaysia
'mx', # Mexico
'nl', # Netherlands
'nz', # New Zealand
'no', # Norway
'at', # Austria
'pk', # Pakistan
'pa', # Panama
'pe', # Peru
'ph', # Philippines
'pl', # Poland
'pt', # Portugal
'qa', # Qatar
'ro', # Romania
'ru', # Russia
'sa', # Saudi Arabia
'ch', # Switzerland
'sg', # Singapore
'sk', # Slovakia
'si', # Slovenia
'za', # South Africa
'lk', # Sri Lanka
'se', # Sweden
'tw', # Taiwan
'th', # Thailand
'tr', # Turkey
'ae', # United Arab Emirates
'gb', # United Kingdom
'us', # United States
've', # Venezuela
'vn', # Vietnam
'jp', # Japan
'do', # Dominican Republic
'ec', # Ecuador
'eg', # Egypt
'ee', # Estonia
'hn', # Honduras
'jm', # Jamaica
'kz', # Kazakhstan
'lv', # Latvia
'lt', # Lithuania
'mo', # Macao
'mt', # Malta
'md', # Moldova
'ni', # Nicaragua
'py', # Paraguay
'uy', # Uruguay
'by', # Belarus
'ua', # Ukraine
'al' # Albania
}
def get_reviews(appId, country):
page = 1
reviews = []
while True:
entries = get_reviews_for_page(appId, country, page)
if entries == None:
break
if len(entries) == 0:
break
page += 1
for entry in entries:
review = parse_review(entry)
if review != None:
review['country'] = country
reviews.append(review)
return reviews
def get_reviews_for_page(appId, country, page):
print("Start loading reviews for country: %s. Page: %d" % (country, page))
url = 'https://itunes.apple.com/%s/rss/customerreviews/page=%d/id=%s/sortBy=mostRecent/xml' % (country, page, appId)
response = requests.get(url)
root = et.fromstring(response.content)
return root.findall('{http://www.w3.org/2005/Atom}entry')
def parse_review(entry):
review = {}
rss_prefix = '{http://itunes.apple.com/rss}'
atom_prefix = '{http://www.w3.org/2005/Atom}'
review["review_id"] = get_text_value_from_entry(entry, atom_prefix, 'id')
review["version"] = get_text_value_from_entry(entry, rss_prefix, 'version')
review["rating"] = get_text_value_from_entry(entry, rss_prefix, 'rating')
review["review"] = get_review_from_entry(entry)
review["author"] = get_author_from_entry(entry)
review["title"] = get_text_value_from_entry(entry, atom_prefix, 'title')
review["updated"] = get_text_value_from_entry(entry, atom_prefix, 'updated')
return review
def get_text_value_from_entry(entry, prefix, name):
full_name = prefix + name
value = entry.find(full_name)
if value == None:
return ''
return value.text
def get_author_from_entry(entry):
value = entry.find('{http://www.w3.org/2005/Atom}author')
if value == None:
return ''
name = value.find('{http://www.w3.org/2005/Atom}name')
if name == None:
return ''
return name.text.replace('\n', '.')
def get_review_from_entry(entry):
for review in entry.findall('{http://www.w3.org/2005/Atom}content'):
if review.get('type') == 'text':
return review.text.replace('\n', '.')
return ''
if len(sys.argv) < 2:
print("Use: python get_appstore_reviews.py <app_id>")
exit(1)
app_id = sys.argv[1]
if app_id == None:
print("Use: python get_appstore_reviews.py <app_id>")
exit(1)
all_reviews = []
for countryCode in countries:
reviews = get_reviews(app_id, countryCode)
all_reviews.extend(reviews)
print('Downloaded %d reviews. Writing in file...' % len(all_reviews))
csvTitles = [
u'Id'.encode("utf-8"),
u'Title'.encode("utf-8"),
u'Author'.encode("utf-8"),
u'App. Version'.encode("utf-8"),
u'Rating'.encode("utf-8"),
u'Review'.encode("utf-8"),
u'Country'.encode("utf-8"),
u'Date'.encode("utf-8")
]
with open('./result.csv', "w") as file:
writer = csv.writer(file)
writer.writerow(csvTitles)
for review in all_reviews:
csvData = [
review['review_id'].encode("utf-8"),
review['title'].encode("utf-8"),
review['author'].encode("utf-8"),
review['version'].encode("utf-8"),
review['rating'].encode("utf-8"),
review['review'].encode("utf-8"),
review['country'].encode("utf-8"),
review['updated'].encode("utf-8"),
]
writer.writerow(csvData)