all repos — gopipe @ cec307a9e07ec65951325dcc377f9c24c25ac94b

Embed YouTube videos on Telegram, Discord and more!

use extemplate
Marco Andronaco andronacomarco@gmail.com
Wed, 16 Oct 2024 10:58:23 +0200
commit

cec307a9e07ec65951325dcc377f9c24c25ac94b

parent

83cd0bd9237351f9da3ef236617c2f915f2277c4

M go.modgo.mod

@@ -6,6 +6,7 @@ require (

github.com/birabittoh/myks v0.0.2 github.com/joho/godotenv v1.5.1 github.com/kkdai/youtube/v2 v2.10.1 + github.com/utking/extemplate v0.0.0-20240811163052-49c208254ff2 golang.org/x/time v0.7.0 )
M go.sumgo.sum

@@ -32,6 +32,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=

github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= +github.com/utking/extemplate v0.0.0-20240811163052-49c208254ff2 h1:GvYJOhvifh/8nUBNnb+LPk+U9p9SLWSyGu4GQr9fAi8= +github.com/utking/extemplate v0.0.0-20240811163052-49c208254ff2/go.mod h1:1WxnPx53d4RfgrIlNkhRTp37c/82H/KEUuul+Wh26dM= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
M src/app/handlers.gosrc/app/handlers.go

@@ -2,10 +2,10 @@ package app

import ( "fmt" + "html/template" "log" "net/http" "regexp" - "text/template" g "github.com/birabittoh/gopipe/src/globals" "golang.org/x/time/rate"

@@ -15,10 +15,21 @@ const (

fmtYouTubeURL = "https://www.youtube.com/watch?v=%s" err404 = "Not Found" err500 = "Internal Server Error" + heading = `<!-- + .d8888b. 88888888888 888 +d88P Y88b 888 888 +888 888 888 888 +888 .d88b. 888 888 888 88888b. .d88b. +888 88888 d88""88b 888 888 888 888 "88b d8P Y8b +888 888 888 888 888 888 888 888 888 88888888 +Y88b d88P Y88..88P 888 Y88b 888 888 d88P Y8b. + "Y8888P88 "Y88P" 888 "Y88888 88888P" "Y8888 + +A better way to embed YouTube videos on Telegram. +-->` ) var ( - templates = template.Must(template.ParseGlob("templates/*.html")) userAgentRegex = regexp.MustCompile(`(?i)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`) videoRegex = regexp.MustCompile(`(?i)^[a-z0-9_-]{11}$`) )

@@ -35,7 +46,7 @@ })

} func indexHandler(w http.ResponseWriter, r *http.Request) { - err := templates.ExecuteTemplate(w, "index.html", nil) + err := g.XT.ExecuteTemplate(w, "index.tmpl", nil) if err != nil { log.Println("indexHandler ERROR: ", err) http.Error(w, err500, http.StatusInternalServerError)

@@ -100,9 +111,10 @@ "Description": video.Description,

"Thumbnail": thumbnail, "Duration": video.Duration, "Debug": g.Debug, + "Heading": template.HTML(heading), } - err = templates.ExecuteTemplate(w, "video.html", data) + err = g.XT.ExecuteTemplate(w, "video.tmpl", data) if err != nil { log.Println("indexHandler ERROR: ", err) http.Error(w, err500, http.StatusInternalServerError)
M src/app/main.gosrc/app/main.go

@@ -16,6 +16,7 @@

func Main() { godotenv.Load() + // read env vars g.Debug = parseBool(os.Getenv("APP_DEBUG")) if g.Debug { log.Println("Debug mode enabled.")

@@ -29,6 +30,13 @@ }

g.Port = getEnvDefault("APP_PORT", "3000") + // set up extemplate + err := g.XT.ParseDir("templates", []string{".tmpl"}) + if err != nil { + log.Fatal(err) + } + + // set up http server r := http.NewServeMux() var serveMux http.Handler

@@ -41,7 +49,6 @@ }

r.HandleFunc("GET /", indexHandler) - // video handlers r.HandleFunc("GET /watch", videoHandler) r.HandleFunc("GET /shorts/{videoID}", videoHandler) r.HandleFunc("GET /{videoID}", videoHandler)
M src/globals/globals.gosrc/globals/globals.go

@@ -7,6 +7,7 @@ "time"

"github.com/birabittoh/myks" "github.com/kkdai/youtube/v2" + "github.com/utking/extemplate" ) var (

@@ -16,6 +17,7 @@ Port string

C = http.DefaultClient YT = youtube.Client{} + XT = extemplate.New() KS = myks.New[youtube.Video](3 * time.Hour) PKS *myks.KeyStore[bytes.Buffer]
A templates/base.tmpl

@@ -0,0 +1,30 @@

+<!doctype html> +{{ .Heading }} +<html lang="en"> + +<head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + {{block "meta" .}}{{end}} + <link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text fill=%22white%22 y=%22.9em%22 font-size=%2290%22>🛠</text></svg>"> + <link rel="stylesheet" href="https://unpkg.com/@picocss/pico@latest/css/pico.min.css"> +</head> + +<body> + <main class="container" style="max-width: 35rem"> + {{block "content" .}} + <h1>Base template</h1> + {{end}} + <hr> + <section> + <small> + <a href="https://github.com/birabittoh/gopipe" target="_blank">Source code</a> + </small> + <br /> + <small>• YouTube is a trademark of Google LLC. This app is not affiliated with Google LLC.</small> + </section> + </main> + {{block "scripts" .}}{{end}} +</body> + +</html>
D templates/index.html

@@ -1,48 +0,0 @@

-<!doctype html> -<html lang="en"> - -<head> - <meta charset="utf-8"> - <meta name="viewport" content="width=device-width, initial-scale=1"> - <meta property="og:title" content="GoTube" /> - <meta property="og:site_name" content="GoTube" /> - <meta property="og:description" content="Embed YouTube videos on Telegram, Discord and more!" /> - <title>GoTube</title> - - <link rel="icon" - href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text fill=%22white%22 y=%22.9em%22 font-size=%2290%22>🛠</text></svg>"> - <link rel="stylesheet" href="https://unpkg.com/@picocss/pico@latest/css/pico.min.css"> -</head> - -<body> - <main class="container" style="max-width: 35rem"> - <hgroup> - <h1>GoTube</h1> - <h2>Embed YouTube videos on Telegram, Discord and more!</h2> - </hgroup> - <p>GoTube serves fixed YouTube video embeds. Heavily inspired by <a href="https://fxtwitter.com">fxtwitter.com</a> and <a href="https://ddinstagram.com">ddinstagram.com</a>.</p> - - <section> - <header> - <h3 style="margin-bottom: 4px">How to use</h3> - <p>Replace <code>www.youtube.com</code> or <code>youtu.be</code> with <span id="changeme">this domain</span> to fix embeds for short videos.</p> - </header> - <video - src="https://github.com/birabittoh/FixYouTube-legacy/assets/26506860/2896d39e-a86e-47ce-939a-785b73d11683" - style="width: 100%; max-height: 100%;" autoplay loop muted> - Your browser does not support the video tag. - </video> - <hr> - <small><a href="https://github.com/birabittoh/gopipe" target="_blank">Source code available in GitHub!</a></small> - <br> - <small>• YouTube is a trademark of Google LLC. This app is not affiliated with Google LLC.</small> - </section> - </main> - <script> - code = document.createElement('code'); - code.innerText = window.location.host; - document.getElementById('changeme').replaceWith(code); - </script> -</body> - -</html>
A templates/index.tmpl

@@ -0,0 +1,36 @@

+{{ extends "base.tmpl" }} + +{{define "meta" -}} + <title>GoTube</title> + <meta property="og:title" content="GoTube" /> + <meta property="og:site_name" content="GoTube" /> + <meta property="og:description" content="Embed YouTube videos on Telegram, Discord and more!" /> +{{end}} + +{{define "content" -}} + <hgroup> + <h1>GoTube</h1> + <h2>Embed YouTube videos on Telegram, Discord and more!</h2> + </hgroup> + <p>GoTube serves fixed YouTube video embeds. Heavily inspired by <a href="https://fxtwitter.com">fxtwitter.com</a> and <a href="https://ddinstagram.com">ddinstagram.com</a>.</p> + + <section> + <header> + <h3 style="margin-bottom: 4px">How to use</h3> + <p>Replace <code>www.youtube.com</code> or <code>youtu.be</code> with <span id="changeme">this domain</span> to fix embeds for short videos.</p> + </header> + <video + src="https://github.com/birabittoh/FixYouTube-legacy/assets/26506860/2896d39e-a86e-47ce-939a-785b73d11683" + style="width: 100%; max-height: 100%;" autoplay loop muted> + Your browser does not support the video tag. + </video> + </section> +{{end}} + +{{define "scripts" -}} +<script> + code = document.createElement('code'); + code.innerText = window.location.host; + document.getElementById('changeme').replaceWith(code); +</script> +{{end}}
D templates/video.html

@@ -1,55 +0,0 @@

-<!DOCTYPE html> -<!-- -███████ ██ ██ ██ ██ ██ ███ ██ ██ ██████ ██ ██ ██████ ███████ -██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ -█████ ██ ███ ████ ██ ██ ██ ██ ██ ██ ██ ██████ █████ -██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ -██ ██ ██ ██ ██ ███ ███ ██ ███ ██████ ███████ -██ -██ A better way to embed YouTube videos on Telegram (inspired by FixTweet). -██ --> -<html lang="en"> - -<head> - <link rel="canonical" href="https://www.youtube.com/watch?v={{ .VideoID }}" /> - {{ if not .Debug }} - <meta http-equiv="refresh" content="0;url=https://www.youtube.com/watch?v={{ .VideoID }}" /> - {{ end }} - <meta charset="utf-8"> - <meta name="viewport" content="width=device-width, initial-scale=1"> - <meta property="og:url" content="https://www.youtube.com/watch?v={{ .VideoID }}" /> - <meta property="theme-color" content="0000FF" /> - <meta property="twitter:card" content="player" /> - <meta property="twitter:site" content="{{ .Uploader }}" /> - <meta property="twitter:creator" content="{{ .Uploader }}" /> - <meta property="twitter:title" content="{{ .Title }}" /> - <meta property="og:title" content="{{ .Title }}" /> - <meta property="og:description" content="{{ .Description }}" /> - <meta property="og:site_name" content="GoTube ({{ .Uploader }})" /> - <meta property="twitter:image" content="{{ .Thumbnail }}" /> - <meta property="twitter:player:stream:content_type" content="video/mp4" /> - {{ if .VideoURL }} - <meta property="og:video" content="{{ .VideoURL }}" /> - <meta property="og:video:secure_url" content="{{ .VideoURL }}" /> - <meta property="og:video:duration" content="{{ .Duration }}"> - <meta property="og:video:type" content="video/mp4" /> - {{ end }} - <link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text fill=%22white%22 y=%22.9em%22 font-size=%2290%22>🛠</text></svg>"> - <link rel="stylesheet" href="https://unpkg.com/@picocss/pico@latest/css/pico.min.css"> -</head> - -<body> - {{ if .VideoURL }} - <main class="container" style="max-width: 35rem"> - <video style="width: 100%" autoplay controls> - <source src="{{ .VideoURL }}" type="video/mp4" /> - </video> - <h2>{{ .Title }}</h2> - <h3>&gt; {{ .Uploader }}</h3> - <p>{{ .Description }}</p> - <a href="https://www.youtube.com/watch?v={{ .VideoID }}">Watch on YouTube.</a> - </main> - {{ end }} -</body> - -</html>
A templates/video.tmpl

@@ -0,0 +1,39 @@

+{{ extends "base.tmpl" }} + +{{define "meta" -}} + <title>{{ .Title }} - GoTube</title> + <link rel="canonical" href="https://www.youtube.com/watch?v={{ .VideoID }}" /> + {{ if not .Debug }} + <meta http-equiv="refresh" content="0;url=https://www.youtube.com/watch?v={{ .VideoID }}" /> + {{ end }} + <meta property="og:url" content="https://www.youtube.com/watch?v={{ .VideoID }}" /> + <meta property="theme-color" content="0000FF" /> + <meta property="twitter:card" content="player" /> + <meta property="twitter:site" content="{{ .Uploader }}" /> + <meta property="twitter:creator" content="{{ .Uploader }}" /> + <meta property="twitter:title" content="{{ .Title }}" /> + <meta property="og:title" content="{{ .Title }}" /> + <meta property="og:description" content="{{ .Description }}" /> + <meta property="og:site_name" content="GoTube ({{ .Uploader }})" /> + <meta property="twitter:image" content="{{ .Thumbnail }}" /> + <meta property="twitter:player:stream:content_type" content="video/mp4" /> + {{ if .VideoURL }} + <meta property="og:video" content="{{ .VideoURL }}" /> + <meta property="og:video:secure_url" content="{{ .VideoURL }}" /> + <meta property="og:video:duration" content="{{ .Duration }}"> + <meta property="og:video:type" content="video/mp4" /> + {{ end }} +{{end}} + + +{{define "content" -}} + <video style="width: 100%" autoplay controls> + <source src="{{ .VideoURL }}" type="video/mp4" /> + </video> + <h2>{{ .Title }}</h2> + <h3>&gt; {{ .Uploader }}</h3> + <pre>{{ .Description }}</pre> + <a href="https://www.youtube.com/watch?v={{ .VideoID }}">Watch on YouTube</a> + <br /> + <a href="/">What is this?</a> +{{end}}