all repos — gopipe @ 28aff114e23cb584fea3a9c94c0836f4cb521eca

Embed YouTube videos on Telegram, Discord and more!

initial commit
Marco Andronaco andronacomarco@gmail.com
Tue, 15 Oct 2024 18:36:25 +0200
commit

28aff114e23cb584fea3a9c94c0836f4cb521eca

A .env.example

@@ -0,0 +1,2 @@

+APP_DEBUG=true +APP_PORT=3000
A .github/workflows/publish.yaml

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

+# +name: Create and publish a Docker image + +# Configures this workflow to run every time a change is pushed to the branch called `main`. +on: + push: + branches: ['main'] + +# Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds. +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +# There is a single job in this workflow. It's configured to run on the latest available version of Ubuntu. +jobs: + build-and-push-image: + runs-on: ubuntu-latest + # Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job. + permissions: + contents: read + packages: write + # + steps: + - name: Checkout repository + uses: actions/checkout@v3 + # Uses the `docker/login-action` action to log in to the Container registry registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here. + - name: Log in to the Container registry + uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + # This step uses [docker/metadata-action](https://github.com/docker/metadata-action#about) to extract tags and labels that will be applied to the specified image. The `id` "meta" allows the output of this step to be referenced in a subsequent step. The `images` value provides the base name for the tags and labels. + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + # This step uses the `docker/build-push-action` action to build the image, based on your repository's `Dockerfile`. If the build succeeds, it pushes the image to GitHub Packages. + # It uses the `context` parameter to define the build's context as the set of files located in the specified path. For more information, see "[Usage](https://github.com/docker/build-push-action#usage)" in the README of the `docker/build-push-action` repository. + # It uses the `tags` and `labels` parameters to tag and label the image with the output from the "meta" step. + - name: Build and push Docker image + uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4 + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }}
A .gitignore

@@ -0,0 +1,2 @@

+__debug_bin* +*.env
A .vscode/launch.json

@@ -0,0 +1,15 @@

+{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Launch Package", + "type": "go", + "request": "launch", + "mode": "auto", + "program": "." + } + ] +}
A Dockerfile

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

+# syntax=docker/dockerfile:1 + +FROM golang:alpine AS builder + +WORKDIR /build + +# Download Go modules +COPY go.mod go.sum ./ +RUN go mod download + +# Transfer source code +COPY src ./src +COPY *.go ./ + +# Build +RUN CGO_ENABLED=0 go build -trimpath -o /dist/fixyoutube + +# Test +FROM build-stage AS run-test-stage +RUN go test -v ./... + +FROM scratch AS build-release-stage + +WORKDIR /app + +COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ +COPY --from=builder /dist . +COPY templates ./templates + +ENTRYPOINT ["./fixyoutube"]
A LICENSE

@@ -0,0 +1,21 @@

+MIT License + +Copyright (c) 2024 birabittoh + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE.
A README.md

@@ -0,0 +1,37 @@

+# FixYouTube +Embed YouTube videos on Telegram, Discord and more! + +## How to use: +Replace `www.youtube.com` or `youtu.be` with `y.outube.duckdns.org` to fix embeds for short videos. + +https://github.com/birabittoh/FixYouTube-legacy/assets/26506860/e1ad5397-41c8-4073-9b3e-598c66241255 + +## Instructions + +First of all, you should set the API key to whatever you want: +``` +cp .env.example .env +nano .env +``` + +### Docker with reverse proxy +Copy the template config file and make your adjustments. My configuration is based on [DuckDNS](http://duckdns.org/) but you can use whatever provider you find [here](https://docs.linuxserver.io/general/swag#docker-compose). + +``` +cp docker/swag.env.example docker/swag.env +nano docker/swag.env +``` + +Finally: `docker-compose up -d`. + +### Docker without reverse proxy +Just run: +``` +docker-compose -f docker-compose.simple.yaml up -d +``` + +## Test and debug locally +``` +go test -v ./... +go run . +```
A docker-compose.simple.yaml

@@ -0,0 +1,10 @@

+services: + app: + build: . + image: ghcr.io/birabittoh/gopipe:main + container_name: fixyoutube + restart: unless-stopped + ports: + - 3000:3000 + env_file: + - .env
A docker-compose.yaml

@@ -0,0 +1,22 @@

+services: + app: + build: . + image: ghcr.io/birabittoh/gopipe:main + container_name: fixyoutube + restart: unless-stopped + env_file: + - .env + swag: + image: ghcr.io/linuxserver/swag + container_name: swag + cap_add: + - NET_ADMIN + env_file: + - docker/swag.env + volumes: + #- /etc/config/swag:/config + - ./docker/fixyoutube.subdomain.conf:/config/nginx/proxy-confs/fixyoutube.subdomain.conf:ro + ports: + - 443:443 + - 80:80 + restart: unless-stopped
A go.mod

@@ -0,0 +1,19 @@

+module github.com/birabittoh/gopipe + +go 1.23.2 + +require ( + github.com/birabittoh/myks v0.0.2 + github.com/joho/godotenv v1.5.1 + github.com/kkdai/youtube/v2 v2.10.1 + golang.org/x/time v0.7.0 +) + +require ( + github.com/bitly/go-simplejson v0.5.1 // indirect + github.com/dlclark/regexp2 v1.11.0 // indirect + github.com/dop251/goja v0.0.0-20240220182346-e401ed450204 // indirect + github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect + github.com/google/pprof v0.0.0-20240227163752-401108e1b7e7 // indirect + golang.org/x/text v0.14.0 // indirect +)
A go.sum

@@ -0,0 +1,69 @@

+github.com/birabittoh/myks v0.0.2 h1:EBukMUsAflwiqdNo4LE7o2WQdEvawty5ewCZWY+IXSU= +github.com/birabittoh/myks v0.0.2/go.mod h1:klNWaeUWm7TmhnBHBMt9vALwCHW11/Xw1BpCNkCx7hs= +github.com/bitly/go-simplejson v0.5.1 h1:xgwPbetQScXt1gh9BmoJ6j9JMr3TElvuIyjR8pgdoow= +github.com/bitly/go-simplejson v0.5.1/go.mod h1:YOPVLzCfwK14b4Sff3oP1AmGhI9T9Vsg84etUnlyp+Q= +github.com/chzyer/logex v1.2.0/go.mod h1:9+9sk7u7pGNWYMkh0hdiL++6OeibzJccyQU4p4MedaY= +github.com/chzyer/readline v1.5.0/go.mod h1:x22KAscuvRqlLoK9CsoYsmxoXZMMFVyOl86cAH8qUic= +github.com/chzyer/test v0.0.0-20210722231415-061457976a23/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/dlclark/regexp2 v1.4.1-0.20201116162257-a2a8dda75c91/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc= +github.com/dlclark/regexp2 v1.7.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= +github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI= +github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= +github.com/dop251/goja v0.0.0-20211022113120-dc8c55024d06/go.mod h1:R9ET47fwRVRPZnOGvHxxhuZcbrMCuiqOz3Rlrh4KSnk= +github.com/dop251/goja v0.0.0-20240220182346-e401ed450204 h1:O7I1iuzEA7SG+dK8ocOBSlYAA9jBUmCYl/Qa7ey7JAM= +github.com/dop251/goja v0.0.0-20240220182346-e401ed450204/go.mod h1:QMWlm50DNe14hD7t24KEqZuUdC9sOTy8W6XbCU1mlw4= +github.com/dop251/goja_nodejs v0.0.0-20210225215109-d91c329300e7/go.mod h1:hn7BA7c8pLvoGndExHudxTDKZ84Pyvv+90pbBjbTz0Y= +github.com/dop251/goja_nodejs v0.0.0-20211022123610-8dd9abb0616d/go.mod h1:DngW8aVqWbuLRMHItjPUyqdj+HWPvnQe8V8y1nDpIbM= +github.com/go-sourcemap/sourcemap v2.1.3+incompatible h1:W1iEw64niKVGogNgBN3ePyLFfuisuzeidWPMPWmECqU= +github.com/go-sourcemap/sourcemap v2.1.3+incompatible/go.mod h1:F8jJfvm2KbVjc5NqelyYJmf/v5J0dwNLS2mL4sNA1Jg= +github.com/google/pprof v0.0.0-20230207041349-798e818bf904/go.mod h1:uglQLonpP8qtYCYyzA+8c/9qtqgA3qsXGYqCPKARAFg= +github.com/google/pprof v0.0.0-20240227163752-401108e1b7e7 h1:y3N7Bm7Y9/CtpiVkw/ZWj6lSlDF3F74SfKwfTCer72Q= +github.com/google/pprof v0.0.0-20240227163752-401108e1b7e7/go.mod h1:czg5+yv1E0ZGTi6S6vVK1mke0fV+FaUhNGcd6VRS9Ik= +github.com/ianlancetaylor/demangle v0.0.0-20220319035150-800ac71e25c2/go.mod h1:aYm2/VgdVmcIU8iMfdMvDMsRAQjcfZSKFby6HOFvi/w= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/kkdai/youtube/v2 v2.10.1 h1:jdPho4R7VxWoRi9Wx4ULMq4+hlzSVOXxh4Zh83f2F9M= +github.com/kkdai/youtube/v2 v2.10.1/go.mod h1:qL8JZv7Q1IoDs4nnaL51o/hmITXEIvyCIXopB0oqgVM= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= +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/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= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/time v0.7.0 h1:ntUhktv3OPE6TgYxXWv9vKvUSJyIFJlyohwbkEwPrKQ= +golang.org/x/time v0.7.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
A main.go

@@ -0,0 +1,7 @@

+package main + +import "github.com/birabittoh/gopipe/src/app" + +func main() { + app.Main() +}
A src/app/handlers.go

@@ -0,0 +1,91 @@

+package app + +import ( + "log" + "net/http" + "regexp" + "text/template" + "time" + + g "github.com/birabittoh/gopipe/src/globals" +) + +const err500 = "Internal Server Error" +const urlDuration = 6 * time.Hour + +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}$`) +) + +func indexHandler(w http.ResponseWriter, r *http.Request) { + err := templates.ExecuteTemplate(w, "index.html", nil) + if err != nil { + log.Println("indexHandler ERROR: ", err) + http.Error(w, err500, http.StatusInternalServerError) + return + } +} + +func videoHandler(w http.ResponseWriter, r *http.Request) { + videoID := r.URL.Query().Get("v") + if videoID == "" { + videoID = r.PathValue("videoID") + if videoID == "" { + http.Error(w, "Missing video ID", http.StatusBadRequest) + return + } + } + + url := "https://www.youtube.com/watch?v=" + videoID + + if !userAgentRegex.MatchString(r.UserAgent()) { + log.Println("Regex did not match.") + if !g.Debug { + log.Println("Redirecting. UA:", r.UserAgent()) + http.Redirect(w, r, url, http.StatusFound) + return + } + } + + if !videoRegex.MatchString(videoID) { + log.Println("Invalid video ID: ", videoID) + http.Error(w, "Invalid video ID.", http.StatusBadRequest) + return + } + + video, err := g.YT.GetVideo(url) + if err != nil { + log.Println("videoHandler ERROR: ", err) + http.Error(w, err500, http.StatusInternalServerError) + return + } + + formats := video.Formats.WithAudioChannels() + if len(formats) == 0 { + log.Println("videoHandler ERROR: ", err) + http.Error(w, err500, http.StatusInternalServerError) + return + } + + // TODO: check formats[i].ContentLength + // g.KS.Set(videoID, formats[0].URL, urlDuration) + + data := map[string]interface{}{ + "VideoID": videoID, + "VideoURL": formats[0].URL, + "Uploader": video.Author, + "Title": video.Title, + "Description": video.Description, + "Duration": video.Duration, + "Debug": g.Debug, + } + + err = templates.ExecuteTemplate(w, "video.html", data) + if err != nil { + log.Println("indexHandler ERROR: ", err) + http.Error(w, err500, http.StatusInternalServerError) + return + } +}
A src/app/main.go

@@ -0,0 +1,70 @@

+package app + +import ( + "log" + "net/http" + "os" + "strings" + + g "github.com/birabittoh/gopipe/src/globals" + "github.com/joho/godotenv" + "golang.org/x/time/rate" +) + +func limit(limiter *rate.Limiter, next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if !limiter.Allow() { + status := http.StatusTooManyRequests + http.Error(w, http.StatusText(status), status) + return + } + next.ServeHTTP(w, r) + }) +} + +func getEnvDefault(key string, def string) string { + res := os.Getenv(key) + if res == "" { + return def + } + return res +} + +func parseBool(s string) bool { + return strings.ToLower(s) == "true" || s == "1" +} + +func Main() { + godotenv.Load() + + g.Debug = parseBool(os.Getenv("APP_DEBUG")) + if g.Debug { + log.Println("Debug mode enabled.") + } + + g.Port = getEnvDefault("APP_PORT", "3000") + + r := http.NewServeMux() + + var serveMux http.Handler + if g.Debug { + serveMux = r + } else { + limiter := rate.NewLimiter(rate.Limit(1), 3) // TODO: use env vars + serveMux = limit(limiter, r) + } + + r.HandleFunc("GET /", indexHandler) + // r.HandleFunc("/proxy/{videoId}", proxyHandler) + // r.HandleFunc("/clear", clearHandler) + + // video handlers + r.HandleFunc("GET /watch", videoHandler) + r.HandleFunc("GET /{videoID}", videoHandler) + r.HandleFunc("GET /shorts/{videoID}", videoHandler) + + // r.HandleFunc("GET /robots.txt", robotsHandler) + + log.Println("Serving on port " + g.Port) + log.Fatal(http.ListenAndServe(":"+g.Port, serveMux)) +}
A src/globals/globals.go

@@ -0,0 +1,14 @@

+package globals + +import ( + "github.com/birabittoh/myks" + "github.com/kkdai/youtube/v2" +) + +var ( + Debug bool + Port string + + YT = youtube.Client{} + KS = myks.New[string](0) +)
A swag/fixyoutube.subdomain.conf

@@ -0,0 +1,23 @@

+# make sure that your app container is named fixyoutube +# make sure that your dns has a cname set for fixyoutube + +server { + listen 443 ssl; + listen [::]:443 ssl; + http2 on; + + server_name y.*; # change your subdomain here + + include /config/nginx/ssl.conf; + + client_max_body_size 0; + + location / { + include /config/nginx/proxy.conf; + include /config/nginx/resolver.conf; + set $upstream_app fixyoutube; + set $upstream_port 3000; + set $upstream_proto http; + proxy_pass $upstream_proto://$upstream_app:$upstream_port; + } +}
A swag/swag.env.example

@@ -0,0 +1,13 @@

+PUID=1000 +PGID=1000 +TZ=Etc/UTC +URL=domain.duckdns.org +SUBDOMAINS=wildcard +VALIDATION=duckdns +CERTPROVIDER= #optional +DNSPLUGIN= #optional +DUCKDNSTOKEN=abc123ab-c123-abc1-23ab-c123abc123ab +EMAIL=your@email.com +ONLY_SUBDOMAINS=false +EXTRA_DOMAINS=y +STAGING=false
A templates/index.html

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

+<!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="FixYouTube" /> + <meta property="og:site_name" content="FixYouTube" /> + <meta property="og:description" content="Embed YouTube videos on Telegram, Discord and more!" /> + <title>FixYouTube</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>FixYouTube</h1> + <h2>Embed YouTube videos on Telegram, Discord and more!</h2> + </hgroup> + <p>FixYouTube 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/video.html

@@ -0,0 +1,33 @@

+<!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 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="FixYouTube ({{ .Uploader }})" /> + <meta property="twitter:image" content="0" /> + <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 }} +</head><body></body></html>