yuuki/libs/connection.py

95 lines
2.9 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-01-29 14:31:01 +08:00
from yuuki_core.TalkService import Client
2019-08-22 18:33:10 +08:00
""" NC HightSpeed Lib """
try:
from thrift.protocol import fastbinary
2020-02-12 15:01:03 +08:00
except ImportError:
print("[No fast_binary using]")
2019-08-22 18:33:10 +08:00
##########################################
2019-12-25 21:35:43 +08:00
2019-08-23 18:12:41 +08:00
class Yuuki_Connect:
def __init__(self, Yuuki_Configs):
2019-08-22 18:33:10 +08:00
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-08-22 18:52:24 +08:00
self.helper = []
self.helper_ids = []
2019-08-31 12:36:18 +08:00
self.helper_authTokens = {}
2019-08-22 18:52:24 +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
def helperConnect(self, LINE_ACCESS_KEY):
2019-08-26 23:43:03 +08:00
helper_ConnectHeader = self.con_header.copy()
2019-08-22 18:52:24 +08:00
helper_ConnectHeader["X-Line-Access"] = LINE_ACCESS_KEY
transport = THttpClient.THttpClient(self.host + self.com_path)
transport.setCustomHeaders(helper_ConnectHeader)
protocol = TCompactProtocol.TCompactProtocol(transport)
client = Client(protocol)
transport.open()
2020-02-12 15:01:03 +08:00
# noinspection PyBroadException
2019-08-22 18:52:24 +08:00
try:
profile = client.getProfile()
self.helper.append(client)
self.helper_ids.append(profile.mid)
2019-08-31 12:36:18 +08:00
self.helper_authTokens[profile.mid] = LINE_ACCESS_KEY
2019-08-22 18:52:24 +08:00
return True
except:
print("Error:\n%s\nNot Acceptable\n" % (LINE_ACCESS_KEY,))
2019-08-31 12:36:18 +08:00
def helperThreadConnect(self, userId):
if userId in self.helper_authTokens:
LINE_ACCESS_KEY = self.helper_authTokens.get(userId)
else:
return None
helper_ConnectHeader = self.con_header.copy()
helper_ConnectHeader["X-Line-Access"] = LINE_ACCESS_KEY
transport = THttpClient.THttpClient(self.host + self.com_path)
transport.setCustomHeaders(helper_ConnectHeader)
protocol = TCompactProtocol.TCompactProtocol(transport)
client = Client(protocol)
transport.open()
return client