common.py
7.1 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2020-02-10 23:12
# @Author : Lemon
# @File : common.py
# @Software: PyCharm
import datetime
import demjson
import base64
from enum import Enum
from attr import attrs, attrib
@attrs(cmp=False)
class WorkPlace():
employer_name = attrib("")
employer_id = attrib("")
position_text = attrib("")
position_id = attrib("")
location_name = attrib("")
location_id = attrib("")
description = attrib("")
current = attrib(False, type=bool)
start_time = attrib(None, type=datetime.date)
end_time = attrib(None, type=datetime.date)
def to_dict(self):
data = {
'employer_name': self.employer_name,
'employer_id': self.employer_id,
'position_text': self.position_text,
'position_id': self.position_id,
'location_name': self.location_name,
'location_id': self.location_id,
'description': self.description,
'date_start[year]': str(self.start_time.year),
'date_start[month]': str(self.start_time.month),
'date_start[day]': str(self.start_time.day),
'date_end[year]': str(self.end_time.year) if self.end_time else '',
'date_end[month]': str(self.end_time.month) if self.end_time else '',
'date_end[day]': str(self.end_time.day) if self.end_time else '',
}
if self.current:
data['date[current]'] = 'on'
return data
@classmethod
def from_dict(cls, data: dict):
self = cls()
for k, v in data.items():
if hasattr(self, k):
setattr(self, k, v)
start_time = []
end_time = []
for x in ['year', 'month', 'day']:
if data.get(f'date_start[{x}]'):
start_time.append(int(data.get(f'date_start[{x}]')))
if data.get(f'date_end[{x}]'):
end_time.append(int(data.get(f'date_end[{x}]')))
if start_time:
self.start_time = datetime.date(*start_time)
if end_time:
self.end_time = datetime.date(*end_time)
else:
self.current = True
return self
@attrs(cmp=False)
class College():
school_text = attrib("")
school_id = attrib("")
subject = attrib([], type=list, converter=lambda x: [] if x is None else x)
school_type = attrib("college")
description = attrib("")
graduated = attrib(False, type=bool) # 'graduated': 'on',
start_time = attrib(None, type=datetime.date)
end_time = attrib(None, type=datetime.date)
def to_dict(self):
data = {
'school_text': self.school_text,
'school_id': self.school_id,
'date_start[year]': str(self.start_time.year),
'date_start[month]': str(self.start_time.month),
'date_start[day]': str(self.start_time.day),
'date_end[year]': str(self.end_time.year) if self.end_time else '',
'date_end[month]': str(self.end_time.month) if self.end_time else '',
'date_end[day]': str(self.end_time.day) if self.end_time else '',
'description': self.description,
'school_type': self.school_type
}
if self.graduated:
data['graduated'] = 'on'
if self.subject:
for i, sub in enumerate(self.subject):
assert len(sub) == 2, '格式不对,要求格式(concentration_text,concentration_ids)'
data["concentration_text[%d]" % i] = sub[0]
data["concentration_ids[%d]" % i] = sub[1]
return data
@classmethod
def from_dict(cls, data: dict):
self = cls()
for k, v in data.items():
if hasattr(self, k):
setattr(self, k, v)
start_time = []
end_time = []
for x in ['year', 'month', 'day']:
if data.get(f'date_start[{x}]'):
start_time.append(int(data.get(f'date_start[{x}]')))
if data.get(f'date_end[{x}]'):
end_time.append(int(data.get(f'date_end[{x}]')))
if start_time:
self.start_time = datetime.date(*start_time)
if end_time:
self.end_time = datetime.date(*end_time)
self.graduated = True
return self
class TaskStatus():
EXCEPTION = -2
FAILED = -1
EXECUTE = 0
SUCCESS = 1
class Exchange(Enum):
CLIENT = 0
# SERVER = 1
# NSQ = 2
# MQTT = 3
@classmethod
def get(cls, code) -> Enum:
if isinstance(code, str):
code = int(code)
keys = [x for x in dir(cls) if not x.startswith('__')]
for key in keys:
member = getattr(cls, key)
if member.value == code:
return member
return None
def todict(obj, include: list = None):
keys = dir(obj)
res = {}
if include:
for key in include:
try:
value = getattr(obj, key)
res[key] = value
except:
pass
return res
else:
for key in keys:
if not key.startswith('_'):
value = getattr(obj, key)
res[key] = value
return res
def tobase64(obj) -> str:
if isinstance(obj, (dict, list)):
obj = demjson.encode(obj)
if isinstance(obj, str):
obj = obj.encode('utf-8')
if isinstance(obj, bytes):
bin = base64.b64encode(obj)
return str(bin, 'utf-8')
raise BaseException("must str,list,dict,bytes")
def frombase64(base) -> dict:
string = base64.b64decode(base).decode()
return demjson.decode(string)
def format_proxies(conf: dict, format='requests'):
'''
e.g.
{
'type': 'socks5',
'host': '47.56.152.111',
'pass': 'nantian888',
'port': '1080',
'user': 'ntkj'
}
'''
type_ = conf.get('type', None)
host = conf.get('host', None)
port = conf.get('port', None)
user = conf.get('user', None)
pass_ = conf.get('pass', None)
if format == 'requests':
info = []
info.append(type_)
info.append("://")
p_auth = []
if user:
p_auth.append(str(user))
p_auth.append(":")
if pass_:
p_auth.append(str(pass_))
if p_auth:
info.append(''.join(p_auth))
info.append('@')
info.append(host)
if port:
info.append(':')
info.append(str(port))
text = ''.join(info)
return {'http': text, 'https': text}
else:
_type_ = getattr(__import__('socks'), 'PROXY_TYPES').get(type_.upper())
assert _type_, '代理协议错误,可选socks5,socks4,http'
proxy = {
"proxy_type": _type_,
"proxy_addr": host,
"proxy_port": int(port),
}
if user:
proxy['proxy_username'] = user
if pass_:
proxy['proxy_password'] = pass_
return proxy
if __name__ == '__main__':
conf = {'type': 'http',
'host': '47.56.152.111',
'pass': 'nantian888',
'port': '1080',
'user': 'ntkj'}
print(format_proxies(conf, '1'))