Added YouTube support, fixed weird condition in Twitter

This commit is contained in:
Logan G 2022-07-17 21:06:46 -06:00
parent 6d4cd79940
commit 0358e17a5a
Signed by: logan
GPG key ID: E328528C921E7A7A
2 changed files with 87 additions and 5 deletions

View file

@ -27,10 +27,45 @@ func getRedirectURL(input string) (string, error) {
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
var size float64
if input.Formats()[i].FilesizeApprox > 0 {
size = input.Formats()[i].FilesizeApprox
} else {
size = input.Formats()[i].Filesize
}
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))
}
// Cringe YouTube
func getLargestDashFormat(input goutubedl.Result, sizeLimit float64) (videoFormat goutubedl.Format, audioFormat goutubedl.Format, err error) {
// Get best audio format
var bestAudio goutubedl.Format
for i := len(input.Formats())-1; i >= 0; i-- {
if input.Formats()[i].ACodec == "opus" {
bestAudio = input.Formats()[i]
break
}
}
// Get largest video that will fit in the limit with the audio
for i := len(input.Formats())-1; i >= 0; i-- {
if input.Formats()[i].VCodec == "vp9" {
var size float64
if input.Formats()[i].FilesizeApprox > 0 {
size = input.Formats()[i].FilesizeApprox + bestAudio.FilesizeApprox
} else {
size = input.Formats()[i].Filesize + bestAudio.Filesize
}
if size <= sizeLimit {
return input.Formats()[i], bestAudio, nil
}
}
}
return goutubedl.Format{}, goutubedl.Format{}, errors.New(fmt.Sprintf("Could not find a video format that is under the size limit of %f", sizeLimit))
}

55
main.go
View file

@ -57,6 +57,8 @@ func main() {
BotToken = flag.String("token", "", "Bot access token")
)
goutubedl.Path = "yt-dlp"
flag.Parse()
session, err = discordgo.New("Bot " + *BotToken)
@ -172,17 +174,17 @@ func messageCreate(session *discordgo.Session, message *discordgo.MessageCreate)
}
}
response.Content = response.Content + regexp.MustCompile("https://twitter.com/").ReplaceAllString(url, "https://nitter.pussthecat.org/")
response.Content = response.Content + fmt.Sprintf("<%s>", regexp.MustCompile("http.*twitter.com").ReplaceAllString(url, "https://nitter.pussthecat.org"))
goutubedl.Path = "yt-dlp"
result, err := goutubedl.New(context.Background(), url, goutubedl.Options{})
if err != nil {
// 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 {
if noVideo, _ := regexp.MatchString(".*There's no video in this tweet.*", err.Error()); noVideo {
respond = true
} else {
log.Println(err)
}
//session.ChannelMessageSend(message.ChannelID, err.Error())
continue
} else {
choice, err := getLargestFormat(result, 8*1024*1024)
@ -208,8 +210,53 @@ func messageCreate(session *discordgo.Session, message *discordgo.MessageCreate)
}
respond = true
} else if output, _ := regexp.MatchString("(http.*youtube.com/watch.*?v=.*)|(http.*youtu.be/.*)", url); output {
log.Println("YouTube detected.")
if shortned, _ := regexp.MatchString("http.*youtu\\.be/.*", url); shortned {
var err error
log.Println("Short URL detected. Getting long version.")
url, err = getRedirectURL(url)
if err != nil {
log.Println(err)
continue
}
}
response.Content = response.Content + fmt.Sprintf("<%s>", regexp.MustCompile("http.*youtube\\.com").ReplaceAllString(url, "https://piped.kavin.rocks"))
result, err := goutubedl.New(context.Background(), url, goutubedl.Options{})
if err != nil {
log.Println(err)
continue
} else {
videoChoice, audioChoice, err := getLargestDashFormat(result, 8*1024*1024)
log.Printf("Choice: %s+%s | Size: %fM\n", videoChoice.FormatID, audioChoice.FormatID, (videoChoice.Filesize+audioChoice.Filesize)/1024/1024)
if err == nil {
downloadResult, err := result.Download(context.Background(), fmt.Sprintf("%s+%s", videoChoice.FormatID, audioChoice.FormatID))
if err != nil {
log.Println(err)
//session.ChannelMessageSend(message.ChannelID, err.Error())
continue
}
response.Files = append(response.Files, &discordgo.File {
Name: fmt.Sprintf("%s.%s", result.Info.ID, videoChoice.Ext),
ContentType: "text/plain", // This is of course not true, but Discord doesn't give a shit
Reader: downloadResult,
})
defer downloadResult.Close()
} else {
log.Println(err)
}
}
respond = true
}
}
if respond {
if result, err := session.ChannelMessageSendComplex(message.ChannelID, &response); err != nil {
log.Println(result)