Compare commits
78 Commits
99c6bd379a
..
v1.0.2
| Author | SHA1 | Date | |
|---|---|---|---|
| 509dab5395 | |||
|
6b9934a221
|
|||
| adefe2f177 | |||
|
084b7ed979
|
|||
| 51e1cc5e85 | |||
|
d3e6ddebcd
|
|||
|
558f95ad9d
|
|||
| e0de621e41 | |||
|
451333860f
|
|||
|
e3b5542b78
|
|||
|
aaf3338797
|
|||
|
8f2bda0b52
|
|||
|
91f2eb39cb
|
|||
| 82b867ae9c | |||
| ebb6c11d49 | |||
|
d4beb62fdd
|
|||
|
13d65ef6ce
|
|||
|
8b1cead3ea
|
|||
|
4347bf733c
|
|||
|
5748834af4
|
|||
|
06d2a5ef01
|
|||
|
bdcd77eff1
|
|||
|
4541559f47
|
|||
|
906ef98bd5
|
|||
|
640c952b20
|
|||
|
f30686fd7c
|
|||
|
eceb3b3d76
|
|||
|
23a27217df
|
|||
|
82a6421c88
|
|||
|
c72bc42496
|
|||
|
991a5f376f
|
|||
|
13bd3b82db
|
|||
|
2c99fbf517
|
|||
|
890a0dd5c9
|
|||
|
6fd6160edf
|
|||
|
43c2fa2f91
|
|||
| 81968a6811 | |||
|
7d83fd8518
|
|||
| 7b34919e72 | |||
|
98fec74ac0
|
|||
|
481c8d9bb6
|
|||
|
3cac63ba82
|
|||
|
b3d779374b
|
|||
| 2269104805 | |||
| 4bea5e020f | |||
| 60803961b3 | |||
|
197e35314a
|
|||
|
db0c0a3893
|
|||
|
46fec9b85a
|
|||
|
204404b761
|
|||
| 6e7fc73420 | |||
|
67c85aebf9
|
|||
| b9088d932c | |||
|
8c1d044f79
|
|||
| f688ee035f | |||
|
7fd5d93b6d
|
|||
|
600fde2e71
|
|||
|
a2a0aabb3a
|
|||
|
bd643344ef
|
|||
|
63a6c38079
|
|||
|
4f34872f10
|
|||
|
6ae35ec636
|
|||
|
725c650deb
|
|||
|
d7ad90a1d5
|
|||
| ac5abffd74 | |||
|
1c43c62523
|
|||
| bef0a4d593 | |||
|
bf7739228f
|
|||
| 270934613f | |||
|
5cea64626c
|
|||
| 70d72759ce | |||
|
c537874adb
|
|||
|
b496d14cf7
|
|||
|
7bd9ef1230
|
|||
|
c2de1abfd2
|
|||
|
aa9dc58259
|
|||
|
4bad5f72da
|
|||
|
635d5d5113
|
@@ -23,14 +23,14 @@ jobs:
|
||||
|
||||
# Set up Docker Buildx for building the image
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v3
|
||||
uses: docker/setup-buildx-action@v4
|
||||
with:
|
||||
driver: remote
|
||||
endpoint: 'tcp://buildkitd:1234'
|
||||
endpoint: 'tcp://buildkitd.gitea-runner.svc.cluster.local:1234'
|
||||
|
||||
# Log in to the Gitea container registry
|
||||
- name: Log in to Gitea Container Registry
|
||||
uses: docker/login-action@v3
|
||||
uses: docker/login-action@v4
|
||||
with:
|
||||
registry: git.dubyatp.xyz
|
||||
username: williamp
|
||||
@@ -38,7 +38,7 @@ jobs:
|
||||
|
||||
# Build and push the Docker image
|
||||
- name: Build and Push Docker Image
|
||||
uses: docker/build-push-action@v6
|
||||
uses: docker/build-push-action@v7
|
||||
with:
|
||||
context: . # Build context (current directory)
|
||||
file: ./Dockerfile # Path to Dockerfile
|
||||
@@ -75,8 +75,13 @@ jobs:
|
||||
git config --local user.signingkey ~/.ssh/id_ed25519
|
||||
git config --local gpg.format ssh
|
||||
git config --local commit.gpgsign true
|
||||
git commit -a -m "yt-dlp-bot: deploy update to ${{ needs.build-and-push.outputs.sha_short }}"
|
||||
|
||||
if [ -n "$(git status --porcelain)" ]; then
|
||||
git commit -a -m "yt-dlp-bot: deploy update to ${{ needs.build-and-push.outputs.sha_short }}"
|
||||
else
|
||||
echo "No changes to commit, skipping..."
|
||||
fi
|
||||
exit 0
|
||||
- name: Push changes
|
||||
uses: ad-m/github-push-action@v1.0.0
|
||||
with:
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
name: Build only (for PRs)
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
|
||||
jobs:
|
||||
build-only:
|
||||
runs-on: ubuntu-latest # Use a runner with Docker support
|
||||
container: ghcr.io/catthehacker/ubuntu:act-latest # Image with Docker pre-installed
|
||||
outputs:
|
||||
sha_short: ${{ steps.vars.outputs.sha_short }}
|
||||
steps:
|
||||
# Checkout the repository code
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v6
|
||||
|
||||
- name: Set outputs
|
||||
id: vars
|
||||
run: |
|
||||
echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
|
||||
|
||||
# Set up Docker Buildx for building the image
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v4
|
||||
with:
|
||||
driver: remote
|
||||
endpoint: 'tcp://buildkitd.gitea-runner.svc.cluster.local:1234'
|
||||
|
||||
# Build the Docker image
|
||||
- name: Build Docker Image
|
||||
uses: docker/build-push-action@v7
|
||||
with:
|
||||
context: . # Build context (current directory)
|
||||
file: ./Dockerfile # Path to Dockerfile
|
||||
platforms: "linux/amd64,linux/arm64"
|
||||
+91
-6
@@ -1,6 +1,91 @@
|
||||
FROM python:3.14.2-alpine3.22
|
||||
COPY ./app /app
|
||||
WORKDIR /app
|
||||
RUN apk add ffmpeg deno
|
||||
RUN pip install -r requirements.txt
|
||||
CMD ["python", "/app/main.py"]
|
||||
# Portions of this Dockerfile are sourced from GPLv3 licensed `yt-dlp slim` by Henrique Almeida (https://github.com/h3nc4/yt-dlp-slim)
|
||||
# Derivations to this Dockerfile in this repository following March 3, 2026 should be considered licensed under this project's MIT license (see ../LICENSE) unless otherwise stated
|
||||
|
||||
########################################
|
||||
# Versions
|
||||
ARG YT_DLP_VERSION="2026.03.03"
|
||||
|
||||
################################################################################
|
||||
# Deno builder stage
|
||||
FROM denoland/deno:bin-2.6.6@sha256:9f18d20207f2699595ea26d14e0b7e123cd0cd01100a577bc11f8ca5906c2d81 AS deno-builder
|
||||
|
||||
################################################################################
|
||||
# YT-DLP builder stage
|
||||
FROM alpine:3.23@sha256:25109184c71bdad752c8312a8623239686a9a2071e8825f20acb8f2198c3f659 AS yt-dlp-builder
|
||||
ARG YT_DLP_VERSION
|
||||
ARG TARGETARCH
|
||||
|
||||
RUN mkdir -p /rootfs/target /rootfs/tmp /rootfs/bin
|
||||
|
||||
ADD "https://github.com/yt-dlp/yt-dlp/releases/download/${YT_DLP_VERSION}/SHA2-256SUMS" /SHA2-256SUMS
|
||||
ADD "https://github.com/yt-dlp/yt-dlp/releases/download/${YT_DLP_VERSION}/SHA2-256SUMS.sig" /SHA2-256SUMS.sig
|
||||
ADD "https://keyserver.ubuntu.com/pks/lookup?op=get&search=0xAC0CBBE6848D6A873464AF4E57CF65933B5A7581" "/yt-dlp_pubkey.asc"
|
||||
|
||||
RUN apk add --no-cache gnupg && \
|
||||
gpg --import /yt-dlp_pubkey.asc && \
|
||||
gpg --verify /SHA2-256SUMS.sig /SHA2-256SUMS
|
||||
|
||||
RUN case "$TARGETARCH" in \
|
||||
amd64) YT_DLP_FILE="yt-dlp_linux" ;; \
|
||||
arm64) YT_DLP_FILE="yt-dlp_linux_aarch64" ;; \
|
||||
*) echo "Unsupported architecture: $TARGETARCH" && exit 1 ;; \
|
||||
esac && \
|
||||
wget -qO "/${YT_DLP_FILE}" "https://github.com/yt-dlp/yt-dlp/releases/download/${YT_DLP_VERSION}/${YT_DLP_FILE}" && \
|
||||
grep " ${YT_DLP_FILE}$" /SHA2-256SUMS | sha256sum -c - && \
|
||||
mv "/${YT_DLP_FILE}" /rootfs/bin/yt-dlp && \
|
||||
chmod 755 /rootfs/bin/yt-dlp && \
|
||||
chmod 1777 /rootfs/tmp
|
||||
|
||||
################################################################################
|
||||
# FFmpeg builder stage
|
||||
FROM debian:13-slim@sha256:1d3c811171a08a5adaa4a163fbafd96b61b87aa871bbc7aa15431ac275d3d430 AS ffmpeg-builder
|
||||
RUN apt-get update && \
|
||||
apt-get install -y --no-install-recommends ffmpeg
|
||||
COPY --from=yt-dlp-builder /rootfs/bin/yt-dlp /yt-dlp
|
||||
RUN mkdir -p /rootfs/bin && \
|
||||
cp /usr/bin/ffmpeg /usr/bin/ffprobe /rootfs/bin/ && \
|
||||
{ ldd /usr/bin/ffmpeg; ldd /yt-dlp; } 2>/dev/null | \
|
||||
grep -o '/[^ ]*' | sort -u | \
|
||||
xargs -I '{}' cp --parents '{}' /rootfs && \
|
||||
LIBDIR=$(dirname "$(find /rootfs -name 'libc.so.6' | head -1)") && \
|
||||
for stub in libutil.so.1 libdl.so.2 libpthread.so.0 librt.so.1; do \
|
||||
[ -f "${LIBDIR}/${stub}" ] || ln -sf libc.so.6 "${LIBDIR}/${stub}"; \
|
||||
done
|
||||
|
||||
################################################################################
|
||||
# App builder stage
|
||||
FROM golang:1.26.1-trixie@sha256:ab8c4944b04c6f97c2b5bffce471b7f3d55f2228badc55eae6cce87596d5710b AS app-builder
|
||||
|
||||
COPY app/ /opt/app
|
||||
WORKDIR /opt/app
|
||||
|
||||
RUN go get && go build -o out/yt-dlp-bot
|
||||
|
||||
################################################################################
|
||||
# Final squashed image
|
||||
FROM scratch AS final
|
||||
|
||||
# Copy deno, yt-dlp, and ffmpeg binaries
|
||||
COPY --from=deno-builder /deno /bin/deno
|
||||
COPY --from=yt-dlp-builder /rootfs /
|
||||
COPY --from=ffmpeg-builder /rootfs/ /
|
||||
|
||||
# Copy yt-dlp-bot app binary
|
||||
COPY --from=app-builder /opt/app/out/yt-dlp-bot /bin/
|
||||
|
||||
# Copy SSL CA's (needed for Discord)
|
||||
COPY --from=app-builder /etc/ssl/certs /etc/ssl/certs
|
||||
|
||||
WORKDIR /target
|
||||
ENV XDG_CACHE_HOME=/tmp/.cache
|
||||
|
||||
ENV YTDLP_BIN=/bin/yt-dlp
|
||||
|
||||
ENTRYPOINT ["/bin/yt-dlp-bot"]
|
||||
|
||||
LABEL org.opencontainers.image.title="yt-dlp bot" \
|
||||
org.opencontainers.image.description="A totally overengineered Discord bot to locally download YouTube videos for private use" \
|
||||
org.opencontainers.image.authors="William Peebles <me@williamtpeebles.com>" \
|
||||
org.opencontainers.image.vendor="William Peebles" \
|
||||
org.opencontainers.image.licenses="MIT" \
|
||||
org.opencontainers.image.source="https://git.dubyatp.xyz/williamp/yt-dlp-bot"
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 23 KiB |
+109
@@ -0,0 +1,109 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
// startAsyncDownload initiates a download in a goroutine and handles progress updates
|
||||
func startAsyncDownload(s *discordgo.Session, i *discordgo.InteractionCreate, requester, url, videoFormatID, videoFormatName, audioFormatID, audioFormatName, outputDir, tempDir string) {
|
||||
progressChan := make(chan ProgressUpdate, 1)
|
||||
resultChan := make(chan DownloadResult, 1)
|
||||
|
||||
// Start download in goroutine
|
||||
go func() {
|
||||
defer close(resultChan)
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
// Handle panic from DownloadVideo
|
||||
resultChan <- DownloadResult{
|
||||
Success: false,
|
||||
Message: fmt.Sprintf("❌ **Download Failed**: %v", r),
|
||||
URL: url,
|
||||
Format: fmt.Sprintf("video: %s, audio: %s", videoFormatID, audioFormatID),
|
||||
Error: fmt.Errorf("%v", r),
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
// Call DownloadVideo (it panics on error instead of returning error)
|
||||
DownloadVideo(outputDir, tempDir, url, DownloadOptions{
|
||||
EmbedThumbnail: true,
|
||||
IncludeSubtitles: true,
|
||||
VideoFormatID: videoFormatID,
|
||||
AudioFormatID: audioFormatID,
|
||||
}, progressChan)
|
||||
|
||||
// If we reach here, download was successful
|
||||
resultChan <- DownloadResult{
|
||||
Success: true,
|
||||
Message: "✅ **Successfully Downloaded**",
|
||||
URL: url,
|
||||
Format: fmt.Sprintf("video: %s, audio: %s", videoFormatID, audioFormatID),
|
||||
Error: nil,
|
||||
}
|
||||
}()
|
||||
|
||||
// Handle progress and results asynchronously
|
||||
go func() {
|
||||
_, err := s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
|
||||
Content: ptr(fmt.Sprintf("%s **Starting Download**", loading_emoji)),
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("Error updating interaction: %v", err)
|
||||
}
|
||||
|
||||
for {
|
||||
select {
|
||||
case prog, ok := <-progressChan:
|
||||
if !ok {
|
||||
progressChan = nil
|
||||
continue
|
||||
}
|
||||
|
||||
var content string
|
||||
if prog.Phase == "post-processing" {
|
||||
content = fmt.Sprintf("%s **Post Processing**", loading_emoji)
|
||||
} else {
|
||||
var progressStr string
|
||||
if prog.DownloadedBytes > 0 {
|
||||
progressStr = formatBytes(prog.DownloadedBytes) + " downloaded"
|
||||
} else {
|
||||
progressStr = "starting..."
|
||||
}
|
||||
content = fmt.Sprintf("%s **Downloading Video**: %s", loading_emoji, progressStr)
|
||||
}
|
||||
|
||||
_, err := s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
|
||||
Content: ptr(content),
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("Error updating progress: %v", err)
|
||||
}
|
||||
|
||||
case result := <-resultChan:
|
||||
if result.Success {
|
||||
_, err = s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
|
||||
Content: ptr("✅ **Successfully Downloaded**"),
|
||||
})
|
||||
_, err = s.FollowupMessageCreate(i.Interaction, false, &discordgo.WebhookParams{
|
||||
Content: fmt.Sprintf("## Video Downloaded \n**URL**: %s \n**Quality**: %s + %s \n**Requested By**: <@%s> \n", result.URL, videoFormatName, audioFormatName, requester),
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("Error updating interaction: %v", err)
|
||||
}
|
||||
} else {
|
||||
_, err = s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
|
||||
Content: ptr("❌ **Download Failed**: " + result.Message),
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("Error updating interaction: %v", err)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
+11
-8
@@ -3,12 +3,15 @@ module git.dubyatp.xyz/williamp/yt-dlp-bot
|
||||
go 1.25.2
|
||||
|
||||
require (
|
||||
github.com/ProtonMail/go-crypto v1.3.0 // indirect
|
||||
github.com/bwmarrin/discordgo v0.29.0 // indirect
|
||||
github.com/cloudflare/circl v1.6.1 // indirect
|
||||
github.com/gorilla/websocket v1.4.2 // indirect
|
||||
github.com/lrstanley/go-ytdlp v1.2.7 // indirect
|
||||
github.com/ulikunitz/xz v0.5.13 // indirect
|
||||
golang.org/x/crypto v0.41.0 // indirect
|
||||
golang.org/x/sys v0.35.0 // indirect
|
||||
github.com/bwmarrin/discordgo v0.29.0
|
||||
github.com/lrstanley/go-ytdlp v1.3.3
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/ProtonMail/go-crypto v1.3.0 // indirect
|
||||
github.com/cloudflare/circl v1.6.3 // indirect
|
||||
github.com/gorilla/websocket v1.4.2 // indirect
|
||||
github.com/ulikunitz/xz v0.5.15 // indirect
|
||||
golang.org/x/crypto v0.48.0 // indirect
|
||||
golang.org/x/sys v0.41.0 // indirect
|
||||
)
|
||||
|
||||
+22
-2
@@ -4,19 +4,39 @@ github.com/bwmarrin/discordgo v0.29.0 h1:FmWeXFaKUwrcL3Cx65c20bTRW+vOb6k8AnaP+Eg
|
||||
github.com/bwmarrin/discordgo v0.29.0/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
|
||||
github.com/cloudflare/circl v1.6.1 h1:zqIqSPIndyBh1bjLVVDHMPpVKqp8Su/V+6MeDzzQBQ0=
|
||||
github.com/cloudflare/circl v1.6.1/go.mod h1:uddAzsPgqdMAYatqJ0lsjX1oECcQLIlRpzZh3pJrofs=
|
||||
github.com/cloudflare/circl v1.6.3 h1:9GPOhQGF9MCYUeXyMYlqTR6a5gTrgR/fBLXvUgtVcg8=
|
||||
github.com/cloudflare/circl v1.6.3/go.mod h1:2eXP6Qfat4O/Yhh8BznvKnJ+uzEoTQ6jVKJRn81BiS4=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dubyatp/go-ytdlp v0.0.0-20260213041320-010f95f0f1d3 h1:tGzvzV6fSzybhIpwU+wjvjOSq8RBNtHENm+G03E+mgo=
|
||||
github.com/dubyatp/go-ytdlp v0.0.0-20260213041320-010f95f0f1d3/go.mod h1:38IL64XM6gULrWtKTiR0+TTNCVbxesNSbTyaFG2CGTI=
|
||||
github.com/dubyatp/go-ytdlp v0.0.0-20260308044557-db32b29c1590 h1:27d1UwjlfuF/kwHj98B6UP30D1d7mvk/bmnDQ0xKX1s=
|
||||
github.com/dubyatp/go-ytdlp v0.0.0-20260308044557-db32b29c1590/go.mod h1:VgjnTrvkTf+23JuySjyPq1iQ8ijSovBtTPpXH5XrLtI=
|
||||
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
|
||||
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/lrstanley/go-ytdlp v1.2.7 h1:YNDvKkd0OCJSZLZePZvJwcirBCfL8Yw3eCwrTCE5w7Q=
|
||||
github.com/lrstanley/go-ytdlp v1.2.7/go.mod h1:38IL64XM6gULrWtKTiR0+TTNCVbxesNSbTyaFG2CGTI=
|
||||
github.com/lrstanley/go-ytdlp v1.3.3 h1:Y9kJcdTwskPWDiwONMIl501Dhi+OrTF7HHY6J6+Lbco=
|
||||
github.com/lrstanley/go-ytdlp v1.3.3/go.mod h1:VgjnTrvkTf+23JuySjyPq1iQ8ijSovBtTPpXH5XrLtI=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||
github.com/ulikunitz/xz v0.5.13 h1:ar98gWrjf4H1ev05fYP/o29PDZw9DrI3niHtnEqyuXA=
|
||||
github.com/ulikunitz/xz v0.5.13/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
|
||||
github.com/ulikunitz/xz v0.5.15 h1:9DNdB5s+SgV3bQ2ApL10xRc35ck0DuIX/isZvIk+ubY=
|
||||
github.com/ulikunitz/xz v0.5.15/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
|
||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
|
||||
golang.org/x/crypto v0.41.0 h1:WKYxWedPGCTVVl5+WHSSrOBT0O8lx32+zxmHxijgXp4=
|
||||
golang.org/x/crypto v0.41.0/go.mod h1:pO5AFd7FA68rFak7rOAGVuygIISepHftHnr8dr6+sUc=
|
||||
golang.org/x/crypto v0.48.0 h1:/VRzVqiRSggnhY7gNRxPauEQ5Drw9haKdM0jqfcCFts=
|
||||
golang.org/x/crypto v0.48.0/go.mod h1:r0kV5h3qnFPlQnBSrULhlsRfryS2pmewsg+XfMgkVos=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI=
|
||||
golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||
golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k=
|
||||
golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
+445
-10
@@ -1,23 +1,29 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"sort"
|
||||
"syscall"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
out_dir := os.Getenv("OUT_PATH")
|
||||
temp_dir := os.Getenv("TEMP_PATH")
|
||||
bot_token := os.Getenv("DISCORD_TOKEN")
|
||||
|
||||
if out_dir == "" {
|
||||
panic("No output dir specified")
|
||||
}
|
||||
|
||||
if temp_dir == "" {
|
||||
panic("No temp dir specified")
|
||||
}
|
||||
|
||||
s, err := discordgo.New("Bot " + bot_token)
|
||||
if err != nil {
|
||||
log.Fatalf("Invalid bot parameters: %v", err)
|
||||
@@ -46,6 +52,220 @@ func main() {
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "version",
|
||||
Description: "Show application version",
|
||||
DefaultMemberPermissions: &defaultMemberPermissions,
|
||||
Contexts: &[]discordgo.InteractionContextType{interactionPrivateChannel},
|
||||
},
|
||||
{
|
||||
Name: "download video",
|
||||
Type: discordgo.MessageApplicationCommand,
|
||||
IntegrationTypes: &[]discordgo.ApplicationIntegrationType{
|
||||
discordgo.ApplicationIntegrationUserInstall,
|
||||
},
|
||||
|
||||
Contexts: &[]discordgo.InteractionContextType{
|
||||
discordgo.InteractionContextBotDM,
|
||||
discordgo.InteractionContextPrivateChannel,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
var componentHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
|
||||
"video_select": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
// Get interaction state with fallback search
|
||||
var state *InteractionState
|
||||
|
||||
// First try current token
|
||||
state = getInteractionState(i.Interaction.Token)
|
||||
|
||||
// If not found, try to find state associated with this interaction context
|
||||
if state == nil {
|
||||
interactionStatesMutex.RLock()
|
||||
for token, s := range interactionStates {
|
||||
if s != nil && s.FormatOptions != nil {
|
||||
// Found a state, transfer it to current token
|
||||
state = s
|
||||
// Copy to current token and clean up old one
|
||||
interactionStatesMutex.RUnlock()
|
||||
setInteractionState(i.Interaction.Token, state)
|
||||
deleteInteractionState(token)
|
||||
break
|
||||
}
|
||||
}
|
||||
if state == nil {
|
||||
interactionStatesMutex.RUnlock()
|
||||
}
|
||||
}
|
||||
if state == nil || state.FormatOptions == nil {
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: "Error: Session expired. Please start over with /download.",
|
||||
Flags: discordgo.MessageFlagsEphemeral,
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Get requester user ID
|
||||
state.Requester = i.User.ID
|
||||
|
||||
// Get selected video format
|
||||
selectedValues := i.MessageComponentData().Values
|
||||
if len(selectedValues) == 0 {
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: "Error: No video format selected",
|
||||
Flags: discordgo.MessageFlagsEphemeral,
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Store selected video format ID and name
|
||||
state.VideoFormatID = selectedValues[0]
|
||||
for _, vOpt := range state.FormatOptions.VideoOptions {
|
||||
if vOpt.FormatID == selectedValues[0] {
|
||||
label := fmt.Sprintf("%s (%s", vOpt.Resolution, vOpt.Ext)
|
||||
if vOpt.TBR != nil {
|
||||
label += fmt.Sprintf(", %.0fkbps", *vOpt.TBR)
|
||||
}
|
||||
label += ")"
|
||||
state.VideoFormatName = label
|
||||
break
|
||||
}
|
||||
}
|
||||
setInteractionState(i.Interaction.Token, state)
|
||||
|
||||
// Build audio format options
|
||||
audioMenuOptions := buildAudioMenuOptions(state.FormatOptions.AudioOptions)
|
||||
|
||||
// Build video format options (to keep them visible but disabled)
|
||||
videoMenuOptions := buildVideoMenuOptions(state.FormatOptions.VideoOptions)
|
||||
|
||||
// Update components - disable video select, enable audio select
|
||||
updatedComponents := []discordgo.MessageComponent{
|
||||
discordgo.ActionsRow{
|
||||
Components: []discordgo.MessageComponent{
|
||||
discordgo.SelectMenu{
|
||||
CustomID: "video_select",
|
||||
Placeholder: "Choose a video format...",
|
||||
MaxValues: 1,
|
||||
Disabled: true,
|
||||
Options: videoMenuOptions,
|
||||
},
|
||||
},
|
||||
},
|
||||
discordgo.ActionsRow{
|
||||
Components: []discordgo.MessageComponent{
|
||||
discordgo.SelectMenu{
|
||||
CustomID: "audio_select",
|
||||
Placeholder: "Choose an audio format...",
|
||||
MaxValues: 1,
|
||||
Disabled: false,
|
||||
Options: audioMenuOptions,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseUpdateMessage,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: "Now select an audio format:",
|
||||
Components: updatedComponents,
|
||||
Flags: discordgo.MessageFlagsEphemeral,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("Error: %v", err)
|
||||
}
|
||||
},
|
||||
"audio_select": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
// Get URL from interaction state with fallback search
|
||||
var state *InteractionState
|
||||
|
||||
// First try current token
|
||||
state = getInteractionState(i.Interaction.Token)
|
||||
|
||||
// If not found, try to find state associated with this interaction context
|
||||
if state == nil {
|
||||
interactionStatesMutex.RLock()
|
||||
for token, s := range interactionStates {
|
||||
if s != nil && s.URL != "" {
|
||||
// Found a state, transfer it to current token
|
||||
state = s
|
||||
// Copy to current token and clean up old one
|
||||
interactionStatesMutex.RUnlock()
|
||||
setInteractionState(i.Interaction.Token, state)
|
||||
deleteInteractionState(token)
|
||||
break
|
||||
}
|
||||
}
|
||||
if state == nil {
|
||||
interactionStatesMutex.RUnlock()
|
||||
}
|
||||
}
|
||||
if state == nil {
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseUpdateMessage,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: "Error: No video URL found. Please start over with /download.",
|
||||
Flags: discordgo.MessageFlagsEphemeral,
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Get selected audio format
|
||||
selectedValues := i.MessageComponentData().Values
|
||||
audioFormatID := ""
|
||||
if len(selectedValues) > 0 {
|
||||
audioFormatID = selectedValues[0]
|
||||
}
|
||||
|
||||
// Store selected audio format ID and name
|
||||
state.AudioFormatID = audioFormatID
|
||||
for _, aOpt := range state.FormatOptions.AudioOptions {
|
||||
if aOpt.FormatID == audioFormatID {
|
||||
label := aOpt.Format
|
||||
if aOpt.Language != nil {
|
||||
label += fmt.Sprintf(" [%s]", *aOpt.Language)
|
||||
}
|
||||
if aOpt.TBR != nil {
|
||||
label += fmt.Sprintf(" (%.0fkbps)", *aOpt.TBR)
|
||||
}
|
||||
state.AudioFormatName = label
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
response := ""
|
||||
if state.URL != "" {
|
||||
// Respond immediately to prevent timeout
|
||||
response = fmt.Sprintf("%s **Starting download**", loading_emoji)
|
||||
|
||||
// Clean up state before responding
|
||||
deleteInteractionState(i.Interaction.Token)
|
||||
} else {
|
||||
response = "I don't see a video here :("
|
||||
}
|
||||
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseUpdateMessage,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: response,
|
||||
Flags: discordgo.MessageFlagsEphemeral,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("Error: %v", err)
|
||||
}
|
||||
if state.URL != "" {
|
||||
go startAsyncDownload(s, i, state.Requester, state.URL, state.VideoFormatID, state.VideoFormatName, state.AudioFormatID, state.AudioFormatName, out_dir, temp_dir)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
var commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
|
||||
@@ -55,16 +275,83 @@ func main() {
|
||||
for _, opt := range options {
|
||||
optionMap[opt.Name] = opt
|
||||
}
|
||||
response := ""
|
||||
|
||||
var url string
|
||||
if option, ok := optionMap["url"]; ok {
|
||||
response = "It works! Your URL is: " + option.StringValue()
|
||||
url = option.StringValue()
|
||||
} else {
|
||||
response = "It works!"
|
||||
}
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: response,
|
||||
Content: "Error: No URL provided",
|
||||
Flags: discordgo.MessageFlagsEphemeral,
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Send initial "fetching formats" response
|
||||
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: fmt.Sprintf("%s Fetching available formats...", loading_emoji),
|
||||
Flags: discordgo.MessageFlagsEphemeral,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("Error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Fetch formats asynchronously
|
||||
go fetchAndShowFormats(s, i, url)
|
||||
},
|
||||
"download video": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
data := i.ApplicationCommandData()
|
||||
targetMsg, ok := data.Resolved.Messages[data.TargetID]
|
||||
if !ok || targetMsg == nil {
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: "Error: Could not find the target message",
|
||||
Flags: discordgo.MessageFlagsEphemeral,
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
url := extractURLFromString(targetMsg.Content)
|
||||
if url == "" {
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: "Error: No URL provided",
|
||||
Flags: discordgo.MessageFlagsEphemeral,
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Send initial "fetching formats" response
|
||||
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: fmt.Sprintf("%s Fetching available formats...", loading_emoji),
|
||||
Flags: discordgo.MessageFlagsEphemeral,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("Error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Fetch formats asynchronously
|
||||
go fetchAndShowFormats(s, i, url)
|
||||
},
|
||||
"version": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: "[yt-dlp-bot](https://git.dubyatp.xyz/williamp/yt-dlp-bot) by dubyatp",
|
||||
Flags: discordgo.MessageFlagsEphemeral,
|
||||
},
|
||||
})
|
||||
@@ -72,11 +359,22 @@ func main() {
|
||||
}
|
||||
|
||||
s.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
switch i.Type {
|
||||
case discordgo.InteractionApplicationCommand:
|
||||
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
|
||||
h(s, i)
|
||||
}
|
||||
case discordgo.InteractionMessageComponent:
|
||||
if h, ok := componentHandlers[i.MessageComponentData().CustomID]; ok {
|
||||
h(s, i)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
log.Println("Initialize loading emoji")
|
||||
initLoadingEmoji(s)
|
||||
|
||||
|
||||
log.Println("Adding commands")
|
||||
registeredCommands := make([]*discordgo.ApplicationCommand, len(commands))
|
||||
for i, v := range commands {
|
||||
@@ -93,8 +391,145 @@ func main() {
|
||||
<-sc
|
||||
|
||||
s.Close()
|
||||
|
||||
//var url string = "https://www.youtube.com/watch?v=WpBWSFF03eI"
|
||||
|
||||
//downloadVideo(out_dir, url, DownloadOptions{EmbedThumbnail: true, IncludeSubtitles: true})
|
||||
}
|
||||
|
||||
func buildVideoMenuOptions(videoOptions []VideoOption) []discordgo.SelectMenuOption {
|
||||
opts := make([]discordgo.SelectMenuOption, 0, 25)
|
||||
for _, vOpt := range videoOptions {
|
||||
label := fmt.Sprintf("%s (%s", vOpt.Resolution, vOpt.Ext)
|
||||
if vOpt.TBR != nil {
|
||||
label += fmt.Sprintf(", %.0fkbps", *vOpt.TBR)
|
||||
}
|
||||
label += ")"
|
||||
if len(label) > 100 {
|
||||
label = label[:97] + "..."
|
||||
}
|
||||
opts = append(opts, discordgo.SelectMenuOption{
|
||||
Label: label,
|
||||
Value: vOpt.FormatID,
|
||||
})
|
||||
if len(opts) >= 25 {
|
||||
break
|
||||
}
|
||||
}
|
||||
return opts
|
||||
}
|
||||
|
||||
func buildAudioMenuOptions(audioOptions []AudioOption) []discordgo.SelectMenuOption {
|
||||
opts := make([]discordgo.SelectMenuOption, 0, 25)
|
||||
for _, aOpt := range audioOptions {
|
||||
label := aOpt.Format
|
||||
if aOpt.Language != nil {
|
||||
label += fmt.Sprintf(" [%s]", *aOpt.Language)
|
||||
}
|
||||
if aOpt.TBR != nil {
|
||||
label += fmt.Sprintf(" (%.0fkbps)", *aOpt.TBR)
|
||||
}
|
||||
if len(label) > 100 {
|
||||
label = label[:97] + "..."
|
||||
}
|
||||
opts = append(opts, discordgo.SelectMenuOption{
|
||||
Label: label,
|
||||
Value: aOpt.FormatID,
|
||||
})
|
||||
if len(opts) >= 25 {
|
||||
break
|
||||
}
|
||||
}
|
||||
return opts
|
||||
}
|
||||
|
||||
func fetchAndShowFormats(s *discordgo.Session, i *discordgo.InteractionCreate, url string) {
|
||||
formatOptions, err := GetFormats(url)
|
||||
if err != nil {
|
||||
_, err = s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
|
||||
Content: ptr("❌ Error fetching formats: " + err.Error()),
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("Error updating interaction: %v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Sort video formats: highest resolution first, then by bitrate
|
||||
sort.Slice(formatOptions.VideoOptions, func(x, y int) bool {
|
||||
heightX, heightY := 0, 0
|
||||
if formatOptions.VideoOptions[x].Height != nil {
|
||||
heightX = *formatOptions.VideoOptions[x].Height
|
||||
}
|
||||
if formatOptions.VideoOptions[y].Height != nil {
|
||||
heightY = *formatOptions.VideoOptions[y].Height
|
||||
}
|
||||
if heightX != heightY {
|
||||
return heightX > heightY
|
||||
}
|
||||
tbrX, tbrY := 0.0, 0.0
|
||||
if formatOptions.VideoOptions[x].TBR != nil {
|
||||
tbrX = *formatOptions.VideoOptions[x].TBR
|
||||
}
|
||||
if formatOptions.VideoOptions[y].TBR != nil {
|
||||
tbrY = *formatOptions.VideoOptions[y].TBR
|
||||
}
|
||||
return tbrX > tbrY
|
||||
})
|
||||
|
||||
// Sort audio formats: highest bitrate first
|
||||
sort.Slice(formatOptions.AudioOptions, func(x, y int) bool {
|
||||
tbrX, tbrY := 0.0, 0.0
|
||||
if formatOptions.AudioOptions[x].TBR != nil {
|
||||
tbrX = *formatOptions.AudioOptions[x].TBR
|
||||
}
|
||||
if formatOptions.AudioOptions[y].TBR != nil {
|
||||
tbrY = *formatOptions.AudioOptions[y].TBR
|
||||
}
|
||||
return tbrX > tbrY
|
||||
})
|
||||
|
||||
videoMenuOptions := buildVideoMenuOptions(formatOptions.VideoOptions)
|
||||
audioMenuOptions := buildAudioMenuOptions(formatOptions.AudioOptions)
|
||||
|
||||
if len(videoMenuOptions) == 0 || len(audioMenuOptions) == 0 {
|
||||
_, err = s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
|
||||
Content: ptr("❌ No separate video/audio streams found for this URL. The source may only provide combined formats."),
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("Error updating interaction: %v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
setInteractionState(i.Interaction.Token, &InteractionState{
|
||||
URL: url,
|
||||
FormatOptions: formatOptions,
|
||||
})
|
||||
|
||||
_, err = s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
|
||||
Content: ptr("Select a video format:"),
|
||||
Components: &[]discordgo.MessageComponent{
|
||||
discordgo.ActionsRow{
|
||||
Components: []discordgo.MessageComponent{
|
||||
discordgo.SelectMenu{
|
||||
CustomID: "video_select",
|
||||
Placeholder: "Choose a video format...",
|
||||
MaxValues: 1,
|
||||
Options: videoMenuOptions,
|
||||
},
|
||||
},
|
||||
},
|
||||
discordgo.ActionsRow{
|
||||
Components: []discordgo.MessageComponent{
|
||||
discordgo.SelectMenu{
|
||||
CustomID: "audio_select",
|
||||
Placeholder: "Choose an audio format...",
|
||||
MaxValues: 1,
|
||||
Disabled: true,
|
||||
Options: audioMenuOptions,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("Error updating interaction: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"regexp"
|
||||
_ "embed"
|
||||
"encoding/base64"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
var loading_emoji string
|
||||
|
||||
//go:embed assets/loading.webp
|
||||
var rawLoadingEmoji []byte
|
||||
|
||||
var loadingEmojiBase64 = func() string {
|
||||
s := "data:image/webp;base64," + base64.StdEncoding.EncodeToString(rawLoadingEmoji)
|
||||
rawLoadingEmoji = nil
|
||||
return s
|
||||
}()
|
||||
|
||||
func initLoadingEmoji(s *discordgo.Session) {
|
||||
emojis, err := s.ApplicationEmojis(s.State.User.ID)
|
||||
if err != nil {
|
||||
log.Panic("Cannot get emojis")
|
||||
}
|
||||
for _, e := range emojis {
|
||||
if e.Name == "loading" {
|
||||
loading_emoji = fmt.Sprintf("<a:%s:%s>", e.Name, e.ID)
|
||||
return
|
||||
}
|
||||
}
|
||||
e, err := s.ApplicationEmojiCreate(s.State.User.ID, &discordgo.EmojiParams{
|
||||
Name: "loading",
|
||||
Image: loadingEmojiBase64,
|
||||
})
|
||||
if err != nil {
|
||||
log.Panicf("Cannot create loading emoji: %s", err)
|
||||
}
|
||||
loading_emoji = fmt.Sprintf("<a:%s:%s>", e.Name, e.ID)
|
||||
}
|
||||
|
||||
var urlPattern = regexp.MustCompile(`https?://\S+`)
|
||||
|
||||
func extractURLFromString(in_url string) string {
|
||||
return string(urlPattern.Find([]byte(in_url)))
|
||||
}
|
||||
|
||||
// Helper function to create string pointer
|
||||
func ptr(s string) *string {
|
||||
return &s
|
||||
}
|
||||
|
||||
func formatBytes(b int) string {
|
||||
switch {
|
||||
case b >= 1<<30:
|
||||
return fmt.Sprintf("%.1f GB", float64(b)/float64(1<<30))
|
||||
case b >= 1<<20:
|
||||
return fmt.Sprintf("%.1f MB", float64(b)/float64(1<<20))
|
||||
case b >= 1<<10:
|
||||
return fmt.Sprintf("%.1f KB", float64(b)/float64(1<<10))
|
||||
default:
|
||||
return fmt.Sprintf("%d B", b)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package main
|
||||
|
||||
import "sync"
|
||||
|
||||
// Global state management
|
||||
var (
|
||||
interactionStates = make(map[string]*InteractionState)
|
||||
interactionStatesMutex = sync.RWMutex{}
|
||||
)
|
||||
|
||||
func getInteractionState(token string) *InteractionState {
|
||||
interactionStatesMutex.RLock()
|
||||
defer interactionStatesMutex.RUnlock()
|
||||
return interactionStates[token]
|
||||
}
|
||||
|
||||
func setInteractionState(token string, state *InteractionState) {
|
||||
interactionStatesMutex.Lock()
|
||||
defer interactionStatesMutex.Unlock()
|
||||
interactionStates[token] = state
|
||||
}
|
||||
|
||||
func deleteInteractionState(token string) {
|
||||
interactionStatesMutex.Lock()
|
||||
defer interactionStatesMutex.Unlock()
|
||||
delete(interactionStates, token)
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package main
|
||||
|
||||
type DownloadOptions struct {
|
||||
EmbedThumbnail bool
|
||||
IncludeSubtitles bool
|
||||
VideoFormatID string
|
||||
AudioFormatID string
|
||||
}
|
||||
|
||||
type VideoOption struct {
|
||||
Height *int `json:"height,omitempty"`
|
||||
Resolution string `json:"resolution,omitempty"`
|
||||
FormatID string `json:"format_id"`
|
||||
Ext string `json:"ext"`
|
||||
TBR *float64 `json:"tbr,omitempty"`
|
||||
}
|
||||
|
||||
type AudioOption struct {
|
||||
Format string `json:"format"`
|
||||
FormatID string `json:"format_id"`
|
||||
Ext string `json:"ext"`
|
||||
TBR *float64 `json:"tbr,omitempty"`
|
||||
Language *string `json:"language,omitempty"`
|
||||
}
|
||||
|
||||
type FormatOptions struct {
|
||||
VideoOptions []VideoOption `json:"video_options"`
|
||||
AudioOptions []AudioOption `json:"audio_options"`
|
||||
}
|
||||
|
||||
type ProgressUpdate struct {
|
||||
Phase string
|
||||
DownloadedBytes int
|
||||
Filename string
|
||||
}
|
||||
|
||||
// InteractionState holds the state for a specific interaction
|
||||
type InteractionState struct {
|
||||
Requester string
|
||||
URL string
|
||||
FormatOptions *FormatOptions
|
||||
VideoFormatID string
|
||||
VideoFormatName string
|
||||
AudioFormatID string
|
||||
AudioFormatName string
|
||||
}
|
||||
|
||||
// DownloadResult represents the result of an async download operation
|
||||
type DownloadResult struct {
|
||||
Success bool
|
||||
Message string
|
||||
URL string
|
||||
Format string
|
||||
Error error
|
||||
}
|
||||
+171
-15
@@ -2,33 +2,189 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/lrstanley/go-ytdlp"
|
||||
)
|
||||
|
||||
type DownloadOptions struct {
|
||||
EmbedThumbnail bool
|
||||
IncludeSubtitles bool
|
||||
var ytdlpBinary = os.Getenv("YTDLP_BIN")
|
||||
|
||||
func GetFormats(url string) (*FormatOptions, error) {
|
||||
dl := ytdlp.New().
|
||||
SetExecutable(ytdlpBinary).
|
||||
SkipDownload().
|
||||
DumpJSON()
|
||||
|
||||
result, err := dl.Run(context.TODO(), url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Parse the JSON output
|
||||
var info struct {
|
||||
Formats []struct {
|
||||
VCodec *string `json:"vcodec"`
|
||||
ACodec *string `json:"acodec"`
|
||||
NeedsTesting *bool `json:"__needs_testing"`
|
||||
Height *int `json:"height"`
|
||||
Resolution string `json:"resolution"`
|
||||
FormatID string `json:"format_id"`
|
||||
Format string `json:"format"`
|
||||
Ext string `json:"ext"`
|
||||
TBR *float64 `json:"tbr"`
|
||||
Language *string `json:"language"`
|
||||
LanguagePref *int `json:"language_preference"`
|
||||
URL *string `json:"url"`
|
||||
Protocol *string `json:"protocol"`
|
||||
} `json:"formats"`
|
||||
}
|
||||
|
||||
if err := json.Unmarshal([]byte(result.Stdout), &info); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
formatOpts := &FormatOptions{
|
||||
VideoOptions: []VideoOption{},
|
||||
AudioOptions: []AudioOption{},
|
||||
}
|
||||
|
||||
for _, fmt := range info.Formats {
|
||||
// Skip formats that need testing
|
||||
if fmt.NeedsTesting != nil && *fmt.NeedsTesting {
|
||||
continue
|
||||
}
|
||||
|
||||
// Skip SABR formats (https://github.com/yt-dlp/yt-dlp/issues/12482)
|
||||
if fmt.URL == nil || *fmt.URL == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
// Video-only: has video codec but no audio codec
|
||||
if fmt.VCodec != nil && *fmt.VCodec != "none" &&
|
||||
fmt.ACodec != nil && *fmt.ACodec == "none" {
|
||||
formatOpts.VideoOptions = append(formatOpts.VideoOptions, VideoOption{
|
||||
Height: fmt.Height,
|
||||
Resolution: fmt.Resolution,
|
||||
FormatID: fmt.FormatID,
|
||||
Ext: fmt.Ext,
|
||||
TBR: fmt.TBR,
|
||||
})
|
||||
}
|
||||
|
||||
// Audio-only: has audio codec but no video codec
|
||||
if fmt.ACodec != nil && *fmt.ACodec != "none" &&
|
||||
fmt.VCodec != nil && *fmt.VCodec == "none" {
|
||||
audioOpt := AudioOption{
|
||||
Format: fmt.Format,
|
||||
FormatID: fmt.FormatID,
|
||||
Ext: fmt.Ext,
|
||||
TBR: fmt.TBR,
|
||||
}
|
||||
|
||||
// Use language if available, otherwise use language_preference
|
||||
if fmt.Language != nil {
|
||||
audioOpt.Language = fmt.Language
|
||||
}
|
||||
|
||||
formatOpts.AudioOptions = append(formatOpts.AudioOptions, audioOpt)
|
||||
}
|
||||
}
|
||||
|
||||
return formatOpts, nil
|
||||
}
|
||||
|
||||
func downloadVideo(out_dir, url string, opts DownloadOptions) {
|
||||
func dirSize(path string) int {
|
||||
var total int64
|
||||
filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
|
||||
if err == nil && !info.IsDir() {
|
||||
total += info.Size()
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return int(total)
|
||||
}
|
||||
|
||||
func DownloadVideo(out_dir, temp_dir, url string, opts DownloadOptions, progressChan chan<- ProgressUpdate) {
|
||||
var mu sync.Mutex
|
||||
currentPhase := "downloading"
|
||||
currentFilename := ""
|
||||
|
||||
// Poll the temp directory for actual bytes-on-disk progress.
|
||||
// The yt-dlp progress callback only tracks phase/filename since
|
||||
// DownloadedBytes from the callback is unreliable for DASH streams.
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(1)
|
||||
stopPoll := make(chan struct{})
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
ticker := time.NewTicker(500 * time.Millisecond)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-stopPoll:
|
||||
return
|
||||
case <-ticker.C:
|
||||
mu.Lock()
|
||||
phase := currentPhase
|
||||
filename := currentFilename
|
||||
mu.Unlock()
|
||||
progressChan <- ProgressUpdate{
|
||||
Phase: phase,
|
||||
DownloadedBytes: dirSize(temp_dir),
|
||||
Filename: filename,
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
defer func() {
|
||||
close(stopPoll)
|
||||
wg.Wait()
|
||||
close(progressChan)
|
||||
}()
|
||||
|
||||
homePath := "home:" + out_dir
|
||||
tempPath := "temp:" + temp_dir
|
||||
|
||||
dl := ytdlp.New().
|
||||
SetWorkDir(out_dir).
|
||||
FormatSort("res,ext:mp4:m4a").
|
||||
RecodeVideo("mp4").
|
||||
SetExecutable(ytdlpBinary).
|
||||
Paths(homePath).
|
||||
Paths(tempPath).
|
||||
ProgressFunc(100*time.Millisecond, func(prog ytdlp.ProgressUpdate) {
|
||||
fmt.Printf(
|
||||
"%s @ %s [eta: %s] :: %s\n",
|
||||
prog.Status,
|
||||
prog.PercentString(),
|
||||
prog.ETA(),
|
||||
prog.Filename,
|
||||
)
|
||||
if prog.Status == ytdlp.ProgressStatusFinished ||
|
||||
prog.Status == ytdlp.ProgressStatusStarting ||
|
||||
prog.Status == ytdlp.ProgressStatusError {
|
||||
return
|
||||
}
|
||||
|
||||
phase := "downloading"
|
||||
if prog.Status == ytdlp.ProgressStatusPostProcessing {
|
||||
phase = "post-processing"
|
||||
}
|
||||
|
||||
mu.Lock()
|
||||
currentPhase = phase
|
||||
currentFilename = prog.Filename
|
||||
mu.Unlock()
|
||||
}).
|
||||
Output("%(title)s.%(ext)s")
|
||||
|
||||
// Set format if both video and audio format IDs are provided
|
||||
if opts.VideoFormatID != "" && opts.AudioFormatID != "" {
|
||||
dl = dl.Format(opts.VideoFormatID + "+" + opts.AudioFormatID)
|
||||
} else if opts.VideoFormatID != "" {
|
||||
dl = dl.Format(opts.VideoFormatID)
|
||||
} else if opts.AudioFormatID != "" {
|
||||
dl = dl.Format(opts.AudioFormatID)
|
||||
} else {
|
||||
// Default format selection if none specified
|
||||
dl = dl.FormatSort("res,ext:mp4:m4a")
|
||||
}
|
||||
|
||||
if opts.EmbedThumbnail {
|
||||
dl = dl.EmbedThumbnail()
|
||||
}
|
||||
|
||||
Generated
+3
-3
@@ -2,11 +2,11 @@
|
||||
"nodes": {
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1768875095,
|
||||
"narHash": "sha256-dYP3DjiL7oIiiq3H65tGIXXIT1Waiadmv93JS0sS+8A=",
|
||||
"lastModified": 1772674223,
|
||||
"narHash": "sha256-/suKbHSaSmuC9UY7G0VRQ3aO+QKqxAQPQ19wG7QNkF8=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "ed142ab1b3a092c4d149245d0c4126a5d7ea00b0",
|
||||
"rev": "66d9241e3dc2296726dc522e62dbfe89c7b449f3",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
|
||||
@@ -36,6 +36,18 @@
|
||||
source .env
|
||||
set +a
|
||||
fi
|
||||
|
||||
if [ ! -d .venv ]; then
|
||||
echo "Creating Python virtual environment in .venv"
|
||||
python3 -m venv .venv
|
||||
fi
|
||||
|
||||
if compgen -G "app/*.py" > /dev/null; then
|
||||
.venv/bin/pip install -r ./app/requirements.txt
|
||||
source .venv/bin/activate
|
||||
fi
|
||||
|
||||
export YTDLP_BIN=${pkgs.lib.getExe pkgs.yt-dlp}
|
||||
'';
|
||||
};
|
||||
});
|
||||
|
||||
+16
-1
@@ -1,3 +1,18 @@
|
||||
{
|
||||
"$schema": "https://docs.renovatebot.com/renovate-schema.json"
|
||||
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
|
||||
"customManagers": [
|
||||
{
|
||||
"description": "Update yt-dlp version in Dockerfiles",
|
||||
"customType": "regex",
|
||||
"managerFilePatterns": [
|
||||
"/^Dockerfile$/"
|
||||
],
|
||||
"matchStrings": [
|
||||
"ARG YT_DLP_VERSION=\"(?<currentValue>.*?)\""
|
||||
],
|
||||
"datasourceTemplate": "github-tags",
|
||||
"depNameTemplate": "yt-dlp/yt-dlp",
|
||||
"versioningTemplate": "regex:^(?<major>\\d{4})\\.(?<minor>\\d{2})\\.(?<patch>\\d{2})$"
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user