From: Georgios Atheridis Date: Tue, 27 Dec 2022 12:18:49 +0000 (+0000) Subject: Initial Commit X-Git-Url: https://git.atheridis.org/?a=commitdiff_plain;p=personal%2Ftwitch-clip-viewer.git Initial Commit --- 3bcbb4d4f99c58159e2d54a4fef3df8c170a1de3 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b6e4761 --- /dev/null +++ b/.gitignore @@ -0,0 +1,129 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..71d4c1d --- /dev/null +++ b/LICENSE @@ -0,0 +1,14 @@ +BSD Zero Clause License + +Copyright (c) 2022 Georgios Atheridis + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH +REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, +INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR +OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +PERFORMANCE OF THIS SOFTWARE. diff --git a/README b/README new file mode 100644 index 0000000..c4364a6 --- /dev/null +++ b/README @@ -0,0 +1,30 @@ +Random Twitch Clip Viewer +------------------------- + +Requirements: + +uvicorn[standard] +fastapi +python-dotenv + +or +$ pip install -r "requirements.txt" +------------------------- + +Install the packages +$ python -m pip install python-dotenv fastapi uvicorn[standard] + +create a .env file in the project's root and add the following +BROADCASTER_ID=00000000 +CLIENT_ID="yourclientidgoeshere" +OAUTH="youroauthtokengoeshere" + +then if using locally, run +$ uvicorn server:app --host 127.0.0.1 --port 8888 + +open client.html in your browser, or open it using OBS. + + +If running it on a server, change the IP value in client.html +to your server's IP and run +$ uvicorn server:app --host 0.0.0.0 --port 8888 diff --git a/client.html b/client.html new file mode 100644 index 0000000..c40ca0f --- /dev/null +++ b/client.html @@ -0,0 +1,42 @@ + + + + clips + + + + + + + diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..ea1fe57 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,20 @@ +anyio==3.6.2 +certifi==2022.12.7 +charset-normalizer==2.1.1 +click==8.1.3 +fastapi==0.88.0 +h11==0.14.0 +httptools==0.5.0 +idna==3.4 +pydantic==1.10.2 +python-dotenv==0.21.0 +PyYAML==6.0 +requests==2.28.1 +sniffio==1.3.0 +starlette==0.22.0 +typing_extensions==4.4.0 +urllib3==1.26.13 +uvicorn==0.20.0 +uvloop==0.17.0 +watchfiles==0.18.1 +websockets==10.4 diff --git a/server.py b/server.py new file mode 100644 index 0000000..6495aac --- /dev/null +++ b/server.py @@ -0,0 +1,53 @@ +import os +import random +import requests +import dotenv +from fastapi import FastAPI +from fastapi.middleware.cors import CORSMiddleware + +dotenv.load_dotenv() + +broadcaster_id = os.getenv("BROADCASTER_ID") +client_id = os.getenv("CLIENT_ID") +oauth = os.getenv("OAUTH") + +if not (broadcaster_id and client_id and oauth): + exit(1) + +response = requests.get( + "https://api.twitch.tv/helix/clips", + params={ + "broadcaster_id": broadcaster_id, + "first": 100, + }, + headers={ + "Authorization": f"Bearer {oauth}", + "Client-Id": client_id, + } +) + +if response.status_code != 200: + exit(1) + +data = response.json()["data"] +for d in data: + d["video_mp4"] = \ + d["thumbnail_url"][:d["thumbnail_url"].find("-preview")] + ".mp4" + +app = FastAPI() + +origins = ["*"] + +app.add_middleware( + CORSMiddleware, + allow_origins=origins, + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) + + +@app.get("/clip") +async def root(): + clip = random.choice(data)["video_mp4"] + return clip