Compare commits
No commits in common. "381bfb32b24179dc8198c5360f6f769b08372ff8" and "0d30bae2d8f31d25298eb7f85cbd86cea6e80775" have entirely different histories.
381bfb32b2
...
0d30bae2d8
@ -1,19 +0,0 @@
|
|||||||
name: Gitea Actions Demo
|
|
||||||
run-name: ${{ gitea.actor }} is testing out Gitea Actions 🚀
|
|
||||||
on: [push]
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
Explore-Gitea-Actions:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- run: echo "🎉 The job was automatically triggered by a ${{ gitea.event_name }} event."
|
|
||||||
- run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by Gitea!"
|
|
||||||
- run: echo "🔎 The name of your branch is ${{ gitea.ref }} and your repository is ${{ gitea.repository }}."
|
|
||||||
- name: Check out repository code
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
- run: echo "💡 The ${{ gitea.repository }} repository has been cloned to the runner."
|
|
||||||
- run: echo "🖥️ The workflow is now ready to test your code on the runner."
|
|
||||||
- name: List files in the repository
|
|
||||||
run: |
|
|
||||||
ls ${{ gitea.workspace }}
|
|
||||||
- run: echo "🍏 This job's status is ${{ job.status }}."
|
|
@ -1,47 +0,0 @@
|
|||||||
name: Container Build & Push
|
|
||||||
|
|
||||||
# add on pull requests too
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
container:
|
|
||||||
image: catthehacker/ubuntu:act-latest
|
|
||||||
env:
|
|
||||||
REGISTRY: www.gitgud.foo
|
|
||||||
OWNER: thegrind
|
|
||||||
TAG: 1.0.0
|
|
||||||
IMAGE_NAME: test
|
|
||||||
steps:
|
|
||||||
-
|
|
||||||
name: Check out code
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
- name: whereami
|
|
||||||
run: ls -lath
|
|
||||||
|
|
||||||
-
|
|
||||||
name: get dicks space
|
|
||||||
run: |
|
|
||||||
df -h
|
|
||||||
|
|
||||||
-
|
|
||||||
name: Log in to the GitGud container registry
|
|
||||||
run: |
|
|
||||||
/usr/bin/docker login $REGISTRY \
|
|
||||||
-u "${{ gitea.actor }}" \
|
|
||||||
-p "${{ secrets.REGISTRY_PASSWORD }}"
|
|
||||||
|
|
||||||
-
|
|
||||||
name: Build and push OCI image
|
|
||||||
run: |
|
|
||||||
IMAGE=$REGISTRY/$OWNER/$IMAGE_NAME:$TAG
|
|
||||||
docker build -t $IMAGE .
|
|
||||||
-
|
|
||||||
name: Push OCI image
|
|
||||||
run: |
|
|
||||||
IMAGE=$REGISTRY/$OWNER/$IMAGE_NAME:$TAG
|
|
||||||
docker push $IMAGE
|
|
@ -1,21 +0,0 @@
|
|||||||
name: Run Go Tests
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request:
|
|
||||||
branches: [master]
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
test:
|
|
||||||
name: Run Tests
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: Checkout Code
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: Set up Go
|
|
||||||
uses: actions/setup-go@v4
|
|
||||||
with:
|
|
||||||
go-version: '1.22'
|
|
||||||
|
|
||||||
- name: Run Tests
|
|
||||||
run: make test
|
|
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,10 +1,9 @@
|
|||||||
testfile.wav
|
testfile.wav
|
||||||
play.opus
|
play.opus
|
||||||
output.opus
|
output.opus
|
||||||
*.of
|
|
||||||
.env
|
.env
|
||||||
*.wav
|
*.wav
|
||||||
*.opus
|
*.opus
|
||||||
*.webm
|
*.webm
|
||||||
*.tar.gz
|
*.tar.gz
|
||||||
#*.old
|
*.old
|
@ -1,78 +0,0 @@
|
|||||||
# Use the official golang:1.22.12-bookworm image as the build stage
|
|
||||||
FROM golang:1.22.12-bookworm
|
|
||||||
|
|
||||||
# Create a directory /app
|
|
||||||
RUN mkdir /app
|
|
||||||
|
|
||||||
# Set the working directory to /app
|
|
||||||
WORKDIR /app
|
|
||||||
|
|
||||||
# Copy the current directory contents into /app
|
|
||||||
COPY ./ /app/
|
|
||||||
|
|
||||||
# Update the package list and install necessary packages
|
|
||||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
||||||
curl \
|
|
||||||
wget \
|
|
||||||
vim \
|
|
||||||
git \
|
|
||||||
debconf \
|
|
||||||
ca-certificates \
|
|
||||||
tar \
|
|
||||||
autoconf \
|
|
||||||
automake \
|
|
||||||
build-essential \
|
|
||||||
cmake \
|
|
||||||
git-core \
|
|
||||||
libass-dev \
|
|
||||||
libfreetype6-dev \
|
|
||||||
libgnutls28-dev \
|
|
||||||
libmp3lame-dev \
|
|
||||||
libsdl2-dev \
|
|
||||||
libtool \
|
|
||||||
libva-dev \
|
|
||||||
libvdpau-dev \
|
|
||||||
libvorbis-dev \
|
|
||||||
libxcb1-dev \
|
|
||||||
libxcb-shm0-dev \
|
|
||||||
libxcb-xfixes0-dev \
|
|
||||||
meson \
|
|
||||||
ninja-build \
|
|
||||||
pkg-config \
|
|
||||||
texinfo \
|
|
||||||
yasm \
|
|
||||||
zlib1g-dev \
|
|
||||||
libx264-dev \
|
|
||||||
libx265-dev \
|
|
||||||
libnuma-dev \
|
|
||||||
libvpx-dev \
|
|
||||||
libopus-dev \
|
|
||||||
libunistring-dev \
|
|
||||||
libaom-dev \
|
|
||||||
libdav1d-dev && \
|
|
||||||
# Download Go module dependencies
|
|
||||||
go mod download && \
|
|
||||||
# Download yt-dlp
|
|
||||||
wget --no-check-certificate -c https://github.com/yt-dlp/yt-dlp/releases/download/2025.01.26/yt-dlp_linux &&\
|
|
||||||
# Download ffmpeg
|
|
||||||
wget --no-check-certificate -c https://www.johnvansickle.com/ffmpeg/old-releases/ffmpeg-6.0.1-amd64-static.tar.xz &&\
|
|
||||||
# Move yt-dlp to /usr/bin and set permissions
|
|
||||||
mv yt-dlp_linux /usr/bin/yt-dlp && \
|
|
||||||
chmod 755 /usr/bin/yt-dlp && \
|
|
||||||
chmod +x /usr/bin/yt-dlp && \
|
|
||||||
# Extract ffmpeg tarball
|
|
||||||
tar -xf ffmpeg-6.0.1-amd64-static.tar.xz && \
|
|
||||||
# Remove the tarball
|
|
||||||
rm -rf ffmpeg-6.0.1-amd64-static.tar.xz && \
|
|
||||||
# Set execute permissions for ffmpeg and ffprobe
|
|
||||||
chmod +x ffmpeg-6.0.1-amd64-static/ffmpeg && \
|
|
||||||
chmod +x ffmpeg-6.0.1-amd64-static/ffprobe && \
|
|
||||||
# Copy ffmpeg and ffprobe to /usr/bin
|
|
||||||
cp -R ffmpeg-6.0.1-amd64-static/ffmpeg /usr/bin/ && \
|
|
||||||
cp -R ffmpeg-6.0.1-amd64-static/ffprobe /usr/bin/ && \
|
|
||||||
# Remove the extracted ffmpeg directory
|
|
||||||
rm -rf ffmpeg-6.0.1-amd64-static && \
|
|
||||||
# Create a new user 'papibot'
|
|
||||||
useradd papibot && \
|
|
||||||
# Build the Go application
|
|
||||||
CGO_ENABLED=0 GOOS=linux go build -o /app/papibot
|
|
@ -1,17 +0,0 @@
|
|||||||
FROM www.gitgud.foo/thegrind/papibot-builder:latest as build
|
|
||||||
|
|
||||||
# Create a group and user 'papibot'
|
|
||||||
RUN groupadd -r papibot && useradd -r -g papibot papibot
|
|
||||||
|
|
||||||
# Switch to the 'papibot' user
|
|
||||||
USER papibot
|
|
||||||
|
|
||||||
# Copy the necessary files from the build stage
|
|
||||||
COPY --from=build --chown=papibot:papibot /usr/bin/ /usr/bin
|
|
||||||
COPY --from=build --chown=papibot:papibot /app/ /app/
|
|
||||||
|
|
||||||
# Set the working directory to /app
|
|
||||||
WORKDIR /app/
|
|
||||||
|
|
||||||
# Set the entry point to the built Go application
|
|
||||||
ENTRYPOINT ["./papibot"]
|
|
3
Makefile
3
Makefile
@ -1,3 +0,0 @@
|
|||||||
test:
|
|
||||||
@echo "Running tests for main and all packages"
|
|
||||||
go test ./...
|
|
11
README.md
11
README.md
@ -11,7 +11,7 @@ docker build -t papibot . ;docker run -d papibot:latest --name papibot
|
|||||||
- `ffmpeg` make sure libopus is included
|
- `ffmpeg` make sure libopus is included
|
||||||
- `yt-dlp`
|
- `yt-dlp`
|
||||||
|
|
||||||
## Running locally
|
## Current testing steps
|
||||||
|
|
||||||
Copy .env.example to .env
|
Copy .env.example to .env
|
||||||
|
|
||||||
@ -20,12 +20,3 @@ Populate the discord bot keys
|
|||||||
`go run .`
|
`go run .`
|
||||||
|
|
||||||
type `!` in any channel in the discord while you're in a voice channel.
|
type `!` in any channel in the discord while you're in a voice channel.
|
||||||
|
|
||||||
## Build new builder container image
|
|
||||||
```
|
|
||||||
docker build -t www.gitgud.foo/thegrind/papibot-builder:<tag> Dockerfile.builder
|
|
||||||
```
|
|
||||||
|
|
||||||
## Running tests locally
|
|
||||||
|
|
||||||
`make test`
|
|
||||||
|
84
main.go
84
main.go
@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
@ -9,7 +10,6 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.thegrind.dev/thegrind/papibot/pkg/opusframes"
|
|
||||||
dg "github.com/bwmarrin/discordgo"
|
dg "github.com/bwmarrin/discordgo"
|
||||||
"github.com/javif89/dotenv"
|
"github.com/javif89/dotenv"
|
||||||
)
|
)
|
||||||
@ -108,28 +108,79 @@ func playCommand(s *dg.Session, i *dg.InteractionCreate) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func playOnVoiceChannel(voiceChannel *dg.VoiceConnection) {
|
func playOnVoiceChannel(voiceChannel *dg.VoiceConnection) {
|
||||||
log.Println("Decoding")
|
log.Println("Starting ffmpeg stream")
|
||||||
start := time.Now()
|
// I got the original implementation from:
|
||||||
frames, err := opusframes.Decode("vid.of")
|
// https://github.com/nhooyr/botatouille/blob/7e1cd9d5a8d517fd43fd11599b2a62bf832a5c96/cmd/botatouille/music/music.go#L62-L104
|
||||||
if err != nil {
|
// after hours of searching.
|
||||||
log.Println("Decoding error: ", err)
|
ffmpeg := exec.Command(
|
||||||
return
|
"ffmpeg",
|
||||||
}
|
"-i", "vid.webm",
|
||||||
duration := time.Since(start)
|
"-hide_banner",
|
||||||
log.Printf("Decoding took: %s", duration)
|
"-loglevel", "quiet",
|
||||||
|
"-f", "data",
|
||||||
|
"-map", "0:a",
|
||||||
|
"-ar", "48k",
|
||||||
|
"-ac", "2",
|
||||||
|
"-acodec", "libopus",
|
||||||
|
"-b:a", "128k",
|
||||||
|
"pipe:1")
|
||||||
|
|
||||||
// for _, f := range frames {
|
ffmpegOut, err := ffmpeg.StdoutPipe()
|
||||||
// log.Printf("Got frame. Size: %d", len(f))
|
if err != nil {
|
||||||
// }
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = ffmpeg.Start()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
packets := [][]byte{}
|
||||||
|
|
||||||
|
startTime := time.Now()
|
||||||
|
|
||||||
|
bytes_sent := 0
|
||||||
|
for {
|
||||||
|
// I read in the RFC that frames will not be bigger than this size
|
||||||
|
p := make([]byte, 960)
|
||||||
|
n, err := ffmpegOut.Read(p)
|
||||||
|
bytes_sent = bytes_sent + n
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Bytes: %d", n)
|
||||||
|
if err == io.EOF {
|
||||||
|
log.Println("Done streaming")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
packets = append(packets, p[:n])
|
||||||
|
}
|
||||||
|
|
||||||
|
elapsedTime := time.Since(startTime)
|
||||||
|
|
||||||
|
log.Printf("bytes sent = %d", bytes_sent)
|
||||||
|
log.Printf("Took %s seconds to run", elapsedTime)
|
||||||
|
|
||||||
voiceChannel.Speaking(true)
|
voiceChannel.Speaking(true)
|
||||||
time.Sleep(time.Second * 2)
|
time.Sleep(time.Second * 2)
|
||||||
log.Println("Playing sound")
|
log.Println("Playing sound")
|
||||||
|
|
||||||
for _, p := range frames {
|
startTime = time.Now()
|
||||||
|
bytes_sent = 0
|
||||||
|
for _, p := range packets {
|
||||||
log.Printf("Sending packet: %d bytes", len(p))
|
log.Printf("Sending packet: %d bytes", len(p))
|
||||||
|
bytes_sent += len(p)
|
||||||
|
|
||||||
voiceChannel.OpusSend <- p
|
voiceChannel.OpusSend <- p
|
||||||
}
|
}
|
||||||
|
|
||||||
|
elapsedTime = time.Since(startTime)
|
||||||
|
log.Printf("Packets = %d", len(packets))
|
||||||
|
log.Printf("Network bytes sent = %d", bytes_sent)
|
||||||
|
log.Printf("Took %s seconds to run", elapsedTime)
|
||||||
|
log.Println("Ended stream")
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleSlashCommand(s *dg.Session, i *dg.InteractionCreate) {
|
func handleSlashCommand(s *dg.Session, i *dg.InteractionCreate) {
|
||||||
@ -182,10 +233,5 @@ func downloadVideo(url string) error {
|
|||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
log.Println("Downloaded")
|
log.Println("Downloaded")
|
||||||
|
|
||||||
start := time.Now()
|
|
||||||
opusframes.Encode("vid.webm", "vid.of")
|
|
||||||
duration := time.Since(start)
|
|
||||||
log.Printf("Encoding took: %s", duration)
|
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
@ -1,130 +0,0 @@
|
|||||||
package opusframes
|
|
||||||
|
|
||||||
// Package to encode/decode .of files.
|
|
||||||
// The .of file format will allow us to cache/preprocess
|
|
||||||
// files from the queue and save it in a format that we
|
|
||||||
// can quickly read and send to discord.
|
|
||||||
// The format spect is very simple:
|
|
||||||
// frame size: 2 bytes
|
|
||||||
// frame data: Up to 1275 bytes
|
|
||||||
// data will be stored in little endian format
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"encoding/binary"
|
|
||||||
"errors"
|
|
||||||
"io"
|
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
"os/exec"
|
|
||||||
)
|
|
||||||
|
|
||||||
type ErrFFMPEG error
|
|
||||||
type ErrEncoding error
|
|
||||||
|
|
||||||
// TODO: Allow streaming frames as they are encoded instead
|
|
||||||
// of having to wait for the whole file.
|
|
||||||
// We can probably do this by taking an io.Reader as input
|
|
||||||
// instead of just a file path. Then we return an io.Writer
|
|
||||||
// and allow the user to either stream each encoded frame
|
|
||||||
// or save it to a file. We could also make the Decode
|
|
||||||
// function take an io.Reader so these functions can
|
|
||||||
// just be piped into each other.
|
|
||||||
|
|
||||||
// TODO: Can we pack the frames into chunks of 960? I wonder
|
|
||||||
// if that would make playback smoother or just fuck the
|
|
||||||
// audio. We can try it at some point.
|
|
||||||
func Encode(input, output string) error {
|
|
||||||
ffmpeg := exec.Command(
|
|
||||||
"ffmpeg",
|
|
||||||
"-i", input,
|
|
||||||
"-hide_banner",
|
|
||||||
"-loglevel", "quiet",
|
|
||||||
"-f", "data",
|
|
||||||
"-map", "0:a",
|
|
||||||
"-ar", "48k",
|
|
||||||
"-ac", "2",
|
|
||||||
"-acodec", "libopus",
|
|
||||||
"-b:a", "128k",
|
|
||||||
"pipe:1")
|
|
||||||
|
|
||||||
ffmpegOut, err := ffmpeg.StdoutPipe()
|
|
||||||
if err != nil {
|
|
||||||
return ErrFFMPEG(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = ffmpeg.Start()
|
|
||||||
if err != nil {
|
|
||||||
return ErrFFMPEG(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var fileBuffer bytes.Buffer
|
|
||||||
|
|
||||||
for {
|
|
||||||
p := make([]byte, 960)
|
|
||||||
n, err := ffmpegOut.Read(p)
|
|
||||||
|
|
||||||
binary.Write(&fileBuffer, binary.LittleEndian, uint16(n))
|
|
||||||
binary.Write(&fileBuffer, binary.LittleEndian, p[:n])
|
|
||||||
|
|
||||||
log.Printf("Frame of: %d", n)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
if err == io.EOF {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
return ErrEncoding(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
f, err := os.OpenFile(output, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
f.Write(fileBuffer.Bytes())
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var ErrMalformedFile error = errors.New("malformed file")
|
|
||||||
|
|
||||||
// TODO: Allow streaming frames as they are decoded instead
|
|
||||||
// of having to decode the whole file.
|
|
||||||
func Decode(input string) ([][]byte, error){
|
|
||||||
f, err := os.Open(input)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
// We read the first 2 bytes for the length
|
|
||||||
// then n bytes after that until we're done
|
|
||||||
frames := [][]byte{}
|
|
||||||
for {
|
|
||||||
var frameSize uint16
|
|
||||||
err = binary.Read(f, binary.LittleEndian, &frameSize)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
if err == io.EOF {
|
|
||||||
return frames, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
frame := make([]byte, frameSize)
|
|
||||||
n, err := io.ReadFull(f, frame)
|
|
||||||
|
|
||||||
if n != int(frameSize) {
|
|
||||||
return nil, ErrMalformedFile
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
frames = append(frames, frame)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,54 +0,0 @@
|
|||||||
package validation
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
neturl "net/url"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Errors
|
|
||||||
var (
|
|
||||||
ErrNotAUrl = errors.New("not a URL")
|
|
||||||
ErrIncorrectProtocol = errors.New("incorrect url protocol")
|
|
||||||
ErrServiceUnsupported = errors.New("not a URL")
|
|
||||||
)
|
|
||||||
|
|
||||||
// Music hosts that we support
|
|
||||||
var musicHosts = []string{
|
|
||||||
"youtube.com",
|
|
||||||
"www.youtube.com",
|
|
||||||
}
|
|
||||||
|
|
||||||
func IsUrl(url string) (bool, *neturl.URL) {
|
|
||||||
// If a URL has no scheme, this will fail.
|
|
||||||
// So we'll add one if not present
|
|
||||||
if !strings.Contains(url, "://") {
|
|
||||||
url = fmt.Sprintf("https://%s", url)
|
|
||||||
}
|
|
||||||
|
|
||||||
parsed, err := neturl.ParseRequestURI(url)
|
|
||||||
|
|
||||||
return (err == nil), parsed
|
|
||||||
}
|
|
||||||
|
|
||||||
func IsMusicUrl(url string) (bool, error) {
|
|
||||||
isUrl, parsed := IsUrl(url)
|
|
||||||
|
|
||||||
if !isUrl {
|
|
||||||
return false, ErrNotAUrl
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we have a scheme and it's not http/https we fail
|
|
||||||
if parsed.Scheme != "" && !strings.Contains(parsed.Scheme, "http") {
|
|
||||||
return false, ErrIncorrectProtocol
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, host := range musicHosts {
|
|
||||||
if host == parsed.Host {
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false, ErrServiceUnsupported
|
|
||||||
}
|
|
@ -1,85 +0,0 @@
|
|||||||
package validation
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestIsUrl(t *testing.T) {
|
|
||||||
is, _ := IsUrl("definitely not a url")
|
|
||||||
|
|
||||||
if is {
|
|
||||||
t.Error("Non-url text detected as URL")
|
|
||||||
}
|
|
||||||
|
|
||||||
is, _ = IsUrl("https://example.com")
|
|
||||||
|
|
||||||
if !is {
|
|
||||||
t.Error("URL not detected as URL")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSchemeHandling(t *testing.T) {
|
|
||||||
// No scheme but valid url
|
|
||||||
is, _ := IsUrl("youtube.com")
|
|
||||||
|
|
||||||
if !is {
|
|
||||||
t.Error("URL without scheme came back as not a url")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Preserve scheme
|
|
||||||
is, parsed := IsUrl("ftp://youtube.com")
|
|
||||||
|
|
||||||
if !is {
|
|
||||||
t.Error("URL without scheme came back as not a url")
|
|
||||||
}
|
|
||||||
|
|
||||||
if parsed.Scheme != "ftp" {
|
|
||||||
t.Error("URL scheme was replaced incorrectly")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSupportedMusicUrls(t *testing.T) {
|
|
||||||
// Test actual music url
|
|
||||||
for _, url := range musicHosts {
|
|
||||||
is, _ := IsMusicUrl(url)
|
|
||||||
|
|
||||||
if !is {
|
|
||||||
t.Error("Supported service was detected as unsupported")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestIsMusicUrlErrors(t *testing.T) {
|
|
||||||
// Not a URL
|
|
||||||
is, err := IsMusicUrl("not a url")
|
|
||||||
|
|
||||||
if is {
|
|
||||||
t.Error("Non-URL was detected as url")
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != ErrNotAUrl {
|
|
||||||
t.Error("Incorrect error returned for Non-url link")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Incorrect protocol
|
|
||||||
is, err = IsMusicUrl("ssh://youtube.com")
|
|
||||||
|
|
||||||
if is {
|
|
||||||
t.Error("Incorrect protocol was not caught")
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != ErrIncorrectProtocol {
|
|
||||||
t.Error("Incorrect error returned for incorrect protocol")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unsupported service
|
|
||||||
is, err = IsMusicUrl("https://www.deezer.com/")
|
|
||||||
|
|
||||||
if is {
|
|
||||||
t.Error("Unsupported service was not caught")
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != ErrServiceUnsupported {
|
|
||||||
t.Error("Unsupported service did not return the correct error")
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user