-
Notifications
You must be signed in to change notification settings - Fork 0
/
apikey.py
80 lines (73 loc) · 2.03 KB
/
apikey.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
import json
from urllib import request, parse, error
import re
import socket
import tinify
def loadkey():
try:
fo = open("config.json", "r")
conf = json.loads(fo.read())
fo.close()
except:
raise Exception("Can't find your config.")
try:
key = conf['key']
except:
raise Exception("Can't find KEY!")
if len(key) != 32:
raise Exception("Can't find true KEY!")
return key
def inputkey(key):
if len(key) != 32:
raise Exception("Please enter true API-Key!")
try:
validate(key)
except tinify.Error as e:
raise e
try:
fo = open("config.json", "w+")
conf = fo.read()
except:
raise Exception("Can't write config! Please check your permission.")
try:
conf = json.loads(conf)
except:
conf = {}
conf['key'] = str(key)
fo.write(json.dumps(conf))
fo.close()
return True
def registerkey(name, mail):
if re.match(r'^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$', mail) is None:
raise Exception("Wrong e-mail address!")
timeout = 20
url = 'https://tinypng.com/web/subscription'
header = {
'Host': 'tinypng.com',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:58.0) Gecko/20100101 Firefox/58.0',
'Accept': 'application/json',
'Referer': 'https://tinypng.com/developers',
'content-type': 'application/json',
'Connection': 'keep-alive'
}
data = {
"fullName": name,
"mail": mail
}
data = json.dumps(data).encode('utf-8')
try:
socket.setdefaulttimeout(timeout)
req = request.Request(url, headers=header, data=data)
req = request.urlopen(req)
except error.HTTPError as e:
msg = json.loads(e.read())
raise Exception(msg['message'])
except error.URLError as e:
raise e
return True
def validate(key):
try:
tinify.key = key
tinify.validate()
except tinify.Error as e:
raise Exception(e)