yuuki/libs/connection.py

72 lines
2.2 KiB
Python
Raw Normal View History

2019-12-31 21:45:14 +08:00
# -*- coding: utf-8 -*-
"""
Yuuki_Libs
(c) 2020 Star Inc.
2019-12-31 21:45:14 +08:00
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
"""
2019-08-22 18:33:10 +08:00
from thrift.protocol import TCompactProtocol
2019-12-25 21:35:43 +08:00
from thrift.transport import THttpClient
2020-03-07 01:11:32 +08:00
from yuuki_core.TalkService import Client, TalkException
2019-08-22 18:33:10 +08:00
2020-02-28 23:30:24 +08:00
# NC HighSpeed Library
2019-08-22 18:33:10 +08:00
try:
from thrift.protocol import fastbinary
2020-02-12 15:01:03 +08:00
except ImportError:
print("[No fast_binary using]")
2019-08-23 18:12:41 +08:00
class Yuuki_Connect:
def __init__(self, Yuuki_Configs):
2019-08-22 18:33:10 +08:00
2020-03-07 01:11:32 +08:00
self.helper = {}
self.host = Yuuki_Configs.connectInfo["Host"]
self.com_path = Yuuki_Configs.connectInfo["Command_Path"]
self.poll_path = Yuuki_Configs.connectInfo["LongPoll_path"]
2019-08-22 18:33:10 +08:00
self.con_header = Yuuki_Configs.connectHeader
2019-08-22 18:33:10 +08:00
2019-09-02 20:54:54 +08:00
def connect(self, listen_timeout=600000):
2019-08-22 18:33:10 +08:00
transport = THttpClient.THttpClient(self.host + self.com_path)
transport_in = THttpClient.THttpClient(self.host + self.poll_path)
2019-09-02 20:54:54 +08:00
transport_in.setTimeout(listen_timeout)
2019-08-22 18:33:10 +08:00
transport.setCustomHeaders(self.con_header)
transport_in.setCustomHeaders(self.con_header)
protocol = TCompactProtocol.TCompactProtocol(transport)
protocol_in = TCompactProtocol.TCompactProtocol(transport_in)
client = Client(protocol)
listen = Client(protocol_in)
transport.open()
transport_in.open()
return client, listen
2019-08-22 18:52:24 +08:00
2020-03-07 01:11:32 +08:00
def helperConnect(self, auth_token):
helper_connect_header = self.con_header.copy()
helper_connect_header["X-Line-Access"] = auth_token
2019-08-22 18:52:24 +08:00
transport = THttpClient.THttpClient(self.host + self.com_path)
2020-03-07 01:11:32 +08:00
transport.setCustomHeaders(helper_connect_header)
2019-08-22 18:52:24 +08:00
protocol = TCompactProtocol.TCompactProtocol(transport)
client = Client(protocol)
transport.open()
try:
profile = client.getProfile()
2020-10-18 21:07:48 +08:00
self.helper[profile.mid] = {
"client": client,
"profile": profile
}
2019-08-22 18:52:24 +08:00
return True
2019-08-31 12:36:18 +08:00
2020-03-07 01:11:32 +08:00
except TalkException:
print("Error:\n%s\nNot Acceptable\n" % (auth_token,))