Added URL replacer, bot now acts on nonvideo tweets, general fixes
Bot now replaces Twitter links with Nitter links Fixed sending empty file when all video formats are too big Bot no longer throws error when checking to see if a tweet contains a video
This commit is contained in:
parent
97e0eaf46b
commit
ab527bf7c6
1 changed files with 54 additions and 27 deletions
81
main.go
81
main.go
|
@ -7,9 +7,10 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
"strings"
|
//"strings"
|
||||||
"regexp"
|
"regexp"
|
||||||
"context"
|
"context"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
"github.com/wader/goutubedl"
|
"github.com/wader/goutubedl"
|
||||||
|
@ -156,44 +157,70 @@ func messageCreate(session *discordgo.Session, message *discordgo.MessageCreate)
|
||||||
if output, _ := regexp.MatchString("(http.*twitter.com/.*/status)|(http.*t.co/.*)", url); output {
|
if output, _ := regexp.MatchString("(http.*twitter.com/.*/status)|(http.*t.co/.*)", url); output {
|
||||||
log.Println("Cringe twitter post detected.")
|
log.Println("Cringe twitter post detected.")
|
||||||
|
|
||||||
|
if shortned, _ := regexp.MatchString("http.*t.co/.*", url); shortned {
|
||||||
|
log.Println("Short URL detected. Getting long version.")
|
||||||
|
|
||||||
|
request, _ := http.NewRequest("GET", url, nil)
|
||||||
|
|
||||||
|
client := &http.Client{}
|
||||||
|
resp, err := client.Do(request)
|
||||||
|
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"
|
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 {
|
||||||
|
|
||||||
// Probably no videos in it, don't care
|
// Probably no videos in it, don't care
|
||||||
log.Println(err)
|
if noVideo, _ := regexp.MatchString(".*There's no video in this tweet.*", err.Error()); ! noVideo {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
//session.ChannelMessageSend(message.ChannelID, err.Error())
|
//session.ChannelMessageSend(message.ChannelID, err.Error())
|
||||||
continue
|
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
|
||||||
|
|
||||||
// Get rid of the stupid short URL that Shitter appends to everything
|
var choice goutubedl.Format
|
||||||
response.Content = response.Content + fmt.Sprintf("\"%s\"\n", strings.Trim(regexp.MustCompile("https://t.co/.*").ReplaceAllString(result.Info.Description, ""), " ")) // Still ugly
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var choice goutubedl.Format
|
|
||||||
for i := len(result.Formats())-1; i >= 0; i-- {
|
if len(choice.FormatID) > 0 {
|
||||||
size := result.Formats()[i].FilesizeApprox
|
downloadResult, err := result.Download(context.Background(), choice.FormatID)
|
||||||
if size < 8*1024*1024 {
|
if err != nil {
|
||||||
choice = result.Formats()[i]
|
log.Println(err)
|
||||||
log.Printf("Choice: %s | Size: %fM\n", choice.FormatID, size/1024/1024)
|
//session.ChannelMessageSend(message.ChannelID, err.Error())
|
||||||
break
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
response.Files = append(response.Files, &discordgo.File {
|
||||||
|
Name: fmt.Sprintf("%s.%s", result.Info.ID, choice.Ext),
|
||||||
|
ContentType: "text/plain", // This is of course not true, but Discord doesn't give a shit
|
||||||
|
Reader: downloadResult,
|
||||||
|
})
|
||||||
|
|
||||||
|
defer downloadResult.Close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
downloadResult, err := result.Download(context.Background(), choice.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, choice.Ext),
|
|
||||||
ContentType: "text/plain", // This is of course not true, but Discord doesn't give a shit
|
|
||||||
Reader: downloadResult,
|
|
||||||
})
|
|
||||||
|
|
||||||
respond = true
|
respond = true
|
||||||
|
|
||||||
defer downloadResult.Close()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if respond {
|
if respond {
|
||||||
|
|
Loading…
Reference in a new issue