forked from samuelclay/NewsBlur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreimport_stripe_history.py
45 lines (41 loc) · 1.49 KB
/
reimport_stripe_history.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
import stripe, datetime, time
stripe.api_key = settings.STRIPE_SECRET
week = (datetime.datetime.now() - datetime.timedelta(days=14)).strftime('%s')
failed = []
limit = 100
offset = 0
while True:
print " ---> At %s" % offset
try:
data = stripe.Customer.all(created={'gt': week}, count=limit, offset=offset)
except stripe.APIConnectionError:
time.sleep(10)
continue
customers = data['data']
if not len(customers):
print "At %s, finished" % offset
break
offset += limit
usernames = [c['description'] for c in customers]
for username in usernames:
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
print " ***> Couldn't find %s" % username
failed.append(username)
try:
if not user.profile.is_premium:
user.profile.activate_premium()
elif user.payments.all().count() != 1:
user.profile.setup_premium_history()
elif not user.profile.premium_expire:
user.profile.setup_premium_history()
elif user.profile.premium_expire > datetime.datetime.now() + datetime.timedelta(days=365):
user.profile.setup_premium_history()
else:
print " ---> %s is fine" % username
except stripe.APIConnectionError:
print " ***> Failed: %s" % username
failed.append(username)
time.sleep(2)
continue