Skip to content

Commit

Permalink
Fix some stuff, support instagram, add publish flow
Browse files Browse the repository at this point in the history
  • Loading branch information
sumboid committed Apr 20, 2024
1 parent 878856b commit 144a3d5
Show file tree
Hide file tree
Showing 13 changed files with 327 additions and 254 deletions.
31 changes: 15 additions & 16 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
name: Build

name: build
on:
push:
branches: [ master ]
branches:
- master
pull_request:
branches: [ master ]
branches:
- master

jobs:

build:
name: Build Shortique
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
permissions:
contents: read

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.17

- name: Build
run: go build -v ./...
steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Test
run: go test -v ./...
- name: Build and push
uses: docker/build-push-action@v4
with:
push: false
33 changes: 33 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: publish
on:
push:
tags:
- "v[0-9]+.[0-9]+.[0-9]+"

env:
REGISTRY: ghcr.io

jobs:
publish:
name: Publish Shortique
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Log in to the Container registry
uses: docker/[email protected]
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push
uses: docker/build-push-action@v4
with:
push: true
tags: ghcr.io/hedlx/shortique:latest,ghcr.io/hedlx/shortique:${{ github.ref_name }}
7 changes: 4 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ WORKDIR /app

COPY go.mod ./
COPY go.sum ./
COPY *.go ./
RUN go mod download
COPY *.go ./
RUN go build -o /shque


FROM alpine
FROM alpine:3

RUN apk add yt-dlp
RUN apk add --no-cache yt-dlp
WORKDIR /
COPY --from=build /shque /shque

ENTRYPOINT ["/shque"]

23 changes: 18 additions & 5 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ type request struct {
}

type result struct {
req request
err error
req request
}

func UniqArtists(artists []string) []string {
Expand Down Expand Up @@ -52,8 +52,17 @@ func ExtractAudioInfo(meta *VideoMeta) (string, string) {
}

func ProxyVideo(bot *tg.BotAPI, req request, done chan<- result) {
url := MakeYoutubeURL(req.Data.VideoID)
metadata, err := GetMetadata(url)
url, err := BuildURL(req.Data.Origin, req.Data.VideoID)
if err != nil {
done <- result{
req: req,
err: err,
}

return
}

metadata, err := GetMetadata(url, req.Data.Origin)
if err != nil {
done <- result{
req: req,
Expand All @@ -63,7 +72,7 @@ func ProxyVideo(bot *tg.BotAPI, req request, done chan<- result) {
return
}

videoReader, waitC, downloadC := DownloadVideoRoutine(VideoSource{url, req.Data.Type})
videoReader, waitC, downloadC := DownloadVideoRoutine(VideoSource{url, req.Data.Type}, req.Data.Origin)
def := tg.NewMessage(req.Data.ChatID, "This should never happen!")
def.ReplyToMessageID = req.Data.MessageID

Expand Down Expand Up @@ -104,8 +113,8 @@ func ProxyVideo(bot *tg.BotAPI, req request, done chan<- result) {
}

func RunBot(token string) error {
L.Info("Starting the bot...")
bot, err := tg.NewBotAPI(token)

if err != nil {
return err
}
Expand Down Expand Up @@ -146,10 +155,13 @@ func RunBot(token string) error {
handleRequest(req)
}

L.Info("Bot is online!")
for {
select {
case update := <-updatesC:
L.Info("New message")
if update.Message == nil {
L.Info("But no message")
break
}

Expand All @@ -160,6 +172,7 @@ func RunBot(token string) error {
MessageID: update.Message.MessageID,
VideoID: src.Id,
Type: src.Type,
Origin: src.Origin,
})
}
case result := <-resultsC:
Expand Down
4 changes: 1 addition & 3 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '3.6'

services:
shortique:
build: .
Expand All @@ -17,4 +15,4 @@ services:
- redis_data:/data

volumes:
redis_data:
redis_data:
82 changes: 73 additions & 9 deletions download.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,22 @@ import (
"bytes"
"fmt"
"io"
"net/http"
"os/exec"
"strings"

"go.uber.org/zap"
)

func GetMetadata(url string) (*VideoMeta, error) {
cmd := exec.Command("yt-dlp", url, "-s", "--print-json")
func GetMetadata(url string, origin string) (*VideoMeta, error) {
if origin != "youtube" {
return &VideoMeta{}, nil
}

L.Info("Getting metadata", zap.String("url", url))
params := []string{url, "-s", "--print-json"}

cmd := exec.Command("yt-dlp", params...)
var output bytes.Buffer
cmd.Stdout = &output

Expand All @@ -24,15 +35,68 @@ type VideoSource struct {
Type int
}

func DownloadVideo(src VideoSource, waitC <-chan struct{}, doneC chan<- error) io.Reader {
cmdParams := []string{src.Url, "-o", "-", "--no-part", "--max-filesize", "50M", "-f", "[filesize<50M]"}
if src.Type == MusicType {
type DownloadInput struct {
Origin string
Src VideoSource
}

type downloader = func(in DownloadInput, waitC <-chan struct{}, doneC chan<- error) io.Reader

var downloadStrategy = map[string]downloader{
"youtube": downloadYT,
"instagram": downloadInsta,
}

func DownloadVideo(in DownloadInput, waitC <-chan struct{}, doneC chan<- error) io.Reader {
download := downloadStrategy[in.Origin]
if download == nil {
doneC <- fmt.Errorf("unknown origin: %s", in.Origin)
return bytes.NewReader(nil)
}

return download(in, waitC, doneC)
}

func downloadInsta(in DownloadInput, waitC <-chan struct{}, doneC chan<- error) io.Reader {
parts := strings.Split(in.Src.Url, "/")
if len(parts) == 0 {
doneC <- fmt.Errorf("invalid URL: %s", in.Src.Url)
return bytes.NewReader(nil)
}
id := parts[len(parts)-1]

resp, err := http.Get(fmt.Sprintf("https://www.ddinstagram.com/videos/%s/1", id))
if err != nil {
doneC <- err
return bytes.NewReader(nil)
}

if resp.StatusCode > 299 || resp.StatusCode < 200 {
resp.Body.Close()
doneC <- fmt.Errorf("bad response: %d", resp.StatusCode)
return bytes.NewReader(nil)
}

go func() {
defer resp.Body.Close()
<-waitC
}()

return resp.Body
}

func downloadYT(in DownloadInput, waitC <-chan struct{}, doneC chan<- error) io.Reader {
L.Info("Starting download video")
cmdParams := []string{in.Src.Url, "-o", "-", "--no-part"}
cmdParams = append(cmdParams, "--max-filesize", "50M", "-f", "[filesize_approx<50M]")

if in.Src.Type == MusicType {
cmdParams = append(cmdParams, "-f", "ba", "--audio-format", "mp3")
}

L.Debug("Executing command", zap.String("cmd", strings.Join(append([]string{"yt-dlp"}, cmdParams...), " ")))
cmd := exec.Command("yt-dlp", cmdParams...)
out, err := cmd.StdoutPipe()

if err != nil {
doneC <- err
return nil
Expand All @@ -59,8 +123,8 @@ func DownloadVideo(src VideoSource, waitC <-chan struct{}, doneC chan<- error) i
return out
}

func DownloadVideoRoutine(src VideoSource) (io.Reader, chan<- struct{}, <-chan error) {
doneC := make(chan error)
func DownloadVideoRoutine(src VideoSource, origin string) (io.Reader, chan<- struct{}, <-chan error) {
doneC := make(chan error, 1)
waitC := make(chan struct{})
return DownloadVideo(src, waitC, doneC), waitC, doneC
return DownloadVideo(DownloadInput{Src: src, Origin: origin}, waitC, doneC), waitC, doneC
}
17 changes: 9 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
module shortique
module github.com/hedlx/shortique

go 1.17
go 1.22

require (
github.com/go-redis/redis/v8 v8.11.4
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
github.com/joho/godotenv v1.4.0
github.com/joho/godotenv v1.5.1
github.com/redis/go-redis/v9 v9.5.1
github.com/samber/lo v1.39.0
github.com/satori/go.uuid v1.2.0
go.uber.org/zap v1.21.0
go.uber.org/zap v1.27.0
)

require (
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
)
Loading

0 comments on commit 144a3d5

Please sign in to comment.