正在显示
4 个修改的文件
包含
69 行增加
和
21 行删除
| @@ -7,6 +7,7 @@ | @@ -7,6 +7,7 @@ | ||
| 7 | import functools | 7 | import functools |
| 8 | import json | 8 | import json |
| 9 | 9 | ||
| 10 | +from apscheduler.schedulers.tornado import TornadoScheduler | ||
| 10 | from tornado import ioloop | 11 | from tornado import ioloop |
| 11 | 12 | ||
| 12 | from conf import settings | 13 | from conf import settings |
| @@ -53,6 +54,7 @@ def host(): | @@ -53,6 +54,7 @@ def host(): | ||
| 53 | 54 | ||
| 54 | 55 | ||
| 55 | def connect_success(sock): | 56 | def connect_success(sock): |
| 57 | + print(monitor._name, '连接成功->', sock.ws_url) | ||
| 56 | if monitor._temp_socket == sock: | 58 | if monitor._temp_socket == sock: |
| 57 | sock.auto_reconnet = True # 新的连接成功后把断线重连开启 | 59 | sock.auto_reconnet = True # 新的连接成功后把断线重连开启 |
| 58 | monitor._socket.quit() # 旧的断开 | 60 | monitor._socket.quit() # 旧的断开 |
| @@ -74,11 +76,20 @@ def set_new_server_func(sock): | @@ -74,11 +76,20 @@ def set_new_server_func(sock): | ||
| 74 | 76 | ||
| 75 | 77 | ||
| 76 | def run(): | 78 | def run(): |
| 79 | + # init socket start | ||
| 77 | socket = MessageSocketClient(connect_success, message_receive) | 80 | socket = MessageSocketClient(connect_success, message_receive) |
| 78 | set_new_server_func(socket) | 81 | set_new_server_func(socket) |
| 79 | - monitor.bind(socket) | ||
| 80 | - | ||
| 81 | conf = settings.get_server() | 82 | conf = settings.get_server() |
| 82 | conf.update({'url': monitor._host, 'name': monitor._name, 'uuid': monitor._imei}) | 83 | conf.update({'url': monitor._host, 'name': monitor._name, 'uuid': monitor._imei}) |
| 83 | socket.connect(**conf) | 84 | socket.connect(**conf) |
| 85 | + monitor.bind(socket) | ||
| 86 | + # init socket end | ||
| 87 | + | ||
| 88 | + # init schedule start | ||
| 89 | + sched = TornadoScheduler() | ||
| 90 | + sched.add_job(monitor._socket.sendheartbeat, 'interval', seconds=25) | ||
| 91 | + sched.add_job(monitor._socket.auto_move_server, 'interval', seconds=31) | ||
| 92 | + sched.start() | ||
| 93 | + # init schedule end | ||
| 94 | + | ||
| 84 | ioloop.IOLoop.instance().start() | 95 | ioloop.IOLoop.instance().start() |
| @@ -254,7 +254,7 @@ class FacebookClient(Client): | @@ -254,7 +254,7 @@ class FacebookClient(Client): | ||
| 254 | "/settings/name_change_preview/", data) | 254 | "/settings/name_change_preview/", data) |
| 255 | return res | 255 | return res |
| 256 | 256 | ||
| 257 | - def saveName(self, display_format, save_password, primary_first, primary_middle, primary_last): | 257 | + def saveName(self, save_password, primary_first, primary_last, primary_middle=None, display_format='complete'): |
| 258 | data = { | 258 | data = { |
| 259 | 'display_format': display_format, | 259 | 'display_format': display_format, |
| 260 | 'save_password': save_password, | 260 | 'save_password': save_password, |
| @@ -265,7 +265,15 @@ class FacebookClient(Client): | @@ -265,7 +265,15 @@ class FacebookClient(Client): | ||
| 265 | # 'alternate_name': 'lin wei quan', 'show_alternate': '1' | 265 | # 'alternate_name': 'lin wei quan', 'show_alternate': '1' |
| 266 | res = self._post( | 266 | res = self._post( |
| 267 | "/ajax/settings/account/name.php", data) | 267 | "/ajax/settings/account/name.php", data) |
| 268 | - return res | 268 | + |
| 269 | + status = res['jsmods']['require'][0][1] | ||
| 270 | + if status == 'addError': | ||
| 271 | + html = res['jsmods']['require'][0][3][0]['__html'] | ||
| 272 | + return {'success': False, 'error': parse_html.get_div_text(html)} | ||
| 273 | + elif status == 'setPreviewForCurrent': | ||
| 274 | + html = res['jsmods']['require'][0][3][0]['__html'] | ||
| 275 | + return {'success': True, 'msg': parse_html.get_div_text(html)} | ||
| 276 | + raise BaseException('未知错误:' + json.dumps(res['jsmods']['require'])) | ||
| 269 | 277 | ||
| 270 | def uploudAvatar(self, binary): | 278 | def uploudAvatar(self, binary): |
| 271 | files = { | 279 | files = { |
| @@ -823,3 +831,27 @@ class FacebookClient(Client): | @@ -823,3 +831,27 @@ class FacebookClient(Client): | ||
| 823 | if 'fbSettingsListItem' in res['domops'][0][1]: | 831 | if 'fbSettingsListItem' in res['domops'][0][1]: |
| 824 | return {"success": True} | 832 | return {"success": True} |
| 825 | return {"success": False} | 833 | return {"success": False} |
| 834 | + | ||
| 835 | + def setBirthday(self, year, month, day): | ||
| 836 | + data = { | ||
| 837 | + 'privacy[8787510733]': PostParam.EVERYONE.value, | ||
| 838 | + 'privacy[8787805733]': PostParam.EVERYONE.value, | ||
| 839 | + 'birthday_month': month, 'birthday_day': day, 'birthday_year': year, | ||
| 840 | + 'birthday_confirmation': '1', | ||
| 841 | + 'bd_surface': 'www_profile', 'edit_birthday_allowed': '0', | ||
| 842 | + '__submit__': '1', 'nctr[_mod]': 'pagelet_basic' | ||
| 843 | + } | ||
| 844 | + res = self._post('/profile/edit/infotab/save/birthday/', data) | ||
| 845 | + html = parse_html.get_domops_3(res) | ||
| 846 | + if 'birthday' in html: | ||
| 847 | + return {"success": True} | ||
| 848 | + else: | ||
| 849 | + raise BaseException("未知错误:", html) | ||
| 850 | + | ||
| 851 | + def setLanguage(self, language): | ||
| 852 | + data = { | ||
| 853 | + 'new_language': language, | ||
| 854 | + 'new_fallback_language': '', | ||
| 855 | + } | ||
| 856 | + res = self._post('/ajax/settings/language/account.php', data) | ||
| 857 | + return {"success": True} |
| @@ -8,8 +8,6 @@ import datetime | @@ -8,8 +8,6 @@ import datetime | ||
| 8 | import functools | 8 | import functools |
| 9 | import json | 9 | import json |
| 10 | import random | 10 | import random |
| 11 | -import time | ||
| 12 | -from threading import Timer | ||
| 13 | 11 | ||
| 14 | from tornado import gen | 12 | from tornado import gen |
| 15 | from tornado import httpclient | 13 | from tornado import httpclient |
| @@ -135,8 +133,6 @@ class MessageSocketClient(WebSocketClient): | @@ -135,8 +133,6 @@ class MessageSocketClient(WebSocketClient): | ||
| 135 | self.connect_timeout, | 133 | self.connect_timeout, |
| 136 | self.request_timeout) | 134 | self.request_timeout) |
| 137 | 135 | ||
| 138 | - self._later_handler['move_server'] = self._io_loop.call_later(random.randint(30, 60), self.auto_move_server) | ||
| 139 | - | ||
| 140 | def new_server_func(self): | 136 | def new_server_func(self): |
| 141 | return self.ws_url, False | 137 | return self.ws_url, False |
| 142 | 138 | ||
| @@ -146,8 +142,8 @@ class MessageSocketClient(WebSocketClient): | @@ -146,8 +142,8 @@ class MessageSocketClient(WebSocketClient): | ||
| 146 | def auto_move_server(self): | 142 | def auto_move_server(self): |
| 147 | ws_url, move = self.new_server_func() | 143 | ws_url, move = self.new_server_func() |
| 148 | if move: | 144 | if move: |
| 145 | + print('需要移动->', ws_url) | ||
| 149 | self.server_replace(ws_url) | 146 | self.server_replace(ws_url) |
| 150 | - self._later_handler['move_server'] = self._io_loop.call_later(random.randint(30, 30), self.auto_move_server) | ||
| 151 | 147 | ||
| 152 | def connect(self, url, name, uuid, reconnect=True, reconnect_interval=3): | 148 | def connect(self, url, name, uuid, reconnect=True, reconnect_interval=3): |
| 153 | self.ws_url = url | 149 | self.ws_url = url |
| @@ -161,8 +157,6 @@ class MessageSocketClient(WebSocketClient): | @@ -161,8 +157,6 @@ class MessageSocketClient(WebSocketClient): | ||
| 161 | def on_connection_success(self): | 157 | def on_connection_success(self): |
| 162 | if self._connect_success: | 158 | if self._connect_success: |
| 163 | self._connect_success(self) | 159 | self._connect_success(self) |
| 164 | - self._later_handler['heartbeat'] = self._io_loop.call_later(self.heartbeat_interval_in_secs, | ||
| 165 | - functools.partial(self.sendheartbeat, self.ws_url)) | ||
| 166 | 160 | ||
| 167 | def on_connection_close(self, reason): | 161 | def on_connection_close(self, reason): |
| 168 | print('%s Connection closed reason=%s' % (self.ws_url, reason,)) | 162 | print('%s Connection closed reason=%s' % (self.ws_url, reason,)) |
| @@ -179,16 +173,10 @@ class MessageSocketClient(WebSocketClient): | @@ -179,16 +173,10 @@ class MessageSocketClient(WebSocketClient): | ||
| 179 | def payload_data(self): | 173 | def payload_data(self): |
| 180 | return {"type": "ping"} | 174 | return {"type": "ping"} |
| 181 | 175 | ||
| 182 | - def sendheartbeat(self, _url): | 176 | + def sendheartbeat(self): |
| 183 | print(datetime.datetime.now(), "发送❤️") | 177 | print(datetime.datetime.now(), "发送❤️") |
| 184 | - if self.ws_url == _url: | ||
| 185 | - msg = self.payload_data() | ||
| 186 | - bool = self.send(msg) if msg else False | ||
| 187 | - | ||
| 188 | - if msg is None or bool: | ||
| 189 | - self._later_handler['heartbeat'] = self._io_loop.call_later(self.heartbeat_interval_in_secs, | ||
| 190 | - functools.partial(self.sendheartbeat, | ||
| 191 | - self.ws_url)) | 178 | + msg = self.payload_data() |
| 179 | + self.send(msg) | ||
| 192 | 180 | ||
| 193 | def on_message(self, msg): | 181 | def on_message(self, msg): |
| 194 | if not self._message_receive: | 182 | if not self._message_receive: |
| @@ -201,3 +189,19 @@ class MessageSocketClient(WebSocketClient): | @@ -201,3 +189,19 @@ class MessageSocketClient(WebSocketClient): | ||
| 201 | self.close("quit") | 189 | self.close("quit") |
| 202 | for k, handler in self._later_handler.items(): | 190 | for k, handler in self._later_handler.items(): |
| 203 | self._io_loop.remove_timeout(handler) | 191 | self._io_loop.remove_timeout(handler) |
| 192 | + | ||
| 193 | + | ||
| 194 | +if __name__ == '__main__': | ||
| 195 | + from apscheduler.schedulers.tornado import TornadoScheduler | ||
| 196 | + | ||
| 197 | + | ||
| 198 | + def run(): | ||
| 199 | + print(1) | ||
| 200 | + | ||
| 201 | + | ||
| 202 | + sock = MessageSocketClient() | ||
| 203 | + sched = TornadoScheduler() | ||
| 204 | + sched.add_job(run, 'interval', seconds=3) | ||
| 205 | + sched.start() | ||
| 206 | + | ||
| 207 | + sock._io_loop.instance().start() |
-
请 注册 或 登录 后发表评论