Skip to content

Commit

Permalink
Merge pull request #1 from kepler471/refactor-1
Browse files Browse the repository at this point in the history
Refactor 1
  • Loading branch information
kepler471 authored Sep 9, 2020
2 parents 32f63a1 + eda4701 commit 8651edf
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 120 deletions.
1 change: 1 addition & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ patch=$4

if [[ -z "$package" || -z "$major" || -z "$minor" || -z "$patch" ]]; then
echo "usage: $0 <package-name> <major-ver-num> <patch-ver-num> <patch-ver-num>"
# Example: ./build.sh dvembed 1 0 8
exit 1
fi

Expand Down
54 changes: 26 additions & 28 deletions download.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,52 @@ package main

import (
"log"
"net/url"
"os"
"os/exec"
"path"
)

type Media struct {
type media struct {
os.FileInfo
Id string
Url string
Info os.FileInfo
Path string
}

// Download handles retrieving the v.redd.it media
func Download(URL string) (Media, error) {
u, err := url.Parse(URL)
if err != nil {
log.Fatalf("Could not parse %v: %v\n", u.Path, err)
}
var m Media
m.Url = URL
m.Id = path.Base(URL)
err = os.MkdirAll(Dir, 0755)
if err != nil {
log.Printf("Error creating directory: %v\n", err)
}
err = os.MkdirAll(path.Join(Dir, m.Id), 0755)
if err != nil {
log.Printf("Error creating sub-directory: %v\n", err)
// download handles retrieving the v.redd.it media. Initialises
// a media instance for each URL.
func download(URL string) (*media, error) {
f := media{
Id: path.Base(URL),
}
cmd := exec.Command(
"youtube-dl",
"-v",
"--id", // use id as name
//"--id", // use id as name
"--output",
f.Id,
"--write-info-json", // save file information
"--restrict-filenames",
//"--restrict-filenames",
"--merge-output-format", // Downloading the best available audio and video
OutputFormat,
outputFormat,
URL,
)
cmd.Dir = path.Join(Dir, m.Id)
cmd.Dir = path.Join(dir, f.Id)
err := os.MkdirAll(cmd.Dir, 0755)
if err != nil {
log.Printf("Error creating sub-directory: %v", err)
cmd.Dir = dir
}
log.Print("...run youtube-dl...")
err = cmd.Run()
if err != nil {
log.Fatalf("Failed: %v\n", cmd.Args)
log.Printf("Failed process: %v", cmd.Args)
return &f, err
}
m.Info, err = os.Stat(path.Join(cmd.Dir, m.Id+OriginalExt))
f.Path = path.Join(cmd.Dir, f.Id+originalExt)
f.FileInfo, err = os.Stat(f.Path)
if err != nil {
log.Println("Error finding downloaded file")
log.Print("Error finding downloaded file: ", err)
}
// TODO want to return output error from youtube-dl
return m, nil
return &f, err
}
83 changes: 63 additions & 20 deletions download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,70 @@ package main
import "testing"

func TestDownloadVRedditLink(t *testing.T) {
URL := "https://v.redd.it/duir5tuwswl51"
_, err := Download(URL)
if err != nil {
t.Errorf(`youtube-dl %q failed`, URL)
URLs := []string{
"https://v.redd.it/duir5tuwswl51",
"https://v.redd.it/5ltubsoyawl51",
"https://v.redd.it/dttgnvp69wl51",
"https://v.redd.it/e497qwjsh1m51",
}
URL = "https://v.redd.it/5ltubsoyawl51"
_, err = Download(URL)
if err != nil {
t.Errorf(`youtube-dl %q failed`, URL)
}
URL = "https://v.redd.it/dttgnvp69wl51"
_, err = Download(URL)
if err != nil {
t.Errorf(`youtube-dl %q failed`, URL)
for _, URL := range URLs {
_, err := download(URL)
if err != nil {
t.Errorf(`youtube-dl %q failed`, URL)
}
}
}

//func TestDownloadRedditLink(t *testing.T) {
// URL := "https://www.reddit.com/r/IdiotsInCars/comments/ioqqbf/i_know_ill_cut_in_front_of_this_semi/"
// _, err := Download(URL)
// if err != nil {
// t.Errorf(`youtube-dl %q failed`, URL)
// }
//}
func TestDownloadRedditLink(t *testing.T) {
URLs := []string{
"https://www.reddit.com/r/IdiotsInCars/comments/ioqqbf/i_know_ill_cut_in_front_of_this_semi/",
"https://www.reddit.com/r/AnimalsBeingBros/comments/ip89wl/possibly_the_most_patient_kitty_in_the_world_with/",
}
for _, URL := range URLs {
_, err := download(URL)
if err != nil {
t.Errorf(`youtube-dl %q failed`, URL)
// The following logs were producing some errors, have been removed
//t.Log(err)
//t.Logf("Id:\t%v,\nPath:\t%v,\nName:\t%v,\nSize:\t%v", f.Id, f.Path, f.Name(), f.Size())
// Output:
/*
=== RUN TestDownloadVRedditLink
2020/09/09 19:18:57 ...run youtube-dl...
2020/09/09 19:18:58 ...run youtube-dl...
2020/09/09 19:18:59 ...run youtube-dl...
2020/09/09 19:19:00 ...run youtube-dl...
2020/09/09 19:19:00 Error finding downloaded file: stat downloads/e497qwjsh1m51/e497qwjsh1m51.mp4: no such file or directory
download_test.go:15: youtube-dl "https://v.redd.it/e497qwjsh1m51" failed
--- FAIL: TestDownloadVRedditLink (3.25s)
=== RUN TestDownloadRedditLink
2020/09/09 19:19:00 ...run youtube-dl...
2020/09/09 19:19:04 ...run youtube-dl...
2020/09/09 19:19:06 Error finding downloaded file: stat downloads/possibly_the_most_patient_kitty_in_the_world_with/possibly_the_most_patient_kitty_in_the_world_with.mp4: no such file or directory
download_test.go:27: youtube-dl "https://www.reddit.com/r/AnimalsBeingBros/comments/ip89wl/possibly_the_most_patient_kitty_in_the_world_with/" failed
download_test.go:28: stat downloads/possibly_the_most_patient_kitty_in_the_world_with/possibly_the_most_patient_kitty_in_the_world_with.mp4: no such file or directory
--- FAIL: TestDownloadRedditLink (5.44s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x30 pc=0x6ed596]
goroutine 7 [running]:
testing.tRunner.func1.1(0x73b8a0, 0xa459e0)
/usr/lib/golang/src/testing/testing.go:988 +0x30d
testing.tRunner.func1(0xc00011c900)
/usr/lib/golang/src/testing/testing.go:991 +0x3f9
panic(0x73b8a0, 0xa459e0)
/usr/lib/golang/src/runtime/panic.go:969 +0x166
dvembed.TestDownloadRedditLink(0xc00011c900)
/home/steli/source/dvembed/download_test.go:29 +0x1f6
testing.tRunner(0xc00011c900, 0x7be460)
/usr/lib/golang/src/testing/testing.go:1039 +0xdc
created by testing.(*T).Run
/usr/lib/golang/src/testing/testing.go:1090 +0x372
exit status 2
FAIL dvembed 8.691s
*/
}
}
}
27 changes: 19 additions & 8 deletions format.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,39 @@ import (
"path"
)

// Format handles file conversion and formatting with ffmpeg.
type convertedMedia struct {
os.FileInfo
Id string
Path string
}

// format handles file conversion and formatting with ffmpeg.
// Discord allows a maximum 8MB file upload, so this is only
// required if the size limit is exceeded.
func Format(m Media) (Media, error) {
func format(f media) (*convertedMedia, error) {
cmd := exec.Command(
"cmd",
"-i "+m.Info.Name(),
"-i "+f.Name(),
" -c:v libvpx ",
"-crf 0 ",
"-b:v 1M ",
"-c:a libvorbis ",
m.Id+ConvertedExt,
f.Id+convertedExt,
)
cmd.Dir = path.Join(Dir, m.Id)
cmd.Dir = path.Join(dir, f.Id)
err := cmd.Run()
if err != nil {
log.Fatalf("Failed: %v\n", cmd.Args)
}
m.Info, err = os.Stat(path.Join(cmd.Dir, m.Id+ConvertedExt))
cm := convertedMedia{
FileInfo: nil,
Id: f.Id,
Path: "",
}
cm.FileInfo, err = os.Stat(cm.Path)
if err != nil {
log.Println("Error finding formatted file")
log.Print("Error finding formatted file")
}
// TODO want to return output error from ffmpeg
return m, nil
return &cm, nil
}
32 changes: 32 additions & 0 deletions format_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"reflect"
"testing"
)

func TestFormat(t *testing.T) {
type args struct {
m media
}
tests := []struct {
name string
args args
want media
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := format(tt.args.m)
if (err != nil) != tt.wantErr {
t.Errorf("format() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("format() got = %v, want %v", got, tt.want)
}
})
}
}
72 changes: 72 additions & 0 deletions handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package main

import (
"github.com/bwmarrin/discordgo"
"log"
"net/url"
"os"
"strings"
)

func handleVredditLink(s *discordgo.Session, m *discordgo.MessageCreate) {
c := m.ChannelID
log.Printf("Message by %s contains v.redd.it link: %s", m.Author.Username, m.Content)
log.Print("Message sent on channel: ", c)
uu := strings.Split(m.Content, " ")
for _, u := range uu {
if strings.Contains(u, "v.redd.it") || strings.Contains(u, "reddit.com") {
u, err := url.Parse(u)
if err != nil {
log.Printf("Message segment: `%s`, did not parse as URl: %v", u, err)
continue
}
log.Printf("Message segment: `%s`, is valid URl", u)
f, err := download(u.String())
if err != nil {
log.Print("Error downloading f: ", err)
continue
}
if f.Size() > 8000000 {
log.Printf("%v bytes is too large for Discord upload", f.Size())
continue
}
blank := discordgo.MessageEmbed{}
_, err = s.ChannelMessageEditEmbed(c, m.ID, &blank)
if err != nil {
log.Printf("Error removing %s's embedded v.redd.it image: %v", m.Author.Username, err)
}
o, err := os.Open(f.Path)
if err != nil {
log.Print("Error reading ", f.Name())
}
_, err = s.ChannelFileSend(c, f.Name(), o)
if err != nil {
log.Printf("Error uploading %s's media %s, %v", m.Author.Username, f.Name(), err)
}
_ = o.Close()
}
}
}

func handleRedditLink(s *discordgo.Session, m *discordgo.MessageCreate) {
log.Printf("Message by %s contains reddit link: %s", m.Author.Username, m.Content)
//// TODO check if standard reddit link contains v.redd.it media through reddit api
//uu := strings.Split(m.Content, " ")
//for _, u := range uu {
// if strings.Contains(u, "reddit.com") {
// u, err := url.Parse(u)
// if err != nil {
// log.Printf("Message segment: `%s`, did not parse as URl: %v", u, err)
// continue
// }
// log.Printf("Message segment: `%s`, is valid URl", u)
// // getVredditLink(URL)
// }
//}
handleVredditLink(s, m)
}

func handleImgurTest(s *discordgo.Session, m *discordgo.MessageCreate) {
// TODO imgur links seem to embed nicely, so see about using imgur api
_, _ = s.ChannelMessageSend(m.ChannelID, "https://i.imgur.com/ZWexH7h.mp4")
}
Loading

0 comments on commit 8651edf

Please sign in to comment.