aboutsummaryrefslogtreecommitdiff
path: root/cmd/patchouli/main.go
blob: 7a30e6316961312f353cb15d0d6fbb0c1028b90b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main

import (
	"context"
	"database/sql"
	"flag"
	"fmt"
	"log"
	"log/slog"
	"net/http"
	"path/filepath"

	"connectrpc.com/connect"
	_ "github.com/ncruces/go-sqlite3/embed"
	"github.com/ncruces/go-sqlite3/gormlite"
	slogGorm "github.com/orandin/slog-gorm"
	"google.golang.org/protobuf/types/known/timestamppb"
	"gorm.io/gorm"
	gormPrometheus "gorm.io/plugin/prometheus"
	"within.website/x/buf/patchouli"
	"within.website/x/buf/patchouli/patchouliconnect"
	"within.website/x/cmd/patchouli/ytdlp"
	"within.website/x/internal"
)

var (
	bind    = flag.String("bind", ":2934", "HTTP bind address")
	dataDir = flag.String("data-dir", "./var", "location to store data persistently")
)

func main() {
	internal.HandleStartup()

	slog.Info("starting up", "bind", *bind, "data-dir", *dataDir)

	db, err := connectDB()
	if err != nil {
		log.Fatalf("can't connect to DB: %v", err)
	}
	_ = db

	s := &Server{db: db}

	compress1KB := connect.WithCompressMinBytes(1024)

	mux := http.NewServeMux()
	mux.Handle(patchouliconnect.NewSyndicateHandler(s, compress1KB))

	slog.Info("listening", "url", "http://localhost"+*bind)
	log.Fatal(http.ListenAndServe(*bind, mux))
}

func connectDB() (*gorm.DB, error) {
	db, err := gorm.Open(gormlite.Open(filepath.Join(*dataDir, "data.db")), &gorm.Config{
		Logger: slogGorm.New(
			slogGorm.WithErrorField("err"),
			slogGorm.WithRecordNotFoundError(),
		),
	})
	if err != nil {
		return nil, fmt.Errorf("failed to connect to database: %w", err)
	}

	if err := db.AutoMigrate(
		&Video{},
	); err != nil {
		return nil, fmt.Errorf("failed to migrate schema: %w", err)
	}

	db.Use(gormPrometheus.New(gormPrometheus.Config{
		DBName: "mi",
	}))

	return db, nil
}

type Server struct {
	db *gorm.DB
}

func (s *Server) Info(
	ctx context.Context,
	req *connect.Request[patchouli.TwitchInfoReq],
) (
	*connect.Response[patchouli.TwitchInfoResp],
	error,
) {
	metadata, err := ytdlp.Metadata(ctx, req.Msg.GetUrl())
	if err != nil {
		slog.Error("can't fetch metadata for video", "url", req.Msg.GetUrl(), "err", err)
		return nil, connect.NewError(connect.CodeInternal, err)
	}

	result := &patchouli.TwitchInfoResp{
		Id:           metadata.ID,
		Title:        metadata.Title,
		ThumbnailUrl: metadata.Thumbnail,
		Duration:     metadata.DurationString,
		UploadDate:   timestamppb.New(metadata.UploadDate.Time),
		Url:          req.Msg.GetUrl(),
	}

	return connect.NewResponse(result), nil
}

func (s *Server) Download(
	ctx context.Context,
	req *connect.Request[patchouli.TwitchDownloadReq],
) (
	*connect.Response[patchouli.TwitchDownloadResp],
	error,
) {
	dir := filepath.Join(*dataDir, "video")

	if err := ytdlp.Download(ctx, req.Msg.GetUrl(), dir); err != nil {
		return nil, err
	}

	result := &patchouli.TwitchDownloadResp{
		Url:      req.Msg.Url,
		Location: dir,
	}

	return connect.NewResponse(result), nil
}

type Video struct {
	gorm.Model
	TwitchID  string `gorm:"uniqueIndex"`
	TwitchURL string `gorm:"uniqueIndex"`
	Title     string
	State     string
	BlogPath  sql.NullString
}