Add option to limit the time for which files will stay unlocked
Default is no limit (files stay unlocked indefinitely)
Simon Volpert
3 years ago
40 | 40 | api_key_unconfirmed = '' |
41 | 41 | api_unit = 'satoshi' |
42 | 42 | address = '{address}' |
43 | unlock_time = 0 | |
43 | 44 | |
44 | 45 | CONFIG_TEMPLATE = f'''# gmipay configuration file |
45 | 46 | # Set this to the absolute path where your paywalled files are located. This |
52 | 53 | # Set this to the default price any given file should be in BITs (1,000,000th |
53 | 54 | # of a Bitcoin Cash): |
54 | 55 | default_price={default_price} |
56 | # The period of time for which the files should stay unlocked, in days. If set | |
57 | # to 0, the files will stay unlocked indefinitely. | |
58 | unlock_time=0 | |
55 | 59 | |
56 | 60 | # Block explorer API options |
57 | 61 | # You can replace these with parameters for your preferred block explorer. |
200 | 204 | xpub = line[5:] |
201 | 205 | elif line.startswith('default_price='): |
202 | 206 | default_price = round(float(line[14:]), 2) |
207 | elif line.startswith('unlock_time='): | |
208 | unlock_time = int(line[12:]) | |
203 | 209 | elif line.startswith('api_url='): |
204 | 210 | api_url = line[8:] |
205 | 211 | elif line.startswith('api_key_balance='): |
258 | 264 | access_index = i |
259 | 265 | break |
260 | 266 | |
261 | timestamp = datetime.datetime.now().isoformat(' ', timespec='seconds') | |
267 | now = datetime.datetime.now() | |
268 | timestamp = now.isoformat(' ', timespec='seconds') | |
262 | 269 | # First request to access file by this client |
263 | 270 | if access_index == -1: |
264 | 271 | # Create an invoice |
269 | 276 | # Write to log file |
270 | 277 | write_log(f'{timestamp} -- New invoice for {request_file.name} by {cert_hash} on address {address}\n') |
271 | 278 | # Subsequent requests to access file by this client |
279 | # Check access expiry time for timed unlocks | |
280 | if unlock_time > 0 and record[1] == 'paid': | |
281 | old_timestamp = datetime.datetime.fromisoformat(record[4].replace(' ', 'T')) | |
282 | delta = datetime.timedelta(days=unlock_time) | |
283 | if now > old_timestamp + delta: | |
284 | record[1] = 'expired' | |
285 | record[3] = 'x' | |
286 | # Check whether payment was received | |
272 | 287 | if len(record) > 1 and record[1] != 'paid': |
273 | 288 | record[4] = timestamp |
274 | 289 | address = record[2] |
280 | 295 | balance = float(record[3]) |
281 | 296 | change = new_balance - balance |
282 | 297 | if change < default_price: |
298 | if record[1] == 'expired': | |
299 | write_log(f'{timestamp} -- Recreated invoice for {request_file.name} by {cert_hash} on address {address}\n') | |
300 | record[1] = 'invoice' | |
283 | 301 | write_access_data() |
284 | 302 | show_invoice() |
285 | 303 | raise SystemExit |