-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApplicationManager.py
204 lines (167 loc) · 4.97 KB
/
ApplicationManager.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
# -*- coding: utf-8 -*-
''' AppStore Reviews
Evgeniy Shurakov <github@shurakov.name>
'''
import datetime
import sys
from models.Application import Application
appStores = {
'Argentina': 143505,
'Australia': 143460,
'Belgium': 143446,
'Brazil': 143503,
'Canada': 143455,
'Chile': 143483,
'China': 143465,
'Colombia': 143501,
'Costa Rica': 143495,
'Croatia': 143494,
'Czech Republic': 143489,
'Denmark': 143458,
'Deutschland': 143443,
'El Salvador': 143506,
'Espana': 143454,
'Finland': 143447,
'France': 143442,
'Greece': 143448,
'Guatemala': 143504,
'Hong Kong': 143463,
'Hungary': 143482,
'India': 143467,
'Indonesia': 143476,
'Ireland': 143449,
'Israel': 143491,
'Italia': 143450,
'Korea': 143466,
'Kuwait': 143493,
'Lebanon': 143497,
'Luxembourg': 143451,
'Malaysia': 143473,
'Mexico': 143468,
'Nederland': 143452,
'New Zealand': 143461,
'Norway': 143457,
'Osterreich': 143445,
'Pakistan': 143477,
'Panama': 143485,
'Peru': 143507,
'Phillipines': 143474,
'Poland': 143478,
'Portugal': 143453,
'Qatar': 143498,
'Romania': 143487,
'Russia': 143469,
'Saudi Arabia': 143479,
'Schweiz/Suisse': 143459,
'Singapore': 143464,
'Slovakia': 143496,
'Slovenia': 143499,
'South Africa': 143472,
'Sri Lanka': 143486,
'Sweden': 143456,
'Taiwan': 143470,
'Thailand': 143475,
'Turkey': 143480,
'United Arab Emirates':143481,
'United Kingdom': 143444,
'United States': 143441,
'Venezuela': 143502,
'Vietnam': 143471,
'Japan': 143462,
'Dominican Republic': 143508,
'Ecuador': 143509,
'Egypt': 143516,
'Estonia': 143518,
'Honduras': 143510,
'Jamaica': 143511,
'Kazakhstan': 143517,
'Latvia': 143519,
'Lithuania': 143520,
'Macau': 143515,
'Malta': 143521,
'Moldova': 143523,
'Nicaragua': 143512,
'Paraguay': 143513,
'Uruguay': 143514
}
class ApplicationManager:
storage = None
fetcherFactory = None
parserFactory = None
application = None
reviewsLastDate = None
def __init__(self, appId, storage):
self.storage = storage
application = self.storage.getApplicationWithIdentifier(appId)
if application is None:
application = Application()
application.identifier = appId
self.application = application
def loadAndUpdateReviews(self, country = None, limit = 50):
if self.application is None:
return
if self.fetcherFactory is None or self.parserFactory is None:
return
if country is None:
self.reviewsLastDate = None
# i = 0
# total = len(appStores)
# progress = int((float(i) / float(total)) * 100.0)
# print "\r[{0}] {1}%".format('#'*(progress/10), progress)
for country in appStores:
self.loadAndUpdateReviews(country, limit)
# i += 1
# progress = int((float(i) / float(total)) * 100)
# print "\r[{0}] {1}%".format('#'*(progress/10), progress)
if self.application.reviewsLastDate is None:
self.application.reviewsLastDate = self.reviewsLastDate
elif self.reviewsLastDate is not None and self.application.reviewsLastDate < self.reviewsLastDate:
self.application.reviewsLastDate = self.reviewsLastDate
self.application.updated = datetime.datetime.now()
self.storage.replaceApplication(self.application)
else:
#print country
if country not in appStores:
print "Country not found"
return
appStoreId = appStores[country]
reviewsCount = 0
reviewsOrder = sys.maxint
lastDayReviews = None
fetcher = self.fetcherFactory.fetcher(self.application.identifier, appStoreId)
while reviewsCount < limit:
reviews = None
try:
parser = self.parserFactory.parser(fetcher.fetchNextPage())
reviews = parser.parse()
except Exception as ex:
print ex
break
if len(reviews) == 0:
break
reviewsCount += len(reviews)
for review in reviews:
if self.reviewsLastDate is None or self.reviewsLastDate < review.date:
self.reviewsLastDate = review.date
if self.application.reviewsLastDate is not None and self.application.reviewsLastDate > review.date:
reviewsCount = limit
break
if lastDayReviews is None:
lastDayReviews = self.storage.getReviews(appId = self.application.identifier, appStoreId = appStoreId, date = self.application.reviewsLastDate)
review.appId = self.application.identifier
review.appStoreId = appStoreId
oldReview = None
for _review in lastDayReviews:
if _review.identifier == review.identifier:
oldReview = _review
break
if oldReview is not None:
reviewsCount = limit
break
review.order = reviewsOrder
reviewsOrder -= 1
self.storage.replaceReview(review)
def getLatestReviews(self, country = None, limit = 10):
if self.application is None:
return
return self.storage.getReviews(appId = self.application.identifier, limit = limit)