From e655068f395c49818f058f616814f5ffd18c3058 Mon Sep 17 00:00:00 2001 From: JustHumanz Date: Mon, 22 Mar 2021 22:20:11 +0700 Subject: [PATCH] v3.3.4 --- CHANGELOG.md | 5 +++ service/fanart/pixiv/pixiv.go | 57 +++++++++++++++++++++++++++- service/frontend/bilibili_handler.go | 12 +++--- service/frontend/general_handler.go | 12 +++--- service/frontend/runner.go | 19 ++++++---- service/frontend/youtube_handler.go | 12 +++--- 6 files changed, 89 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eabefcb4..1853c3cc 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +v3.3.4 +- Fix bug on `setup` +- Add color on pixiv image +- Fix livestream conter not running + v3.3.3 - Fix swaped youtube thub and avatar - Use const for paramater diff --git a/service/fanart/pixiv/pixiv.go b/service/fanart/pixiv/pixiv.go index 3dfef0b1..2ee41b95 100644 --- a/service/fanart/pixiv/pixiv.go +++ b/service/fanart/pixiv/pixiv.go @@ -3,6 +3,10 @@ package pixiv import ( "context" "encoding/json" + "errors" + "io" + "net/http" + "os" "regexp" "sort" "strings" @@ -34,6 +38,7 @@ func Start(a *discordgo.Session, b *cron.Cron, c database.VtubersPayload, d conf Bot = a VtubersData = c configfile = d + CheckPixiv() b.AddFunc(config.PixivFanart, CheckPixiv) log.Info("Enable Pixiv fanart module") } @@ -92,6 +97,16 @@ func CheckPixiv() { Img := Body["urls"].(map[string]interface{}) FixImg := Img["original"].(string) + path, err := DownloadImg(Img["mini"].(string)) + if err != nil { + log.Error(err) + } + + Color, err := engine.GetColor("", path) + if err != nil { + return err + } + usrbyte, err := network.Curl(config.PixivUserEnd+Tags["authorId"].(string), nil) if err != nil { return err @@ -133,7 +148,7 @@ func CheckPixiv() { } var ( tags string - Msg string + Msg = "Pixiv" ) for i, Channel := range ChannelData { @@ -161,6 +176,7 @@ func CheckPixiv() { SetDescription(TextFix). SetImage(config.PixivProxy+FixImg). AddField("User Tags", tags). + SetColor(Color). SetFooter(Msg, config.PixivIMG).MessageEmbed) if err != nil { log.Error(msg, err) @@ -176,7 +192,7 @@ func CheckPixiv() { if i%config.Waiting == 0 && configfile.LowResources { log.WithFields(log.Fields{ - "Func": "Twitter Fanart", + "Func": "Pixiv Fanart", }).Warn(config.FanartSleep) time.Sleep(config.FanartSleep) } @@ -247,3 +263,40 @@ func RemoveHtmlTag(in string) string { } return in } + +func DownloadImg(u string) (string, error) { + dir := config.TmpDir + engine.RanString() + out, err := os.Create(dir) + if err != nil { + return "", err + } + + defer out.Close() + request, err := http.NewRequest(http.MethodGet, u, nil) + if err != nil { + return "", err + } + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer cancel() + + request.Header.Set("cache-control", "no-cache") + request.Header.Set("User-Agent", network.RandomAgent()) + request.Header.Set("Referer", "https://www.pixiv.net") + + spaceClient := http.Client{} + resp, err := spaceClient.Do(request.WithContext(ctx)) + if err != nil { + return "", err + } + + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return "", errors.New(resp.Status) + } + _, err = io.Copy(out, resp.Body) + if err != nil { + return "", err + } + return dir, nil +} diff --git a/service/frontend/bilibili_handler.go b/service/frontend/bilibili_handler.go index 6ec61c9a..1dc46475 100755 --- a/service/frontend/bilibili_handler.go +++ b/service/frontend/bilibili_handler.go @@ -26,7 +26,7 @@ func BiliBiliMessage(s *discordgo.Session, m *discordgo.MessageCreate) { for _, FindGroupArry := range Payload { VTuberGroup, err := FindGropName(FindGroupArry) if err != nil { - Member := FindVtuber(FindGroupArry, 0) + Member := FindVtuber(FindGroupArry) if Member == (database.Member{}) { s.ChannelMessageSend(m.ChannelID, "`"+FindGroupArry+"`,Name of Vtuber Group or Vtuber Name was not found") return @@ -78,7 +78,7 @@ func BiliBiliMessage(s *discordgo.Session, m *discordgo.MessageCreate) { } for _, LiveData := range LiveBili { - LiveData.AddMember(FindVtuber("", LiveData.Member.ID)) + LiveData.AddMember(FindVtuber(LiveData.Member.ID)) FixName := engine.FixName(LiveData.Member.EnName, LiveData.Member.JpName) diff := time.Now().In(loc).Sub(LiveData.Schedul.In(loc)) view, err := strconv.Atoi(LiveData.Viewers) @@ -116,7 +116,7 @@ func BiliBiliMessage(s *discordgo.Session, m *discordgo.MessageCreate) { for _, FindGroupArry := range Payload { VTuberGroup, err := FindGropName(FindGroupArry) if err != nil { - Member := FindVtuber(FindGroupArry, 0) + Member := FindVtuber(FindGroupArry) if Member == (database.Member{}) { s.ChannelMessageSend(m.ChannelID, "`"+FindGroupArry+"`,Name of Vtuber Group or Vtuber Name was not found") return @@ -169,7 +169,7 @@ func BiliBiliMessage(s *discordgo.Session, m *discordgo.MessageCreate) { } for _, LiveData := range LiveBili { - LiveData.AddMember(FindVtuber("", LiveData.Member.ID)) + LiveData.AddMember(FindVtuber(LiveData.Member.ID)) FixName := engine.FixName(LiveData.Member.EnName, LiveData.Member.JpName) view, err := strconv.Atoi(LiveData.Viewers) if err != nil { @@ -223,7 +223,7 @@ func BiliBiliSpace(s *discordgo.Session, m *discordgo.MessageCreate) { for _, FindGroupArry := range strings.Split(strings.TrimSpace(Payload), ",") { VTuberGroup, err := FindGropName(FindGroupArry) if err != nil { - Member := FindVtuber(FindGroupArry, 0) + Member := FindVtuber(FindGroupArry) if Member == (database.Member{}) { s.ChannelMessageSend(m.ChannelID, "`"+FindGroupArry+"`,Name of Vtuber Group or Vtuber Name was not found") return @@ -281,7 +281,7 @@ func BiliBiliSpace(s *discordgo.Session, m *discordgo.MessageCreate) { } for _, SpaceData := range SpaceBili { - SpaceData.AddMember(FindVtuber("", SpaceData.Member.ID)) + SpaceData.AddMember(FindVtuber(SpaceData.Member.ID)) FixName := engine.FixName(SpaceData.Member.EnName, SpaceData.Member.JpName) diff := time.Now().In(loc).Sub(SpaceData.Schedul.In(loc)) diff --git a/service/frontend/general_handler.go b/service/frontend/general_handler.go index 18320260..b08c8a62 100755 --- a/service/frontend/general_handler.go +++ b/service/frontend/general_handler.go @@ -195,7 +195,7 @@ func Tags(s *discordgo.Session, m *discordgo.MessageCreate) { if VtuberName != "" { tmp := strings.Split(VtuberName, ",") for _, Name := range tmp { - Member := FindVtuber(Name, 0) + Member := FindVtuber(Name) if Member == (database.Member{}) { VTuberGroup, err := FindGropName(Name) if err != nil { @@ -348,7 +348,7 @@ func Tags(s *discordgo.Session, m *discordgo.MessageCreate) { tmp := strings.Split(FindInt[1], ",") for _, Name := range tmp { - Member := FindVtuber(Name, 0) + Member := FindVtuber(Name) if Member == (database.Member{}) { VTuberGroup, err := FindGropName(Name) if err != nil { @@ -495,7 +495,7 @@ func Tags(s *discordgo.Session, m *discordgo.MessageCreate) { if VtuberName != "" { tmp := strings.Split(VtuberName, ",") for _, Name := range tmp { - Member := FindVtuber(Name, 0) + Member := FindVtuber(Name) if Member == (database.Member{}) { VTuberGroup, err := FindGropName(Name) if err != nil { @@ -665,7 +665,7 @@ func Tags(s *discordgo.Session, m *discordgo.MessageCreate) { if len(VtuberName[len(VtuberName)-1:]) > 0 { tmp := strings.Split(VtuberName[len(VtuberName)-1:][0], ",") for _, Name := range tmp { - Member := FindVtuber(Name, 0) + Member := FindVtuber(Name) if Member == (database.Member{}) { VTuberGroup, err := FindGropName(Name) if err != nil { @@ -838,7 +838,7 @@ func Tags(s *discordgo.Session, m *discordgo.MessageCreate) { tmp := strings.Split(VtuberName[len(VtuberName)-1:][0], ",") for _, Name := range tmp { - Member := FindVtuber(Name, 0) + Member := FindVtuber(Name) if Member == (database.Member{}) { VTuberGroup, err := FindGropName(Name) if err != nil { @@ -1026,7 +1026,7 @@ func Tags(s *discordgo.Session, m *discordgo.MessageCreate) { if len(VtuberName[len(VtuberName)-2:]) > 0 { tmp := strings.Split(VtuberName[len(VtuberName)-2:][0], ",") for _, Name := range tmp { - Member := FindVtuber(Name, 0) + Member := FindVtuber(Name) if Member == (database.Member{}) { VTuberGroup, err := FindGropName(Name) if err != nil { diff --git a/service/frontend/runner.go b/service/frontend/runner.go index d293476e..3d88d537 100755 --- a/service/frontend/runner.go +++ b/service/frontend/runner.go @@ -191,19 +191,21 @@ func Module(s *discordgo.Session, m *discordgo.MessageCreate) { */ //FindName Find a valid Vtuber name from message handler -func FindVtuber(MemberName string, ID int64) database.Member { - if ID != 0 { +func FindVtuber(M interface{}) database.Member { + MemberName, str := M.(string) + if str { for _, Group := range Payload.VtuberData { for _, Name := range Group.Members { - if Name.ID == ID { + if strings.ToLower(Name.Name) == MemberName || strings.ToLower(Name.JpName) == MemberName || MemberName == strconv.Itoa(int(Name.ID)) { return Name } } } } else { + MemberID := M.(int64) for _, Group := range Payload.VtuberData { for _, Name := range Group.Members { - if strings.ToLower(Name.Name) == MemberName || strings.ToLower(Name.JpName) == MemberName { + if MemberID == Name.ID { return Name } } @@ -215,16 +217,17 @@ func FindVtuber(MemberName string, ID int64) database.Member { //FindGropName Find a valid Vtuber Group from message handler func FindGropName(g interface{}) (database.Group, error) { - ID, err := strconv.Atoi(g.(string)) - if err != nil { + Grp, str := g.(string) + if str { for _, Group := range Payload.VtuberData { - if strings.ToLower(Group.GroupName) == strings.ToLower(g.(string)) { + if strings.ToLower(Group.GroupName) == strings.ToLower(Grp) || strconv.Itoa(int(Group.ID)) == Grp { return Group, nil } } } else { + GrpID := g.(int64) for _, Group := range Payload.VtuberData { - if Group.ID == int64(ID) { + if Group.ID == GrpID { return Group, nil } } diff --git a/service/frontend/youtube_handler.go b/service/frontend/youtube_handler.go index fd0db08b..8efd048b 100755 --- a/service/frontend/youtube_handler.go +++ b/service/frontend/youtube_handler.go @@ -38,7 +38,7 @@ func YoutubeMessage(s *discordgo.Session, m *discordgo.MessageCreate) { for _, GroupNameQuery := range strings.Split(strings.TrimSpace(CommandArray[1]), ",") { VTuberGroup, err := FindGropName(GroupNameQuery) if err != nil { - Member := FindVtuber(GroupNameQuery, 0) + Member := FindVtuber(GroupNameQuery) if Member == (database.Member{}) { _, err := s.ChannelMessageSend(m.ChannelID, "`"+GroupNameQuery+"`,Name of Vtuber Group or Vtuber Name was not found") if err != nil { @@ -109,7 +109,7 @@ func YoutubeMessage(s *discordgo.Session, m *discordgo.MessageCreate) { } if GroupData != nil { for _, Youtube := range GroupData { - Youtube.AddMember(FindVtuber("", Youtube.Member.ID)) + Youtube.AddMember(FindVtuber(Youtube.Member.ID)) FixName := engine.FixName(Youtube.Member.EnName, Youtube.Member.JpName) loc := engine.Zawarudo(Youtube.Member.Region) @@ -173,7 +173,7 @@ func YoutubeMessage(s *discordgo.Session, m *discordgo.MessageCreate) { for _, GroupName := range FindGroupArry { VTuberGroup, err := FindGropName(GroupName) if err != nil { - Member := FindVtuber(GroupName, 0) + Member := FindVtuber(GroupName) if Member == (database.Member{}) { _, err := s.ChannelMessageSend(m.ChannelID, "`"+GroupName+"`,Name of Vtuber Group or Vtuber Name was not found") if err != nil { @@ -242,7 +242,7 @@ func YoutubeMessage(s *discordgo.Session, m *discordgo.MessageCreate) { } if YoutubeData != nil { for _, Youtube := range YoutubeData { - Youtube.AddMember(FindVtuber("", Youtube.Member.ID)) + Youtube.AddMember(FindVtuber(Youtube.Member.ID)) loc := engine.Zawarudo(Youtube.Member.Region) duration := durafmt.Parse(time.Now().In(loc).Sub(Youtube.Schedul.In(loc))).LimitFirstN(2) @@ -309,7 +309,7 @@ func YoutubeMessage(s *discordgo.Session, m *discordgo.MessageCreate) { for _, GroupName := range FindGroupArry { VTuberGroup, err := FindGropName(GroupName) if err != nil { - Member := FindVtuber(GroupName, 0) + Member := FindVtuber(GroupName) if Member == (database.Member{}) { _, err := s.ChannelMessageSend(m.ChannelID, "`"+GroupName+"`,Name of Vtuber Group or Vtuber Name was not found") if err != nil { @@ -386,7 +386,7 @@ func YoutubeMessage(s *discordgo.Session, m *discordgo.MessageCreate) { if YoutubeData != nil { for _, Youtube := range YoutubeData { - Youtube.AddMember(FindVtuber("", Youtube.Member.ID)) + Youtube.AddMember(FindVtuber(Youtube.Member.ID)) Color, err := engine.GetColor(config.TmpDir, Youtube.Thumb) if err != nil {