forked from cbenhagen/python-turboactivate
-
Notifications
You must be signed in to change notification settings - Fork 2
/
example.py
executable file
·200 lines (151 loc) · 7.4 KB
/
example.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
from turboactivate import (
TurboActivate,
IsGenuineResult,
TurboActivateError,
TurboActivateTrialExpiredError,
TA_USER,
TA_SYSTEM,
TA_CB_EXPIRED,
TA_CB_EXPIRED_FRAUD
)
import sys
# Don't use 0 for either of these values.
# We recommend 90, 14. But if you want to lower the values
# we don't recommend going below 7 days for each value.
# Anything lower and you're just punishing legit users.
DAYS_BETWEEN_CHECKS = 90
GRACE_PERIOD_LENGTH = 14
'''
This function will be called by a separate background thread to notify
your app of trial expiration (either naturally, or because of customer fraud).
That means if you're displaying UI to your users you must ensure
that any windows (or any resource sharing for that matter) are
created in the right thread context or bad things might happen.
Test this behavior well before releasing to your end-users.
'''
def trial_callback(status, unused):
if status == TA_CB_EXPIRED:
# TODO: disallow any features in your app.
print("The app trial period has expired")
elif status == TA_CB_EXPIRED_FRAUD:
# TODO: disallow any features in your app.
print("The lease has been dropped due to computer sleeping.")
else:
print("The app trial callback returned an unexpected status: ", status)
if __name__ == "__main__":
# support both Python 2 and 3
# for this simple example app
try:
input = raw_input
except NameError:
pass
# now begins the licensing bit of the code
isGenuine = False
try:
# TODO: go to the version page at LimeLM and
# paste this GUID here
ta = TurboActivate("18324776654b3946fc44a5f3.49025204", TA_USER)
# Check if we're activated, and every 90 days verify it with the activation servers
# In this example we won't show an error if the activation was done offline
# (see the 3rd parameter of the IsGenuine() function)
# https://wyday.com/limelm/help/offline-activation/
gen_r = ta.is_genuine_ex(DAYS_BETWEEN_CHECKS, GRACE_PERIOD_LENGTH, True)
isGenuine = (gen_r == IsGenuineResult.Genuine
or gen_r == IsGenuineResult.GenuineFeaturesChanged
# an internet error means the user is activated but
# TurboActivate failed to contact the LimeLM servers
or gen_r == IsGenuineResult.InternetError
)
if not isGenuine and ta.is_activated():
# There is still activation data on the computer, and it's valid.
# This means that IsGenuineEx() is saying "not activated" (a.k.a. TA_FAIL)
# because the customer blocked connections to the activation servers (intentionally or not)
# for nDaysBetweenChecks + nGraceDaysOnInetErr days.
# What you should do now is prompt the user telling them before they can use your app that they need
# to reverify with the activation servers.
print('You must reverify with the activation servers before you can use this app. ')
print('Type R and then press enter to retry after you\'ve ensured that you\'re connected to the internet. ')
print('Or to exit the app press X. ')
while True:
user_resp = sys.stdin.read(1)
if user_resp == 'x' or user_resp == 'X':
sys.exit("Exiting now. Bye.")
if user_resp == 'r' or user_resp == 'R':
# Now we're using TA_IsGenuine() to retry immediately. Note that we're not using
# TA_IsGenuineEx() because TA_IsGenuineEx() waits 5 hours after an internet failure
# before retrying to contact the servers. TA_IsGenuine() retries immediately.
igr = ta.is_genuine()
if igr == IsGenuineResult.Genuine or igr == IsGenuineResult.GenuineFeaturesChanged:
print('Successfully reverified with the servers! You can now continue to use the app!')
break
else:
print('Failed to reverify with the servers. ')
print('Make sure you\'re connected to the internet and that you\'re not blocking access to the activation servers. ')
print('Then press R to retry again. ')
else:
print('Invalid input. Press R to try to reverify with the servers. Press X to exit the app.')
except TurboActivateError as e:
sys.exit("Failed to check if activated: " + str(e))
trial_days = 0
# We're going to use verified trials:
# https://wyday.com/limelm/help/trials/#verified
verified_trial = True
# Get the number of trial days remaining and print them
if not isGenuine:
try:
# Start or re-validate the trial if it has already started.
# This need to be called at least once before you can use
# any other trial functions.
ta.use_trial(verified_trial, "", trial_callback)
# Get the number of trial days remaining.
trial_days = ta.trial_days_remaining(verified_trial)
if trial_days > 0:
print("Trial days remaining %d" % trial_days)
else:
print("There are no trial days remaining. You must activate now to continue to use this app.")
except TurboActivateTrialExpiredError as e:
print("There are no trial days remaining. You must activate now to continue to use this app.")
except TurboActivateError as e:
print("Failed to start the trial: " + str(e))
# Whether to prompt for the product key
prompt_for_key = False
if not isGenuine:
# ask the user if they want to enter their pkey
print('Would you like to enter your pkey (y/n) [n]: ')
prompt_res = sys.stdin.read(1)
if prompt_res != "" and prompt_res == "y":
prompt_for_key = True
else:
prompt_for_key = False
# Now actually prompt for the product key and try to activate
if prompt_for_key:
try:
# prompt the user for a product key
pkey = input('Enter your product key to activate: ')
if ta.check_and_save_pkey(pkey):
print("Product key saved successfully.")
else:
sys.exit("Product key was not valid for this product version")
except TurboActivateError as e:
sys.exit("Failed to check or save product key: " + str(e))
# try to activate the product key
try:
ta.activate()
isGenuine = True
print("Activated successfully!")
except TurboActivateError as e:
sys.exit("Failed to activate online: " + str(e))
# Prevent the user from going any further if they're not activated.
if not isGenuine and trial_days == 0:
sys.exit("You're not activated and there are no more trial days. You must activate to use this app.")
# The customer is activated or is in trial mode!
# From this point on is the "meat" of your app.
# if this app is activated then you can get a feature value (completely optional)
# See: https://wyday.com/limelm/help/license-features/
#
# feature_value = ta.get_feature_value("myFeature")
# print("the value of myFeature is %s" % feature_value)
print("Hello world!")