Skip to main content

7TV EventAPI Wrapper

Discord Server Python Version PyPI Version PyPI Downloads

This is a Python wrapper for the 7TV EventAPI, which provides async access to the websocket events on their Emote Platform.

Requirements

  • Python 3.8 or higher

Installation

You can install the 7TV EventAPI Wrapper using pip. Open your terminal and run the following command:

pip install eventapi

Usage

Here's a simple example of how to use wrapper:

import asyncio

from eventapi.EventApi import EventApi
from eventapi.WebSocket import (
    EventType,
    ResponseTypes,
    SubscriptionCondition,
    SubscriptionData,
)


async def callback(data: ResponseTypes):
    print("Message from callback: ", data)


async def main():
    # create instance of EventApi with message callback
    app = EventApi(callback=callback)

    # connect to websocket
    await app.connect()
    print("Connected")

    # create subscription with specified condition
    condition = SubscriptionCondition(object_id="6433b7cec07d26f890dd2d01")
    subscription = SubscriptionData(
        subscription_type=EventType.EMOTE_SET_ALL, condition=condition
    )
    await app.subscribe(subscription_data=subscription)

    # you can also use async iterator without specifying callback
    async for message in app:
        print("Message from async iterator: ", message)

    # run forever if we are not using async for
    await asyncio.Future()


asyncio.run(main())

Discord.py / Pycord example:

Discord Bot example
from typing import Any, Dict, List

import aiohttp
import discord

from eventapi.EventApi import EventApi
from eventapi.WebSocket import (
    Dispatch,
    EventType,
    ResponseTypes,
    SubscriptionCondition,
    SubscriptionData,
)


async def format_url(data: Dict[str, Any]) -> str:
    base_url = "https://cdn.7tv.app/emote/{}/4x".format(data.get("id"))
    if "animated" not in data:
        async with aiohttp.ClientSession() as cs:
            async with cs.get(base_url + ".gif") as r:
                if r.status == 200:
                    base_url += ".gif"
    else:
        if data.get("animated"):
            base_url += ".gif"
    return base_url


async def callback(data: ResponseTypes) -> None:
    if not isinstance(data, Dispatch):
        return
    if data.type != EventType.EMOTE_SET_UPDATE:
        return

    channel = client.get_channel(927288026000945162)  # your channel id
    changes: List[discord.Embed] = []

    def add_change(description: str, color: discord.Color, image_url: str):
        changes.append(
            discord.Embed(description=description, color=color).set_image(url=image_url)
        )

    if data.body.pulled:
        for pulled in data.body.pulled:
            add_change(
                "**Deleted emote:** `{}`".format(pulled.old_value.get("name")),
                discord.Color.brand_red(),
                await format_url(pulled.old_value),
            )
    if data.body.pushed:
        for pushed in data.body.pushed:
            add_change(
                "**Added emote:** `{}`".format(pushed.value.get("name")),
                discord.Color.brand_green(),
                await format_url(pushed.value.get("data")),
            )
    if data.body.updated:
        for updated in data.body.updated:
            add_change(
                "**Edited emote:** `{}` » `{}`".format(
                    updated.old_value.get("name"), updated.value.get("name")
                ),
                discord.Color.yellow(),
                await format_url(updated.value),
            )

    if len(changes) > 0:
        await channel.send(embeds=changes)


intents = discord.Intents.default()
client = discord.Client(intents=intents)
client.eventapi = EventApi(callback=callback)


@client.listen("on_ready", once=True)
async def on_ready():
    await client.eventapi.connect()
    condition = SubscriptionCondition(
        object_id="6433b7cec07d26f890dd2d01"
    )  # your emote-set id
    subscription = SubscriptionData(
        subscription_type=EventType.EMOTE_SET_ALL, condition=condition
    )
    await client.eventapi.subscribe(subscription_data=subscription)


client.run("your token here")  # your token

Contributing

Contributions are welcome! If you encounter any issues or have suggestions for improvements, please open an issue or submit a pull request on the GitHub repository.

License

This project is licensed under the MIT License.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

eventapi-1.1.1.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

eventapi-1.1.1-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file eventapi-1.1.1.tar.gz.

File metadata

  • Download URL: eventapi-1.1.1.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.6

File hashes

Hashes for eventapi-1.1.1.tar.gz
Algorithm Hash digest
SHA256 430036b05208485e7deaf4169e58a47e3470b6a6883170c42df4b3e8459e3cb8
MD5 d14c1103942949f33d9018734ee3f33d
BLAKE2b-256 65289446355d2023435209f8fd42228fd28a56b29fc3d8042fe17f31644b6004

See more details on using hashes here.

File details

Details for the file eventapi-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: eventapi-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.6

File hashes

Hashes for eventapi-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 82a04a7ae30f18a6f5c5ed9a629dbd7cc89d7db86adaafc7d4e1445fbe263af9
MD5 db67374af907c685da16f4a417bf1587
BLAKE2b-256 5676a231a1ff282857751d121ccbc220b761779b5bc6f58ac7452223711b725c

See more details on using hashes here.

Release history Release notifications | RSS feed

1.1.2

2 files

This release

1.1.1 This release

2 files

1.1.0

2 files

1.0.4

2 files

1.0.3

2 files

1.0.2

2 files

1.0.1

2 files

1.0.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page