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:
Logan G 2022-07-17 17:07:01 -06:00
parent 97e0eaf46b
commit ab527bf7c6
Signed by: logan
GPG key ID: E328528C921E7A7A

43
main.go
View file

@ -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,17 +157,39 @@ 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.")
goutubedl.Path = "yt-dlp" if shortned, _ := regexp.MatchString("http.*t.co/.*", url); shortned {
result, err := goutubedl.New(context.Background(), url, goutubedl.Options{}) 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 { if err != nil {
// Probably no videos in it, don't care
log.Println(err) log.Println(err)
//session.ChannelMessageSend(message.ChannelID, err.Error())
continue 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 noVideo, _ := regexp.MatchString(".*There's no video in this tweet.*", err.Error()); ! noVideo {
log.Println(err)
}
//session.ChannelMessageSend(message.ChannelID, err.Error())
continue
} else {
// Get rid of the stupid short URL that Shitter appends to everything // 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 //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 var choice goutubedl.Format
for i := len(result.Formats())-1; i >= 0; i-- { for i := len(result.Formats())-1; i >= 0; i-- {
@ -178,6 +201,8 @@ func messageCreate(session *discordgo.Session, message *discordgo.MessageCreate)
} }
} }
if len(choice.FormatID) > 0 {
downloadResult, err := result.Download(context.Background(), choice.FormatID) downloadResult, err := result.Download(context.Background(), choice.FormatID)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
@ -191,11 +216,13 @@ func messageCreate(session *discordgo.Session, message *discordgo.MessageCreate)
Reader: downloadResult, Reader: downloadResult,
}) })
respond = true
defer downloadResult.Close() defer downloadResult.Close()
} }
} }
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)