-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvalidate.go
78 lines (60 loc) · 1.93 KB
/
validate.go
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
package main
import (
"fmt"
"github.com/getsentry/sentry-go"
"net/http"
"os"
"pomu/qualities"
)
func (app *Application) ValidateLivestream(w http.ResponseWriter, r *http.Request) {
url := r.URL.Query().Get("url")
if len(url) <= 0 {
http.Error(w, "required parameter `url` is missing", http.StatusBadRequest)
return
}
id := qualities.ParseVideoID(url)
if id == url {
http.Error(w, "failed to parse video id from url", http.StatusInternalServerError)
return
}
video, err := GetVideoMetadata(id)
if err != nil {
sentry.CaptureException(err)
http.Error(w, "failed to get video meta data from youtube api", http.StatusInternalServerError)
return
}
valid, err := CheckChannelAgainstHolodex(video.Snippet.ChannelId)
if err != nil {
sentry.CaptureException(err)
http.Error(w, "failed to check channel against holodex", http.StatusInternalServerError)
return
}
response := map[string]any{
"channelId": video,
"valid": valid,
}
SerializeJson(w, response)
}
func CheckChannelAgainstHolodex(channelId string) (bool, error) {
// if the submissions are not restricted to only vtubers, always return true as we don't need to check against holodex then
if os.Getenv("RESTRICT_VTUBER_SUBMISSIONS") != "true" {
return true, nil
}
request, err := http.NewRequest("GET", fmt.Sprintf("https://holodex.net/api/v2/channels/%s", channelId), nil)
if err != nil {
sentry.CaptureException(err)
fmt.Printf("failed to start new request to holodex: %s", err)
return false, err
}
request.Header.Set("X-APIKEY", os.Getenv("HOLODEX_API_KEY"))
request.Header.Set("User-Agent", "pomu.app")
response, err := http.DefaultClient.Do(request)
defer response.Body.Close()
if err != nil {
sentry.CaptureException(err)
fmt.Printf("failed to send request to holodex: %s", err)
return false, err
}
// if the search does not result in a 404, they are a valid vtuber or clipper
return response.StatusCode == http.StatusOK, nil
}