Skip to content
This repository has been archived by the owner on Jun 22, 2023. It is now read-only.

Commit

Permalink
Merge pull request #7 from opq-osc/beta
Browse files Browse the repository at this point in the history
Beta
  • Loading branch information
mcoo authored Jun 1, 2021
2 parents 391bdaa + dc3ea10 commit 6a9758f
Show file tree
Hide file tree
Showing 5 changed files with 342 additions and 39 deletions.
49 changes: 19 additions & 30 deletions Bili/bili.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,14 +505,12 @@ func (m *Manager) UnSubscribeUp(groupId int64, mid int64) (e error) {
Config.CoreConfig.GroupConfig[groupId] = v2
}
Config.Save()

return nil
}
}

return nil
return errors.New("没有订阅该UP")
} else {
return nil
return errors.New("没有订阅该UP")
}

}
Expand Down Expand Up @@ -541,24 +539,26 @@ func (m *Manager) UnSubscribeFanju(groupId int64, mid int64) (e error) {
}
}

return nil
return errors.New("没有订阅该番剧")
} else {
return nil
return errors.New("没有订阅该番剧")
}

}
func (m *Manager) SubscribeUpByKeyword(groupId int64, keyword string) (u UpInfoResult, e error) {
if groupId == 0 {
e = errors.New("默认群禁止订阅!")
return
}
mid, e := m.SearchUp(keyword)
if e != nil {
return
}
u, e = m.SubscribeUpByMid(groupId, mid)
return
}

//func (m *Manager) SubscribeUpByKeyword(groupId int64, keyword string) (u UpInfoResult, e error) {
// if groupId == 0 {
// e = errors.New("默认群禁止订阅!")
// return
// }
// mid, e := m.SearchUp(keyword)
// if e != nil {
// return
// }
// u, e = m.SubscribeUpByMid(groupId, mid)
// return
//}

func (m *Manager) SubscribeFanjuByKeyword(groupId int64, keyword string) (u BiliFanjuResult, e error) {
if groupId == 0 {
e = errors.New("默认群禁止订阅!")
Expand Down Expand Up @@ -735,26 +735,15 @@ func (m *Manager) SubscribeFanjuByMid(groupId int64, mediaId int64) (u BiliFanju

return
}
func (m *Manager) SearchUp(keyword string) (mid int64, err error) {
func (m *Manager) SearchUp(keyword string) (result SearchResult, err error) {
res, err := m.r.Get("https://api.bilibili.com/x/web-interface/search/type?context=&search_type=bili_user&page=1&order=&category_id=&user_type=&order_sort=&changing=mid&__refresh__=true&_extra=&highlight=1&single_column=0&keyword=" + keyword)
if err != nil {
return
}
var result SearchResult
err = res.Json(&result)
if err != nil {
return
}
mid = 0
for _, v := range result.Data.Result {
if v.IsUpuser == 1 {
mid = v.Mid
break
}
}
if mid == 0 {
err = errors.New("没有找到UP")
}
return
}
func (m *Manager) SearchFanju(keyword string) (mid int64, err error) {
Expand Down
7 changes: 7 additions & 0 deletions Config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package Config

import (
"github.com/go-playground/webhooks/v6/github"
"io/ioutil"
"log"
"os"
Expand Down Expand Up @@ -30,6 +31,12 @@ type CoreConfigStruct struct {
BlackGroupList []int64
GroupConfig map[int64]GroupConfig
UserData map[int64]UserData
GithubSub map[string]Repo
}
type Repo struct {
Secret string
WebHook *github.Webhook
Groups []int64
}
type UserData struct {
LastSignDay int
Expand Down
167 changes: 167 additions & 0 deletions githubManager/hookManager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package githubManager

import (
"OPQBot-QQGroupManager/Config"
"errors"
"fmt"
"github.com/go-playground/webhooks/v6/github"
"github.com/kataras/iris/v12"
"github.com/mcoo/OPQBot"
"github.com/mcoo/requests"
"log"
"strings"
"sync"
)

type Manager struct {
github map[string]Config.Repo
githubLock sync.RWMutex
b *OPQBot.BotManager
}

func (m *Manager) DelRepo(repo string, groupId int64) error {
m.githubLock.Lock()
defer m.githubLock.Unlock()
if v, ok := m.github[repo]; ok {
for i, v1 := range v.Groups {
if v1 == groupId {
v.Groups = append(v.Groups[:i], v.Groups[i+1:]...)
m.github[repo] = v
if len(v.Groups) == 0 {
delete(m.github, repo)
}
break
}
}
m.Save()
return nil

} else {
return errors.New("订阅不存在!无法删除 ")
}
}
func (m *Manager) AddRepo(repo string, Secret string, groupId int64) error {
m.githubLock.Lock()
defer m.githubLock.Unlock()

if v, ok := m.github[repo]; ok {
if v.Secret != Secret {
v.Secret = Secret
hook, _ := github.New(github.Options.Secret(Secret))
v.WebHook = hook
}
for _, v1 := range v.Groups {
if v1 == groupId {
m.Save()
return errors.New("已经订阅过了")
}
}
v.Groups = append(v.Groups, groupId)
m.github[repo] = v
} else {
if Secret == "" {
return errors.New("需要Secret, 请直接私聊我发送Secret")
}
hook, _ := github.New(github.Options.Secret(Secret))
m.github[repo] = Config.Repo{
WebHook: hook,
Groups: []int64{groupId},
Secret: Secret,
}
}
err := m.Save()
if err != nil {
return err
}
return nil
}

func NewManager(app *iris.Application, bot *OPQBot.BotManager) Manager {
Config.Lock.RLock()
defer Config.Lock.RUnlock()
g := map[string]Config.Repo{}
for k, v := range Config.CoreConfig.GithubSub {
hook, _ := github.New(github.Options.Secret(v.Secret))
v.WebHook = hook
g[k] = v
}
m := Manager{github: g, githubLock: sync.RWMutex{}, b: bot}
app.Any("github/webhook/{root:path}", func(ctx iris.Context) {
h, err := m.GetRepo(ctx.Params().GetString("root"))
if err != nil {
ctx.StatusCode(404)
return
}
payload, err := h.WebHook.Parse((*ctx).Request(), github.PushEvent, github.PingEvent, github.ReleaseEvent, github.PullRequestEvent)
if err != nil {
log.Println(err)
if err == github.ErrEventNotFound {
ctx.StatusCode(404)
return
}
if err == github.ErrHMACVerificationFailed {
ctx.StatusCode(502)
return
}
}
log.Println(payload)
switch v := payload.(type) {
case github.PingPayload:
log.Println(v)
case github.PushPayload:
var commitString []string
for _, v1 := range v.Commits {
commitString = append(commitString, fmt.Sprintf("[%s] %s", v1.Timestamp, v1.Message))
}
r, _ := requests.Get(v.Sender.AvatarURL)
for _, v1 := range h.Groups {
m.b.SendGroupPicMsg(v1, fmt.Sprintf("%s\nCommit:\n%s", v.Repository.FullName, strings.Join(commitString, "\n")), r.Content())
}
case github.ReleasePayload:
r, _ := requests.Get(v.Sender.AvatarURL)
for _, v1 := range h.Groups {
m.b.SendGroupPicMsg(v1, fmt.Sprintf("%s\n发布了:\n%s", v.Repository.FullName, v.Release.TagName), r.Content())

}
case github.PullRequestPayload:
for _, v1 := range h.Groups {
m.b.SendGroupTextMsg(v1, fmt.Sprintf("%s\nPR: %s", v.Repository.FullName, v.PullRequest.Body))
}

}
})
return m
}
func (m *Manager) GetGroupSubList(groupId int64) (r map[string]Config.Repo) {
m.githubLock.RLock()
defer m.githubLock.RUnlock()
r = map[string]Config.Repo{}
for k, v := range m.github {
for _, v1 := range v.Groups {
if v1 == groupId {
r[k] = v
}
}
}
return
}

func (m *Manager) GetRepo(repo string) (r Config.Repo, err error) {
m.githubLock.RLock()
defer m.githubLock.RUnlock()
var ok bool
if r, ok = m.github[repo]; ok {
return
} else {
err = errors.New("没有订阅该Repo")
return
}

}

func (m *Manager) Save() error {
Config.Lock.Lock()
defer Config.Lock.Unlock()
Config.CoreConfig.GithubSub = m.github
return Config.Save()
}
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ require (
github.com/Joker/hpp v1.0.0 // indirect
github.com/fogleman/gg v1.3.0
github.com/fsnotify/fsnotify v1.4.9
github.com/go-playground/webhooks/v6 v6.0.0-beta.3
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/kataras/iris/v12 v12.2.0-alpha2.0.20210427211137-fa175eb84754
github.com/mcoo/OPQBot v0.1.5
github.com/mcoo/OPQBot v0.1.6
github.com/mcoo/requests v0.0.2
github.com/robfig/cron/v3 v3.0.1
github.com/smartystreets/goconvey v1.6.4 // indirect
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)
)
Loading

0 comments on commit 6a9758f

Please sign in to comment.