fixyoutube.go (view raw)
1package main
2
3import (
4 "bytes"
5 "html/template"
6 "log"
7 "net/http"
8 "net/url"
9 "os"
10 "regexp"
11 "slices"
12 "time"
13
14 "github.com/BiRabittoh/fixyoutube-go/invidious"
15 "github.com/gorilla/mux"
16 "github.com/joho/godotenv"
17)
18
19var templatesDirectory = "templates/"
20var indexTemplate = template.Must(template.ParseFiles(templatesDirectory + "index.html"))
21var videoTemplate = template.Must(template.ParseFiles(templatesDirectory + "video.html"))
22var blacklist = []string{"favicon.ico", "robots.txt"}
23var userAgentRegex = regexp.MustCompile(`bot|facebook|embed|got|firefox\/92|firefox\/38|curl|wget|go-http|yahoo|generator|whatsapp|preview|link|proxy|vkshare|images|analyzer|index|crawl|spider|python|cfnetwork|node`)
24var apiKey string
25
26func indexHandler(w http.ResponseWriter, r *http.Request) {
27 buf := &bytes.Buffer{}
28 err := indexTemplate.Execute(buf, nil)
29 if err != nil {
30 http.Error(w, err.Error(), http.StatusInternalServerError)
31 return
32 }
33
34 buf.WriteTo(w)
35}
36
37func clearHandler(w http.ResponseWriter, r *http.Request) {
38 if r.Method != http.MethodPost {
39 http.Redirect(w, r, "/", http.StatusMovedPermanently)
40 return
41 }
42
43 err := r.ParseForm()
44 if err != nil {
45 http.Error(w, err.Error(), http.StatusInternalServerError)
46 return
47 }
48
49 providedKey := r.PostForm.Get("apiKey")
50 if providedKey != apiKey {
51 http.Error(w, "Wrong or missing API key.", http.StatusForbidden)
52 return
53 }
54
55 invidious.ClearDB()
56 http.Error(w, "Done.", http.StatusOK)
57}
58
59func videoHandler(videoId string, invidiousClient *invidious.Client, w http.ResponseWriter, r *http.Request) {
60
61 res := userAgentRegex.MatchString(r.UserAgent())
62 if !res {
63 url := "https://www.youtube.com/watch?v=" + videoId
64 http.Redirect(w, r, url, http.StatusMovedPermanently)
65 return
66 }
67
68 video, err := invidiousClient.GetVideo(videoId)
69 if err != nil {
70 http.Error(w, err.Error(), http.StatusInternalServerError)
71 return
72 }
73
74 buf := &bytes.Buffer{}
75 err = videoTemplate.Execute(buf, video)
76 if err != nil {
77 http.Error(w, err.Error(), http.StatusInternalServerError)
78 return
79 }
80 buf.WriteTo(w)
81}
82
83func watchHandler(invidiousClient *invidious.Client) http.HandlerFunc {
84 return func(w http.ResponseWriter, r *http.Request) {
85 u, err := url.Parse(r.URL.String())
86 if err != nil {
87 http.Error(w, err.Error(), http.StatusInternalServerError)
88 return
89 }
90
91 videoId := u.Query().Get("v")
92 videoHandler(videoId, invidiousClient, w, r)
93 }
94}
95
96func shortHandler(invidiousClient *invidious.Client) http.HandlerFunc {
97 return func(w http.ResponseWriter, r *http.Request) {
98 videoId := mux.Vars(r)["videoId"]
99
100 if slices.Contains(blacklist, videoId) {
101 http.Error(w, "Not a valid ID.", http.StatusBadRequest)
102 return
103 }
104
105 videoHandler(videoId, invidiousClient, w, r)
106 }
107}
108
109func proxyHandler(invidiousClient *invidious.Client) http.HandlerFunc {
110 return func(w http.ResponseWriter, r *http.Request) {
111 videoId := mux.Vars(r)["videoId"]
112
113 invidiousClient.ProxyVideo(videoId, w)
114 }
115}
116
117func main() {
118 err := godotenv.Load()
119 if err != nil {
120 log.Println("Error loading .env file")
121 }
122
123 port := os.Getenv("PORT")
124 if port == "" {
125 port = "3000"
126 }
127
128 apiKey = os.Getenv("API_KEY")
129 if apiKey == "" {
130 apiKey = "itsme"
131 }
132
133 myClient := &http.Client{Timeout: 10 * time.Second}
134 videoapi := invidious.NewClient(myClient)
135
136 r := mux.NewRouter()
137 r.HandleFunc("/", indexHandler)
138 r.HandleFunc("/clear", clearHandler)
139 r.HandleFunc("/watch", watchHandler(videoapi))
140 r.HandleFunc("/{videoId}", shortHandler(videoapi))
141 r.HandleFunc("/proxy/{videoId}", proxyHandler(videoapi))
142 /*
143 // native go implementation (useless until february 2024)
144 r := http.NewServeMux()
145 r.HandleFunc("/watch", watchHandler(videoapi))
146 r.HandleFunc("/{videoId}/", shortHandler(videoapi))
147 r.HandleFunc("/", indexHandler)
148 */
149 println("Serving on port", port)
150 http.ListenAndServe(":"+port, r)
151}