|
package main |
|
|
|
import ( |
|
"bytes" |
|
"encoding/json" |
|
"github.com/bwmarrin/discordgo" |
|
"github.com/mmcdole/gofeed" |
|
"github.com/pelletier/go-toml/v2" |
|
"io/ioutil" |
|
"log" |
|
"net/http" |
|
"net/url" |
|
"strconv" |
|
"strings" |
|
) |
|
|
|
type Chapter struct { |
|
Name string |
|
URL *url.URL |
|
ID string |
|
} |
|
|
|
type FoxaholicContext struct { |
|
FeedUrl string |
|
} |
|
|
|
func NewFoxaholicContext() FoxaholicContext { |
|
context := FoxaholicContext{} |
|
context.FeedUrl = "https://www.foxaholic.com/feed/manga-chapters" |
|
return context |
|
} |
|
|
|
type Config struct { |
|
Manga []struct { |
|
Name string |
|
URL_Prefix string |
|
LastChapterGUID int |
|
Color int |
|
} |
|
WebHook string |
|
} |
|
|
|
func LoadConfig(config *Config) error { |
|
f, err := ioutil.ReadFile("config.toml") |
|
if err != nil { |
|
return err |
|
} |
|
|
|
err = toml.Unmarshal(f, config) |
|
|
|
return err |
|
} |
|
|
|
func SaveConfig(config *Config) error { |
|
f, err := toml.Marshal(config) |
|
if err != nil { |
|
return err |
|
} |
|
|
|
err = ioutil.WriteFile("config.toml", f, 0644) |
|
return err |
|
} |
|
|
|
func PostJson(url string, data interface{}) (*http.Response, error) { |
|
b, err := json.Marshal(data) |
|
if err != nil { |
|
return nil, err |
|
} |
|
|
|
return http.Post(url, "application/json", bytes.NewReader(b)) |
|
} |
|
|
|
func main() { |
|
log.Println("Load Config...") |
|
var cfg = new(Config) |
|
err := LoadConfig(cfg) |
|
if err != nil { |
|
log.Fatalf("%v", err) |
|
return |
|
} |
|
log.Println("Config loaded!") |
|
|
|
log.Println("Get Feed...") |
|
context := NewFoxaholicContext() |
|
fp := gofeed.NewParser() |
|
feed, e := fp.ParseURL(context.FeedUrl) |
|
if e != nil { |
|
log.Fatalf("Error: %e", e) |
|
} |
|
log.Println("Got Feed!") |
|
|
|
for _, Item := range feed.Items { |
|
for Index, Manga := range cfg.Manga { |
|
if !strings.HasPrefix(Item.Link, Manga.URL_Prefix) { |
|
continue |
|
} |
|
GUID, e := strconv.Atoi(Item.GUID) |
|
if e != nil { |
|
continue |
|
} |
|
|
|
if GUID > Manga.LastChapterGUID { |
|
log.Println("New Chapter found!") |
|
|
|
cfg.Manga[Index].LastChapterGUID = GUID |
|
|
|
webhook := discordgo.WebhookParams{AvatarURL: "https://c.disquscdn.com/uploads/forums/597/1344/avatar92.jpg", Username: "Foxaholic"} |
|
embed := &discordgo.MessageEmbed{Type: discordgo.EmbedTypeRich} |
|
webhook.Embeds = append(webhook.Embeds, embed) |
|
embed.Color = Manga.Color |
|
embed.Author = &discordgo.MessageEmbedAuthor{Name: Manga.Name, IconURL: "https://c.disquscdn.com/uploads/forums/597/1344/avatar92.jpg"} |
|
|
|
embed.Fields = append(embed.Fields, &discordgo.MessageEmbedField{Name: strings.Replace(Item.Title, "- -", "-", -1), Value: Item.Link, Inline: false}) |
|
|
|
PostJson(cfg.WebHook, webhook) |
|
} |
|
} |
|
} |
|
|
|
log.Println("Save Config...") |
|
SaveConfig(cfg) |
|
log.Println("Config saved!") |
|
} |