Skip to content

Instantly share code, notes, and snippets.

@Panakotta00
Last active January 8, 2022 15:48
Show Gist options
  • Save Panakotta00/3613634d383ebb4b3b0f97ab65d1aa68 to your computer and use it in GitHub Desktop.
Save Panakotta00/3613634d383ebb4b3b0f97ab65d1aa68 to your computer and use it in GitHub Desktop.
Foxaholic Manga/Novel Discord WebHook Notification in Go
WebHook = '<discord webhook url>'
# Array Of "Manga" Tables, allows for feeding multiple Novels/Mangas
[[Manga]]
Name = '<Header human-readable name for webhook messages>'
URL_Prefix = '<url prefix for all chapters of the same novel>'
Color = 0xff5a14 # color of the embed
LastChapterGUID = 0 # needed to keep track of last sent message

Foxaholic Manga/Novel Discord WebHook Notification in Go

This shitty, quick and dirty program can be executed to check a given manga/novel on the foxahlic page for new chapters.
For this you have to define a config.toml file, which lets you define the manga/novels and the discord webhook you want to trigger.
The program uses the manga-update RSS feed of foxaholic that can be found under https://www.foxaholic.com/feed/manga-chapters.

The URL_Prefix requested in the .toml for each Manga is just stimply the first part of the links to a given chapter that is always the same for the same novel.
f.e. https://www.foxaholic.com/novel/yuushou-ln/v4c1/ is a chapter URL, the prefix we want is https://www.foxaholic.com/novel/yuushou-ln because this part is for all chapter of the same novel the same, but different per Novel.

This program will close to directly return after it checked the chapters and sent the webhook. But you probably want to get notfied without the need to do anything.
For this I reccomend you to simply put the program in a cronjob (make sure the working directory aligns with your config file locaiton) and set it up to run your program every 5min or so.
No webhook will be executed if there is no new chapter found.

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!")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment