Code cleanup

This commit is contained in:
Logan G 2022-07-17 17:36:43 -06:00
parent ab527bf7c6
commit 369bca9547
Signed by: logan
GPG key ID: E328528C921E7A7A
2 changed files with 45 additions and 27 deletions

36
helpers.go Normal file
View file

@ -0,0 +1,36 @@
package main
import (
"log"
"fmt"
"net/http"
"errors"
"github.com/wader/goutubedl"
)
func getRedirectURL(input string) (string, error) {
request, err := http.NewRequest("GET", input, nil)
if err != nil {
return "", err
}
client := &http.Client{}
resp, err := client.Do(request)
if err != nil {
return "", err
}
log.Printf("Found new URL: %s", resp.Request.URL.String())
return resp.Request.URL.String(), nil
}
func getLargestFormat(input goutubedl.Result, sizeLimit float64) (format goutubedl.Format, err error) {
for i := len(input.Formats())-1; i >= 0; i-- {
size := input.Formats()[i].FilesizeApprox
if size <= sizeLimit {
return input.Formats()[i], nil
}
}
return goutubedl.Format{}, errors.New(fmt.Sprintf("Could not find a video format that is under the size limit of %f", sizeLimit))
}

36
main.go
View file

@ -10,7 +10,6 @@ import (
//"strings"
"regexp"
"context"
"net/http"
"github.com/bwmarrin/discordgo"
"github.com/wader/goutubedl"
@ -158,29 +157,22 @@ func messageCreate(session *discordgo.Session, message *discordgo.MessageCreate)
log.Println("Cringe twitter post detected.")
if shortned, _ := regexp.MatchString("http.*t.co/.*", url); shortned {
var err error
log.Println("Short URL detected. Getting long version.")
request, _ := http.NewRequest("GET", url, nil)
client := &http.Client{}
resp, err := client.Do(request)
url, err = getRedirectURL(url)
if err != nil {
log.Println(err)
continue
}
log.Printf("Found new URL: %s", resp.Request.URL.String())
url = resp.Request.URL.String()
}
response.Content = response.Content + regexp.MustCompile("https://twitter.com/").ReplaceAllString(url, "https://nitter.pussthecat.org/")
goutubedl.Path = "yt-dlp"
result, err := goutubedl.New(context.Background(), url, goutubedl.Options{})
if err != nil {
// Probably no videos in it, don't care
// If it's complaining due to a lack of videos, don't care.
if noVideo, _ := regexp.MatchString(".*There's no video in this tweet.*", err.Error()); ! noVideo {
log.Println(err)
}
@ -188,21 +180,9 @@ func messageCreate(session *discordgo.Session, message *discordgo.MessageCreate)
//session.ChannelMessageSend(message.ChannelID, err.Error())
continue
} else {
// Get rid of the stupid short URL that Shitter appends to everything
//response.Content = response.Content + fmt.Sprintf("\"%s\"\n", strings.Trim(regexp.MustCompile("https://t.co/.*").ReplaceAllString(result.Info.Description, ""), " ")) // Still ugly
var choice goutubedl.Format
for i := len(result.Formats())-1; i >= 0; i-- {
size := result.Formats()[i].FilesizeApprox
if size < 8*1024*1024 {
choice = result.Formats()[i]
log.Printf("Choice: %s | Size: %fM\n", choice.FormatID, size/1024/1024)
break
}
}
if len(choice.FormatID) > 0 {
choice, err := getLargestFormat(result, 8*1024*1024)
log.Printf("Choice: %s | Size: %fM\n", choice.FormatID, choice.FilesizeApprox/1024/1024)
if err == nil {
downloadResult, err := result.Download(context.Background(), choice.FormatID)
if err != nil {
log.Println(err)
@ -217,6 +197,8 @@ func messageCreate(session *discordgo.Session, message *discordgo.MessageCreate)
})
defer downloadResult.Close()
} else {
log.Println(err)
}
}