From 9a1ba2d1716df92ebc58cbe7048d0a43093b5278 Mon Sep 17 00:00:00 2001 From: miutaku Date: Sun, 26 Jan 2025 05:52:23 +0900 Subject: [PATCH] =?UTF-8?q?TwitCasting=20Recording=20API=E3=81=AEREADME?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0=E3=81=97=E3=80=81=E3=82=BB=E3=83=83?= =?UTF-8?q?=E3=83=88=E3=82=A2=E3=83=83=E3=83=97=E6=89=8B=E9=A0=86=E3=80=81?= =?UTF-8?q?=E7=92=B0=E5=A2=83=E5=A4=89=E6=95=B0=E3=80=81=E3=82=A8=E3=83=B3?= =?UTF-8?q?=E3=83=89=E3=83=9D=E3=82=A4=E3=83=B3=E3=83=88=E3=80=81=E9=8C=B2?= =?UTF-8?q?=E7=94=BB=E5=87=BA=E5=8A=9B=E3=80=81=E5=AE=9F=E8=A1=8C=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E3=82=92=E8=A9=B3=E7=B4=B0=E3=81=AB=E8=AA=AC=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/README.md | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 api/README.md diff --git a/api/README.md b/api/README.md new file mode 100644 index 00000000..2f3eaded --- /dev/null +++ b/api/README.md @@ -0,0 +1,136 @@ +# TwitCasting Recording API + +This API provides a way to check if a TwitCasting user is live streaming and record their stream using `ffmpeg`. The stream is saved in a specified output directory. + +## Table of Contents + +- [Setup](#setup) +- [Environment Variables](#environment-variables) +- [Endpoints](#endpoints) +- [Recording Output](#recording-output) +- [How to Run](#how-to-run) +- [License](#license) + +## Setup + +1. Clone this repository and go to this directory. + +2. Install dependencies: + +Ensure `ffmpeg` is installed on your system. If not, install it: + +```bash +# For macOS +brew install ffmpeg +# For Debian based +sudo apt update +sudo apt install ffmpeg +# For RHEL based +sudo dnf install ffmpeg +``` + +3. Set up environment variables in `.env` or export them: + +```bash +export TWITCASTING_CLIENT_ID= +export TWITCASTING_CLIENT_SECRET= +export DB_HOST=localhost +export DB_PORT=5432 +export DB_USER=user +export DB_PASSWORD=password +export DB_NAME=dbname +export DB_TABLE_NAME=tablename +export OUTPUT_DIR=./recorded +export LOG_LEVEL=debug +``` + +4. Run the server: + +```bash +go run main.go +``` + +## Environment Variables + +| Variable | Description | Default Value | +|-------------------------|-----------------------------------------------------------------------------|---------------------| +| `TWITCASTING_CLIENT_ID`| Your TwitCasting Client ID. | None (required) | +| `TWITCASTING_CLIENT_SECRET`| Your TwitCasting Client Secret. | None (required) | +| `DB_HOST` | Database host. | `localhost` | +| `DB_PORT` | Database port. | `5432` | +| `DB_USER` | Database user. | `user` | +| `DB_PASSWORD` | Database password. | `password` | +| `DB_NAME` | Database name. | `dbname` | +| `DB_TABLE_NAME` | Database table name. | `tablename` | +| `OUTPUT_DIR` | Directory to save recorded videos. | `./recorded` | +| `LOG_LEVEL` | Set to `debug` to see detailed logs. | None | + +## Endpoints + +### `GET /check-live` + +Checks if a TwitCasting user is live streaming and records their stream if live. + +#### Request + +- **URL Query Parameters**: + - `username` (string): The TwitCasting username to check and record. + +#### Example + +```bash +curl "http://localhost:8080/check-live?username=" +``` + +#### Responses + +- **200 OK**: + - User is not live streaming: + ``` + User is not live streaming. + ``` + - Recording finished: + ``` + Recording finished. Saved as: + ``` +- **400 Bad Request**: + - Missing `username` parameter: + ``` + username parameter is required + ``` +- **500 Internal Server Error**: + - Errors while checking stream or updating recording state: + ``` + Failed to get current live information: + ``` + +## Recording Output + +Recorded streams are saved in the directory specified by the `OUTPUT_DIR` environment variable. The directory structure is as follows: + +``` +recorded/ + └── / + └── / + └── _.mp4 +``` + +- `<username>`: The TwitCasting username. +- `<YYYY-MM-DD>`: The date of the recording. +- `<HH-MM>`: The time of the recording. +- `<title>`: The sanitized title of the live stream. + +## How to Run + +1. Ensure all environment variables are correctly set. +2. Start the server: + +```bash +go run main.go +``` + +3. Access the endpoint to check and record a live stream: + +```bash +curl "http://localhost:8080/check-live?username=<twitcasting_username>" +```