作者 lemon

新增UA缓存持久化

@@ -26,19 +26,24 @@ class CallBack(): @@ -26,19 +26,24 @@ class CallBack():
26 def _task_(self, type_, client, taskid: int, code: int, msg: dict = None): 26 def _task_(self, type_, client, taskid: int, code: int, msg: dict = None):
27 print('【%s】' % type_, code, msg) 27 print('【%s】' % type_, code, msg)
28 28
29 - def onLoggingIn(self, email, password, cookie): 29 + def onLoggingIn(self, email, password, cookie, user_agent=None):
30 user_obj = UserList.get(email=email) 30 user_obj = UserList.get(email=email)
31 if not user_obj: 31 if not user_obj:
32 - user_obj = UserList.insert(email=email, password=password, status=Status.LOGGINE, cookie=cookie) 32 + user_obj = UserList.insert(email=email, password=password, status=Status.LOGGINE, cookie=cookie,
  33 + user_agent=user_agent)
33 else: 34 else:
34 user_obj.set(status=Status.LOGGINE) 35 user_obj.set(status=Status.LOGGINE)
35 36
36 return user_obj 37 return user_obj
37 38
38 def onLoggedIn(self, client: FacebookClient): 39 def onLoggedIn(self, client: FacebookClient):
39 - client.user_obj.set(fbid=client.uid,  
40 - status=Status.ONLINE,  
41 - cookie=client.get_cookie()) 40 + client.user_obj.set(
  41 + fbid=client.uid,
  42 + status=Status.ONLINE,
  43 + cookie=client.get_cookie(),
  44 + user_agent=client.get_user_agent()
  45 + )
  46 +
42 self._notify_( 47 self._notify_(
43 type_="account", 48 type_="account",
44 client=client, 49 client=client,
@@ -58,13 +58,13 @@ class Monitor(callback.CallBack): @@ -58,13 +58,13 @@ class Monitor(callback.CallBack):
58 cfunc = functools.partial(mfunc, client) 58 cfunc = functools.partial(mfunc, client)
59 setattr(client, fname, cfunc) 59 setattr(client, fname, cfunc)
60 60
61 - def login(self, email, password, cookie=None): 61 + def login(self, email, password, cookie=None, user_agent=None):
62 client = self._get_member(email) 62 client = self._get_member(email)
63 if client: 63 if client:
64 self.onLoggedIn(client) # 已登录就直接触发登录成功 64 self.onLoggedIn(client) # 已登录就直接触发登录成功
65 return client.info() 65 return client.info()
66 else: 66 else:
67 - obj = self.onLoggingIn(email, password, cookie) # 返回db中的user对象 67 + obj = self.onLoggingIn(email, password, cookie, user_agent) # 返回db中的user对象
68 try: 68 try:
69 client = FacebookClient(obj) 69 client = FacebookClient(obj)
70 client.user_obj = obj 70 client.user_obj = obj
@@ -168,7 +168,7 @@ class Monitor(callback.CallBack): @@ -168,7 +168,7 @@ class Monitor(callback.CallBack):
168 type_ = cmd.get('type') 168 type_ = cmd.get('type')
169 taskid = int(cmd.get('taskId')) 169 taskid = int(cmd.get('taskId'))
170 target = cmd.get('object') 170 target = cmd.get('object')
171 - payload = cmd.get('data', '{}') # str or jsonstr 171 + payload = cmd.get('data', '{}') # str or jsonstr or dict
172 172
173 if type_ == 'login': 173 if type_ == 'login':
174 kwargs = parameter.join(self.login, payload) 174 kwargs = parameter.join(self.login, payload)
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*- 2 # -*- coding: utf-8 -*-
3 -# @Time : 2020-02-15 17:30  
4 -# @Author : Lemon  
5 -# @File : control_server.py  
6 -# @Software: PyCharm  
7 3
8 import requests 4 import requests
9 5
@@ -147,13 +147,13 @@ class RequestSource(): @@ -147,13 +147,13 @@ class RequestSource():
147 class FacebookClient(Client): 147 class FacebookClient(Client):
148 def __init__(self, 148 def __init__(self,
149 user_obj, 149 user_obj,
150 - user_agent=None,  
151 max_tries=1): 150 max_tries=1):
152 151
153 self.email = user_obj.email 152 self.email = user_obj.email
154 self.user_obj = user_obj 153 self.user_obj = user_obj
155 self.extend = None 154 self.extend = None
156 - super().__init__(user_obj.email, user_obj.password, user_agent, max_tries, user_obj.format_cookie()) 155 +
  156 + super().__init__(user_obj.email, user_obj.password, user_obj.user_agent, max_tries, user_obj.format_cookie())
157 157
158 def __repr__(self): 158 def __repr__(self):
159 return "client:%s" % self.email 159 return "client:%s" % self.email
@@ -710,3 +710,6 @@ class FacebookClient(Client): @@ -710,3 +710,6 @@ class FacebookClient(Client):
710 if 'Arbiter.inform' in onloads: 710 if 'Arbiter.inform' in onloads:
711 return {"success": True} 711 return {"success": True}
712 raise BaseException("未知错误:" + onloads) 712 raise BaseException("未知错误:" + onloads)
  713 +
  714 + def get_user_agent(self):
  715 + return self._state._session.headers.get('User-Agent', random.choice(_util.USER_AGENTS))
@@ -69,6 +69,7 @@ class UserList(Base): @@ -69,6 +69,7 @@ class UserList(Base):
69 email = Column(String(50), index=True, nullable=False, unique=True) 69 email = Column(String(50), index=True, nullable=False, unique=True)
70 password = Column(String(50), nullable=False) 70 password = Column(String(50), nullable=False)
71 cookie = Column(String(512)) 71 cookie = Column(String(512))
  72 + user_agent = Column(String(256))
72 fbid = Column(String(20), index=True) 73 fbid = Column(String(20), index=True)
73 status = Column(Integer, default=0, nullable=False, index=True) 74 status = Column(Integer, default=0, nullable=False, index=True)
74 75
@@ -149,9 +150,11 @@ class UserList(Base): @@ -149,9 +150,11 @@ class UserList(Base):
149 return users 150 return users
150 151
151 152
  153 +# Base.metadata.drop_all(engine)
152 Base.metadata.create_all(engine) 154 Base.metadata.create_all(engine)
153 155
154 if __name__ == '__main__': 156 if __name__ == '__main__':
  157 + print(UserList.all())
155 # def tes(email): 158 # def tes(email):
156 # u = UserList.get(email=email) 159 # u = UserList.get(email=email)
157 # u.set(status=3) 160 # u.set(status=3)