bloaterbot

This commit is contained in:
Watn3y 2023-03-05 04:20:17 +01:00
parent 52f69d518a
commit d53b439455
15 changed files with 452 additions and 0 deletions

81
text/nhentai/data.go Normal file
View file

@ -0,0 +1,81 @@
package nhentai
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"watn3y/bloaterbotv3/config"
)
func doAPIRequest(hentaiID string) (exists bool, Details nhentaiResponse) {
client := &http.Client{}
req, err := http.NewRequest("GET", config.BotConfig.Nhentai.APIEndpoint+"/gallery/"+hentaiID, nil)
if err != nil {
log.Fatalln(err)
}
req.Header.Set("User-Agent", "Golang_Spider_Bot/3.0")
resp, err := client.Do(req)
if err != nil {
log.Fatalln(err)
}
if resp.StatusCode == 404 {
return false, nhentaiResponse{}
} else if resp.StatusCode != 200 {
return false, nhentaiResponse{}
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
var hentai nhentaiResponse
if err := json.Unmarshal(body, &hentai); err != nil { // Parse []byte to the go struct pointer
fmt.Println("Can not unmarshal JSON")
}
return true, hentai
}
func parseAPIResponse(rawHentai nhentaiResponse) (formattedHentai hentai) {
var hentai = hentai{
ID: rawHentai.ID,
Title: rawHentai.Title.Pretty,
UploadDate: rawHentai.UploadDate,
}
for _, tag := range rawHentai.Tags {
if tag.Type == "tag" {
hentai.Tags = append(hentai.Tags, struct {
Name string
URL string
}{Name: tag.Name, URL: tag.URL})
} /* else if tag.Type == "parody" {
} else if tag.Type == "character" {
} else if tag.Type == "language" {
} else if tag.Type == "group" {
} else if tag.Type == "artist" {
} else if tag.Type == "category" {
}*/
}
return hentai
}

41
text/nhentai/nhentai.go Normal file
View file

@ -0,0 +1,41 @@
package nhentai
import (
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"regexp"
"strconv"
"watn3y/bloaterbotv3/botIO"
"watn3y/bloaterbotv3/config"
)
func Nhentai(regex string, update tgbotapi.Update, bot *tgbotapi.BotAPI) {
re := regexp.MustCompile(regex)
getdigit := regexp.MustCompile("\\d{2,}")
match := re.FindStringSubmatch(update.Message.Text)
hentainumber := getdigit.FindStringSubmatch(match[0])
hentaiExists, hentaiResponse := doAPIRequest(hentainumber[0])
if !hentaiExists {
return
}
hentai := parseAPIResponse(hentaiResponse)
var tags string
for _, tag := range hentai.Tags {
tags = tags + `<a href="` + config.BotConfig.Nhentai.Domain + tag.URL + `">` + tag.Name + `</a>` + `, `
}
println(tags)
hentaitext := `<b>` + `<a href="` + config.BotConfig.Nhentai.Domain + `/g/` + strconv.Itoa(hentai.ID) + `">` + hentai.Title + `</a> ` + `</b>` + "\n\n" + tags
message := tgbotapi.MessageConfig{
BaseChat: tgbotapi.BaseChat{ChatID: update.Message.Chat.ID, ReplyToMessageID: update.Message.MessageID},
ParseMode: "html",
DisableWebPagePreview: false,
Text: hentaitext,
}
botIO.SendMessage(message, bot)
}

56
text/nhentai/types.go Normal file
View file

@ -0,0 +1,56 @@
package nhentai
type nhentaiResponse struct {
ID int `json:"id"`
Title struct {
English string `json:"english,omitempty"`
Chinese string `json:"chinese,omitempty"`
Japanese string `json:"japanese,omitempty"`
Pretty string `json:"pretty,omitempty"`
} `json:"title,omitempty"`
UploadDate int `json:"upload_date"`
Tags []struct {
Type string `json:"type"`
Name string `json:"name"`
URL string `json:"url"`
} `json:"tags"`
}
type hentai struct {
ID int
Title string
UploadDate int
/*
Language struct { //since there can be multiple languages I won't implement this shit
Name string
URL string
}
Artist struct {
Name string
URL string
}
Group struct {
Name string
URL string
}
Category struct {
Name string
URL string
}
Character struct {
Name string
URL string
}
Parody struct {
Name string
URL string
}
*/
Tags []struct {
Name string
URL string
}
}