Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Git
.git
.gitignore

# Build artifacts
yt-stats
bin/
dist/

# IDE
.idea/
.vscode/
*.swp
*.swo
*~

# OS
.DS_Store
Thumbs.db

# Environment
.env
.env.local

# Docker
Dockerfile
docker-compose.yml
.dockerignore

# Documentation
*.md
LICENSE

# Test files
*.test
test.sh
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# YouTube Data API Key
# Get your API key from: https://console.cloud.google.com/apis/credentials
YTSTATS_API_KEY=your_youtube_api_key_here
38 changes: 38 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool
*.out

# Go workspace file
go.work

# Dependency directories
vendor/

# Build artifacts
yt-stats
/bin/
/dist/

# IDE and editor files
.idea/
.vscode/
*.swp
*.swo
*~

# OS files
.DS_Store
Thumbs.db

# Environment files
.env
.env.local
32 changes: 32 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Build stage
FROM golang:1.24-alpine AS builder

WORKDIR /app

# Copy go mod files
COPY go.mod go.sum ./

# Download dependencies
RUN go mod download

# Copy source code
COPY . .

# Build the application
RUN CGO_ENABLED=0 GOOS=linux go build -o yt-stats ./cmd/yt-stats

# Runtime stage
FROM alpine:latest

RUN apk --no-cache add ca-certificates

WORKDIR /root/

# Copy the binary from builder
COPY --from=builder /app/yt-stats .

# Expose the application port
EXPOSE 8998

# Run the application
CMD ["./yt-stats"]
133 changes: 125 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,138 @@
# Youtube stat tool
# YouTube Stats Terminal App

Not just another stat tool. The idea is to do some fun with energy consumption.
A Go-based HTTP API service that fetches YouTube video statistics using the YouTube Data API v3. The project includes observability features with OpenTelemetry and Prometheus metrics.

## Features

## Generating client and stub code from OpenApi
- Fetch YouTube video statistics (views, likes, comments, etc.)
- OpenTelemetry tracing with stdout exporter
- Prometheus metrics endpoint
- Structured logging with zerolog
- Docker support for easy deployment

## Project Vision

This project has two main goals:

1. **API Service**: Fetch YouTube statistics for specific videos via HTTP API
2. **Browser Extension** (planned): Display video stats as an overlay/watermark while watching YouTube videos

## Prerequisites

- Go 1.24 or later
- YouTube Data API v3 key ([Get one here](https://console.cloud.google.com/apis/credentials))
- Docker and Docker Compose (optional, for containerized deployment)

## Quick Start

### Using Docker Compose (Recommended)

1. Clone the repository:
```bash
git clone https://github.com/ennc0d3/yt-stats.git
cd yt-stats
```
https://generator.swagger.io/#/

2. Create a `.env` file from the example:
```bash
cp .env.example .env
```

3. Edit `.env` and add your YouTube API key:
```bash
YTSTATS_API_KEY=your_actual_api_key_here
```

### Generate go-server stub
4. Start the service:
```bash
docker-compose up -d
```
curl -X POST -H "content-type:application/json" -d '{"swaggerUrl":"https://petstore.swagger.io/v2/swagger.json"}' https://generator.swagger.io/api/gen/servers/go-server

5. Test the API:
```bash
# Replace VIDEO_ID with an actual YouTube video ID
curl "http://localhost:8998/stats?video_id=dQw4w9WgXcQ"
```

## Start a local DB instance
### Using Go Directly

1. Set your API key:
```bash
export YTSTATS_API_KEY=your_youtube_api_key
```

2. Build and run:
```bash
go build -o yt-stats ./cmd/yt-stats
./yt-stats
```

## API Endpoints

### Get Video Statistics
```
GET /stats?video_id={VIDEO_ID}
```

Returns JSON with video statistics including views, likes, comments, etc.

**Example:**
```bash
curl "http://localhost:8998/stats?video_id=dQw4w9WgXcQ"
```
docker run --name petsitter-db -d -p 27017:27017 mongo:latest

### Prometheus Metrics
```
GET /metrics
```

Returns Prometheus-formatted metrics for monitoring.

## Development

### Building
```bash
go build -v ./cmd/yt-stats
```

### Running Tests
```bash
./test.sh
```

### Building with Docker
```bash
docker build -t yt-stats .
```

## Configuration

The application is configured via environment variables:

- `YTSTATS_API_KEY` (required): Your YouTube Data API v3 key

## Architecture

- **Port**: 8998
- **Logging**: Structured JSON logging via zerolog
- **Tracing**: OpenTelemetry with stdout exporter
- **Metrics**: Prometheus metrics exposed on `/metrics`
- **Router**: Gorilla Mux with OpenTelemetry middleware

## Project Structure

```
.
├── cmd/yt-stats/ # Application entry point
├── internal/api/ # API handlers and server logic
│ ├── server.go # HTTP server setup
│ ├── handler.go # Request handlers
│ └── ytutil.go # YouTube API integration
├── Dockerfile # Docker build configuration
├── docker-compose.yml # Docker Compose setup
└── README.md # This file
```

## License

See LICENSE file for details.
19 changes: 19 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: '3.8'

services:
yt-stats:
build:
context: .
dockerfile: Dockerfile
container_name: yt-stats-app
ports:
- "8998:8998"
environment:
- YTSTATS_API_KEY=${YTSTATS_API_KEY}
restart: unless-stopped
healthcheck:
test: ["CMD", "wget", "--quiet", "--tries=1", "--spider", "http://localhost:8998/metrics"]
interval: 30s
timeout: 10s
retries: 3
start_period: 40s
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/ennc0d3/yt-stats

go 1.21
go 1.24

require (
github.com/gorilla/mux v1.8.1
Expand Down Expand Up @@ -32,7 +32,6 @@ require (
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.46.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
Expand Down
Loading