Kaydet (Commit) 075db0e4 authored tarafından Batuhan Taşkaya's avatar Batuhan Taşkaya

Helix API proxy

üst 871d8966
[settings]
known_third_party = setuptools
known_third_party = aiohttp,setuptools
......@@ -11,6 +11,7 @@ isort = "*"
[packages]
pyside2 = "*"
asyncqt = "*"
aiohttp = "*"
[requires]
python_version = "3.7"
{
"_meta": {
"hash": {
"sha256": "963f8415c79676c2f47d8ef132183a2a00816242ded146a8d57370b0cc377ace"
"sha256": "06eaa8aa7f9a9bed3cae613ef72f79ee864087b0f985905d4bb1ab43a963de4c"
},
"pipfile-spec": 6,
"requires": {
......@@ -16,6 +16,41 @@
]
},
"default": {
"aiohttp": {
"hashes": [
"sha256:00d198585474299c9c3b4f1d5de1a576cc230d562abc5e4a0e81d71a20a6ca55",
"sha256:0155af66de8c21b8dba4992aaeeabf55503caefae00067a3b1139f86d0ec50ed",
"sha256:09654a9eca62d1bd6d64aa44db2498f60a5c1e0ac4750953fdd79d5c88955e10",
"sha256:199f1d106e2b44b6dacdf6f9245493c7d716b01d0b7fbe1959318ba4dc64d1f5",
"sha256:296f30dedc9f4b9e7a301e5cc963012264112d78a1d3094cd83ef148fdf33ca1",
"sha256:368ed312550bd663ce84dc4b032a962fcb3c7cae099dbbd48663afc305e3b939",
"sha256:40d7ea570b88db017c51392349cf99b7aefaaddd19d2c78368aeb0bddde9d390",
"sha256:629102a193162e37102c50713e2e31dc9a2fe7ac5e481da83e5bb3c0cee700aa",
"sha256:6d5ec9b8948c3d957e75ea14d41e9330e1ac3fed24ec53766c780f82805140dc",
"sha256:87331d1d6810214085a50749160196391a712a13336cd02ce1c3ea3d05bcf8d5",
"sha256:9a02a04bbe581c8605ac423ba3a74999ec9d8bce7ae37977a3d38680f5780b6d",
"sha256:9c4c83f4fa1938377da32bc2d59379025ceeee8e24b89f72fcbccd8ca22dc9bf",
"sha256:9cddaff94c0135ee627213ac6ca6d05724bfe6e7a356e5e09ec57bd3249510f6",
"sha256:a25237abf327530d9561ef751eef9511ab56fd9431023ca6f4803f1994104d72",
"sha256:a5cbd7157b0e383738b8e29d6e556fde8726823dae0e348952a61742b21aeb12",
"sha256:a97a516e02b726e089cffcde2eea0d3258450389bbac48cbe89e0f0b6e7b0366",
"sha256:acc89b29b5f4e2332d65cd1b7d10c609a75b88ef8925d487a611ca788432dfa4",
"sha256:b05bd85cc99b06740aad3629c2585bda7b83bd86e080b44ba47faf905fdf1300",
"sha256:c2bec436a2b5dafe5eaeb297c03711074d46b6eb236d002c13c42f25c4a8ce9d",
"sha256:cc619d974c8c11fe84527e4b5e1c07238799a8c29ea1c1285149170524ba9303",
"sha256:d4392defd4648badaa42b3e101080ae3313e8f4787cb517efd3f5b8157eaefd6",
"sha256:e1c3c582ee11af7f63a34a46f0448fca58e59889396ffdae1f482085061a2889"
],
"index": "pypi",
"version": "==3.5.4"
},
"async-timeout": {
"hashes": [
"sha256:0c3c816a028d47f659d6ff5c745cb2acf1f966da1fe5c19c77a70282b25f4c5f",
"sha256:4291ca197d287d274d0b6cb5d6f8f8f82d434ed288f962539ff18cc9012f9ea3"
],
"version": "==3.0.1"
},
"asyncqt": {
"hashes": [
"sha256:8b1507c968c85cf0b7eee5d2a887162d38af15fb8c5b1ec25beed6025d7383ac",
......@@ -24,6 +59,61 @@
"index": "pypi",
"version": "==0.7.0"
},
"attrs": {
"hashes": [
"sha256:69c0dbf2ed392de1cb5ec704444b08a5ef81680a61cb899dc08127123af36a79",
"sha256:f0b870f674851ecbfbbbd364d6b5cbdff9dcedbc7f3f5e18a6891057f21fe399"
],
"version": "==19.1.0"
},
"chardet": {
"hashes": [
"sha256:84ab92ed1c4d4f16916e05906b6b75a6c0fb5db821cc65e70cbd64a3e2a5eaae",
"sha256:fc323ffcaeaed0e0a02bf4d117757b98aed530d9ed4531e3e15460124c106691"
],
"version": "==3.0.4"
},
"idna": {
"hashes": [
"sha256:c357b3f628cf53ae2c4c05627ecc484553142ca23264e593d327bcde5e9c3407",
"sha256:ea8b7f6188e6fa117537c3df7da9fc686d485087abf6ac197f9c46432f7e4a3c"
],
"version": "==2.8"
},
"multidict": {
"hashes": [
"sha256:024b8129695a952ebd93373e45b5d341dbb87c17ce49637b34000093f243dd4f",
"sha256:041e9442b11409be5e4fc8b6a97e4bcead758ab1e11768d1e69160bdde18acc3",
"sha256:045b4dd0e5f6121e6f314d81759abd2c257db4634260abcfe0d3f7083c4908ef",
"sha256:047c0a04e382ef8bd74b0de01407e8d8632d7d1b4db6f2561106af812a68741b",
"sha256:068167c2d7bbeebd359665ac4fff756be5ffac9cda02375b5c5a7c4777038e73",
"sha256:148ff60e0fffa2f5fad2eb25aae7bef23d8f3b8bdaf947a65cdbe84a978092bc",
"sha256:1d1c77013a259971a72ddaa83b9f42c80a93ff12df6a4723be99d858fa30bee3",
"sha256:1d48bc124a6b7a55006d97917f695effa9725d05abe8ee78fd60d6588b8344cd",
"sha256:31dfa2fc323097f8ad7acd41aa38d7c614dd1960ac6681745b6da124093dc351",
"sha256:34f82db7f80c49f38b032c5abb605c458bac997a6c3142e0d6c130be6fb2b941",
"sha256:3d5dd8e5998fb4ace04789d1d008e2bb532de501218519d70bb672c4c5a2fc5d",
"sha256:4a6ae52bd3ee41ee0f3acf4c60ceb3f44e0e3bc52ab7da1c2b2aa6703363a3d1",
"sha256:4b02a3b2a2f01d0490dd39321c74273fed0568568ea0e7ea23e02bd1fb10a10b",
"sha256:4b843f8e1dd6a3195679d9838eb4670222e8b8d01bc36c9894d6c3538316fa0a",
"sha256:5de53a28f40ef3c4fd57aeab6b590c2c663de87a5af76136ced519923d3efbb3",
"sha256:61b2b33ede821b94fa99ce0b09c9ece049c7067a33b279f343adfe35108a4ea7",
"sha256:6a3a9b0f45fd75dc05d8e93dc21b18fc1670135ec9544d1ad4acbcf6b86781d0",
"sha256:76ad8e4c69dadbb31bad17c16baee61c0d1a4a73bed2590b741b2e1a46d3edd0",
"sha256:7ba19b777dc00194d1b473180d4ca89a054dd18de27d0ee2e42a103ec9b7d014",
"sha256:7c1b7eab7a49aa96f3db1f716f0113a8a2e93c7375dd3d5d21c4941f1405c9c5",
"sha256:7fc0eee3046041387cbace9314926aa48b681202f8897f8bff3809967a049036",
"sha256:8ccd1c5fff1aa1427100ce188557fc31f1e0a383ad8ec42c559aabd4ff08802d",
"sha256:8e08dd76de80539d613654915a2f5196dbccc67448df291e69a88712ea21e24a",
"sha256:c18498c50c59263841862ea0501da9f2b3659c00db54abfbf823a80787fde8ce",
"sha256:c49db89d602c24928e68c0d510f4fcf8989d77defd01c973d6cbe27e684833b1",
"sha256:ce20044d0317649ddbb4e54dab3c1bcc7483c78c27d3f58ab3d0c7e6bc60d26a",
"sha256:d1071414dd06ca2eafa90c85a079169bfeb0e5f57fd0b45d44c092546fcd6fd9",
"sha256:d3be11ac43ab1a3e979dac80843b42226d5d3cccd3986f2e03152720a4297cd7",
"sha256:db603a1c235d110c860d5f39988ebc8218ee028f07a7cbc056ba6424372ca31b"
],
"version": "==4.5.2"
},
"pyside2": {
"hashes": [
"sha256:485bea45a860df5eb13714c84f13b0b40bc68c4f799f6f941a144dd8e25aa7ad",
......@@ -46,6 +136,22 @@
"sha256:d7d30ae3ba20556d5ddab5879a629a7d851d3f19255b76ccd21749429e6a3271"
],
"version": "==5.12.3"
},
"yarl": {
"hashes": [
"sha256:024ecdc12bc02b321bc66b41327f930d1c2c543fa9a561b39861da9388ba7aa9",
"sha256:2f3010703295fbe1aec51023740871e64bb9664c789cba5a6bdf404e93f7568f",
"sha256:3890ab952d508523ef4881457c4099056546593fa05e93da84c7250516e632eb",
"sha256:3e2724eb9af5dc41648e5bb304fcf4891adc33258c6e14e2a7414ea32541e320",
"sha256:5badb97dd0abf26623a9982cd448ff12cb39b8e4c94032ccdedf22ce01a64842",
"sha256:73f447d11b530d860ca1e6b582f947688286ad16ca42256413083d13f260b7a0",
"sha256:7ab825726f2940c16d92aaec7d204cfc34ac26c0040da727cf8ba87255a33829",
"sha256:b25de84a8c20540531526dfbb0e2d2b648c13fd5dd126728c496d7c3fea33310",
"sha256:c6e341f5a6562af74ba55205dbd56d248daf1b5748ec48a0200ba227bb9e33f4",
"sha256:c9bb7c249c4432cd47e75af3864bc02d26c9594f49c82e2a28624417f0ae63b8",
"sha256:e060906c0c585565c718d1c3841747b61c5439af2211e185f6739a9412dfbde1"
],
"version": "==1.3.0"
}
},
"develop": {
......
......@@ -10,7 +10,9 @@ async def main():
auth = Auth.from_json("../twitch_auth.json")
transport, protocol = await loop.create_connection(
lambda: TwitchChatProtocol(auth, [Channel("btaskaya")], on_con_lost),
lambda: TwitchChatProtocol(
auth, loop, channels=[Channel("btaskaya")], on_con_lost=on_con_lost
),
"irc.twitch.tv",
6667,
)
......
......@@ -4,16 +4,19 @@ import sys
from contextlib import suppress
from typing import Optional, Sequence
import aiohttp
from chatat.twitch import Auth, Channel, Message
class TwitchProtocol(asyncio.Protocol):
def __init__(
self, auth: Auth, channels: Sequence[Channel], on_con_lost: asyncio.Future
) -> None:
self.on_con_lost = on_con_lost
class _Protocol:
def __init__(self, auth: Auth, loop: asyncio.BaseEventLoop, **kwargs) -> None:
self.on_con_lost = kwargs.pop("on_con_lost", None)
self.channels = kwargs.pop("channels", None)
self.loop = loop
self._auth = auth
self._channels = channels
self.logger = logging.getLogger(__name__)
self.logger.setLevel(logging.DEBUG)
......@@ -28,25 +31,45 @@ class TwitchProtocol(asyncio.Protocol):
self.logger.addHandler(handler)
class TwitchHelixProtocol(TwitchProtocol):
pass
class TwitchHelixProtocol(_Protocol):
BASE = "https://api.twitch.tv/helix/{}"
@classmethod
async def session(cls, *args, **kwargs):
inst = cls(*args, **kwargs)
jar = aiohttp.CookieJar(unsafe=True)
inst.session = aiohttp.ClientSession(
cookie_jar=jar, headers={"Client-ID": inst._auth.client_id}
)
return inst
async def get(self, endpoint: str, **kwargs):
self.logger.debug(f"Getting {endpoint} with {kwargs}")
async with self.session.get(
self.BASE.format(endpoint), params=kwargs
) as result:
content = await result.json()
return content
class TwitchChatProtocol(TwitchProtocol):
def _send(self, cmd: str, value: str) -> None:
class TwitchChatProtocol(_Protocol, asyncio.Protocol):
def _send_irc_command(self, cmd: str, value: str) -> None:
request = f"{cmd.upper()} {value}\r\n"
self.transport.write(request.encode("utf-8"))
def connection_made(self, transport: asyncio.transports.Transport): # type: ignore
def connection_made(
self, transport: asyncio.transports.Transport
) -> None: # type: ignore
self.transport = transport
self._send("pass", self._auth.oauthtok)
self._send("nick", self._auth.username)
self._send_irc_command("pass", self._auth.oauthtok)
self._send_irc_command("nick", self._auth.username)
with suppress(ValueError):
ip, port = transport.get_extra_info("peername")
self.logger.info(f"Connection made to {ip}:{port}")
for channel in self._channels:
self._send("join", channel)
for channel in self.channels:
self._send_irc_command("join", channel)
def data_received(self, raw_msg: bytes) -> None:
data = raw_msg.decode()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment