Added YouTube support, fixed weird condition in Twitter
This commit is contained in:
parent
6d4cd79940
commit
0358e17a5a
2 changed files with 87 additions and 5 deletions
37
helpers.go
37
helpers.go
|
@ -27,10 +27,45 @@ func getRedirectURL(input string) (string, error) {
|
||||||
|
|
||||||
func getLargestFormat(input goutubedl.Result, sizeLimit float64) (format goutubedl.Format, err error) {
|
func getLargestFormat(input goutubedl.Result, sizeLimit float64) (format goutubedl.Format, err error) {
|
||||||
for i := len(input.Formats())-1; i >= 0; i-- {
|
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 {
|
if size <= sizeLimit {
|
||||||
return input.Formats()[i], nil
|
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))
|
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
55
main.go
|
@ -57,6 +57,8 @@ func main() {
|
||||||
BotToken = flag.String("token", "", "Bot access token")
|
BotToken = flag.String("token", "", "Bot access token")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
goutubedl.Path = "yt-dlp"
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
session, err = discordgo.New("Bot " + *BotToken)
|
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{})
|
result, err := goutubedl.New(context.Background(), url, goutubedl.Options{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// If it's complaining due to a lack of videos, 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 {
|
if noVideo, _ := regexp.MatchString(".*There's no video in this tweet.*", err.Error()); noVideo {
|
||||||
|
respond = true
|
||||||
|
} else {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
//session.ChannelMessageSend(message.ChannelID, err.Error())
|
|
||||||
continue
|
continue
|
||||||
} else {
|
} else {
|
||||||
choice, err := getLargestFormat(result, 8*1024*1024)
|
choice, err := getLargestFormat(result, 8*1024*1024)
|
||||||
|
@ -208,8 +210,53 @@ func messageCreate(session *discordgo.Session, message *discordgo.MessageCreate)
|
||||||
}
|
}
|
||||||
|
|
||||||
respond = true
|
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 respond {
|
||||||
if result, err := session.ChannelMessageSendComplex(message.ChannelID, &response); err != nil {
|
if result, err := session.ChannelMessageSendComplex(message.ChannelID, &response); err != nil {
|
||||||
log.Println(result)
|
log.Println(result)
|
||||||
|
|
Loading…
Reference in a new issue