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

Commit

Permalink
add Pixiv搜图
Browse files Browse the repository at this point in the history
  • Loading branch information
mcoo committed Jul 22, 2021
1 parent ae045dc commit f06a611
Show file tree
Hide file tree
Showing 11 changed files with 581 additions and 25 deletions.
1 change: 1 addition & 0 deletions Bili/bili.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Manager struct {
fanjuMapLock *sync.RWMutex
r *requests.Request
}

type SearchFanjuResult struct {
Code int `json:"code"`
Message string `json:"message"`
Expand Down
14 changes: 9 additions & 5 deletions Config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ type CoreConfigStruct struct {
Url string
QQ int64
}
DBConfig struct{
DBType string
DBConfig struct {
DBType string
DBUserName string
DBPassword string
DBIP string
DBPort string
DBName string
DBIP string
DBPort string
DBName string
}
SetuConfig struct {
PixivRefreshToken string
PixivProxy string
}
Debug bool
BiliLive bool
Expand Down
6 changes: 3 additions & 3 deletions Config/dbConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

var (
DB *gorm.DB
DB *gorm.DB
sqlDb *sql.DB
//WithUps = SqlArg{ArgId: 1}
//WithJobs = SqlArg{ArgId: 2}
Expand All @@ -22,10 +22,10 @@ func dbInit() {
var err error
conn := ""
if CoreConfig.DBConfig.DBType == "mysql" {
conn = fmt.Sprintf("%v:%v@tcp(%v:%v)/%v?parseTime=True&loc=Local", CoreConfig.DBConfig.DBUserName, CoreConfig.DBConfig.DBPassword,CoreConfig.DBConfig.DBIP,CoreConfig.DBConfig.DBPort, CoreConfig.DBConfig.DBName)
conn = fmt.Sprintf("%v:%v@tcp(%v:%v)/%v?parseTime=True&loc=Local", CoreConfig.DBConfig.DBUserName, CoreConfig.DBConfig.DBPassword, CoreConfig.DBConfig.DBIP, CoreConfig.DBConfig.DBPort, CoreConfig.DBConfig.DBName)
DB, err = gorm.Open(mysql.Open(conn), &gorm.Config{})
} else if CoreConfig.DBConfig.DBType == "sqlite3" {
conn = fmt.Sprintf("./%v.DB", CoreConfig.DBConfig.DBName)
conn = fmt.Sprintf("./%v.db", CoreConfig.DBConfig.DBName)
DB, err = gorm.Open(sqlite.Open(conn), &gorm.Config{})
} else {
panic(errors.New("not supported database adapter"))
Expand Down
65 changes: 52 additions & 13 deletions Core/Core.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package Core
import (
"OPQBot-QQGroupManager/utils"
"errors"
"fmt"
nested "github.com/antonfisher/nested-logrus-formatter"
"github.com/mcoo/OPQBot"
"github.com/sirupsen/logrus"
Expand All @@ -24,17 +25,54 @@ func init() {
func GetLog() *logrus.Logger {
return log
}

var hasLoad []string

func hasLoaded(infoName string) bool {
tmp := false
for _, v1 := range hasLoad {
if v1 == infoName {
tmp = true
break
}
}
return tmp
}
func InitModule(b *Bot) {
for k, v := range Modules {
l := log.WithField("Module", k)
l.Infof("Author: %s - %s", v.ModuleInfo().Author, v.ModuleInfo().Description)
l.Info("正在载入中")
err := v.ModuleInit(b, l)
if err != nil {
l.Error("导入模块时出错!", err)
for _, v := range Modules {
if err := startModule(b, v); err != nil {
log.Error(err)
}
}
}
func startModule(b *Bot, module Module) error {
info := module.ModuleInfo()
if hasLoaded(info.Name) {
return nil
}
l := log.WithField("Module", info.Name)
for _, v2 := range info.RequireModule {
if !hasLoaded(v2) {
if v, ok := Modules[v2]; ok {
err := startModule(b, v)
if err != nil {
return err
}
} else {
return errors.New(fmt.Sprintf("缺少依赖%s 导入失败\n", v2))
}
}
l.Infof("载入成功")
}

l.Infof("Author: %s - %s", info.Author, info.Description)
l.Info("正在载入中")
err := module.ModuleInit(b, l)
if err != nil {
l.Error("导入模块时出错!", err)
}
l.Infof("载入成功")
hasLoad = append(hasLoad, info.Name)
return nil
}
func RegisterModule(module Module) error {
if _, ok := Modules[module.ModuleInfo().Name]; ok {
Expand All @@ -50,13 +88,14 @@ type Bot struct {
OPQBot.BotManager
BotCronManager utils.BotCron
Modules map[string]*Module
DB *gorm.DB
DB *gorm.DB
}
type ModuleInfo struct {
Name string
Author string
Description string
Version int
Name string
Author string
Description string
Version int
RequireModule []string
}
type Module interface {
ModuleInit(bot *Bot, log *logrus.Entry) error
Expand Down
9 changes: 5 additions & 4 deletions githubManager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ var log *logrus.Entry

func (m *Module) ModuleInfo() Core.ModuleInfo {
return Core.ModuleInfo{
Name: "Github订阅姬",
Author: "enjoy",
Description: "",
Version: 0,
Name: "Github订阅姬",
Author: "enjoy",
Description: "",
Version: 0,
RequireModule: []string{"群管理插件"},
}
}
func (m *Module) ModuleInit(b *Core.Bot, l *logrus.Entry) error {
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"OPQBot-QQGroupManager/androidDns"
_ "OPQBot-QQGroupManager/genAndYiqin"
_ "OPQBot-QQGroupManager/githubManager"
_ "OPQBot-QQGroupManager/setu"
"github.com/sirupsen/logrus"

//_ "OPQBot-QQGroupManager/steam"
Expand Down
19 changes: 19 additions & 0 deletions setu/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package setu

import (
"gorm.io/gorm"
)

type DB struct {
d *gorm.DB
}
type GroupConfig struct {
GroupID int64 `gorm:"primaryKey"`
R18 bool
Enable bool
}

func InitDB(db *gorm.DB) DB {
//log.Println(db.AutoMigrate(&GroupConfig{},&setucore.Pic{}))
return DB{d: db}
}
64 changes: 64 additions & 0 deletions setu/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package setu

import (
"OPQBot-QQGroupManager/Core"
"OPQBot-QQGroupManager/setu/pixiv"
"OPQBot-QQGroupManager/setu/setucore"
"fmt"
"github.com/mcoo/OPQBot"
"github.com/mcoo/requests"
"github.com/sirupsen/logrus"
"math/rand"
"strings"
"time"
)

type Module struct {
}

var log *logrus.Entry

func (m *Module) ModuleInfo() Core.ModuleInfo {
return Core.ModuleInfo{
Name: "Setu姬",
Author: "enjoy",
Description: "思路来源于https://github.com/opq-osc/OPQ-SetuBot 天乐giegie的setu机器人",
Version: 0,
}
}
func (m *Module) ModuleInit(b *Core.Bot, l *logrus.Entry) error {
log = l
InitDB(b.DB)
px := &pixiv.Provider{}
RegisterProvider(px, b)
err := b.AddEvent(OPQBot.EventNameOnGroupMessage, func(qq int64, packet *OPQBot.GroupMsgPack) {
if packet.FromUserID != b.QQ {
cm := strings.Split(packet.Content, " ")
if len(cm) == 2 && cm[0] == "搜图测试" {
pics, err := px.SearchPic(cm[1], false)
rand.Seed(time.Now().UnixNano())
num := rand.Intn(len(pics))
res, err := requests.Get(strings.ReplaceAll(pics[num].OriginalPicUrl, "i.pximg.net", "i.pixiv.cat"))
if err != nil {
log.Error(err)
return
}
b.SendGroupPicMsg(packet.FromGroupID, fmt.Sprintf("标题:%s", pics[num].Title), res.Content())
if err != nil {
log.Error(err)
}

}
}
})
if err != nil {
log.Error(err)
}
return nil
}
func init() {
Core.RegisterModule(&Module{})
}
func RegisterProvider(p setucore.Provider, bot *Core.Bot) {
p.InitProvider(log.WithField("SetuProvider", "Pixiv Core"), bot)
}
108 changes: 108 additions & 0 deletions setu/pixiv/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package pixiv

import "time"

type ErrResult struct {
HasError bool `json:"has_error"`
Errors struct {
System struct {
Message string `json:"message"`
Code int `json:"code"`
} `json:"system"`
} `json:"errors"`
Error string `json:"error"`
}

type LoginSuccessResult struct {
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
TokenType string `json:"token_type"`
Scope string `json:"scope"`
RefreshToken string `json:"refresh_token"`
User struct {
ProfileImageUrls struct {
Px16X16 string `json:"px_16x16"`
Px50X50 string `json:"px_50x50"`
Px170X170 string `json:"px_170x170"`
} `json:"profile_image_urls"`
ID string `json:"id"`
Name string `json:"name"`
Account string `json:"account"`
MailAddress string `json:"mail_address"`
IsPremium bool `json:"is_premium"`
XRestrict int `json:"x_restrict"`
IsMailAuthorized bool `json:"is_mail_authorized"`
RequirePolicyAgreement bool `json:"require_policy_agreement"`
} `json:"user"`
Response struct {
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
TokenType string `json:"token_type"`
Scope string `json:"scope"`
RefreshToken string `json:"refresh_token"`
User struct {
ProfileImageUrls struct {
Px16X16 string `json:"px_16x16"`
Px50X50 string `json:"px_50x50"`
Px170X170 string `json:"px_170x170"`
} `json:"profile_image_urls"`
ID string `json:"id"`
Name string `json:"name"`
Account string `json:"account"`
MailAddress string `json:"mail_address"`
IsPremium bool `json:"is_premium"`
XRestrict int `json:"x_restrict"`
IsMailAuthorized bool `json:"is_mail_authorized"`
RequirePolicyAgreement bool `json:"require_policy_agreement"`
} `json:"user"`
} `json:"response"`
}
type IllustResult struct {
Illusts []struct {
ID int `json:"id"`
Title string `json:"title"`
Type string `json:"type"`
ImageUrls struct {
SquareMedium string `json:"square_medium"`
Medium string `json:"medium"`
Large string `json:"large"`
} `json:"image_urls"`
Caption string `json:"caption"`
Restrict int `json:"restrict"`
User struct {
ID int `json:"id"`
Name string `json:"name"`
Account string `json:"account"`
ProfileImageUrls struct {
Medium string `json:"medium"`
} `json:"profile_image_urls"`
IsFollowed bool `json:"is_followed"`
} `json:"user"`
Tags []struct {
Name string `json:"name"`
TranslatedName string `json:"translated_name"`
} `json:"tags"`
Tools []string `json:"tools"`
CreateDate time.Time `json:"create_date"`
PageCount int `json:"page_count"`
Width int `json:"width"`
Height int `json:"height"`
SanityLevel int `json:"sanity_level"`
XRestrict int `json:"x_restrict"`
Series interface{} `json:"series"`
MetaSinglePage struct {
OriginalImageURL string `json:"original_image_url"`
} `json:"meta_single_page,omitempty"`
MetaPages []struct {
ImageUrls struct {
Original string `json:"original"`
} `json:"image_urls"`
} `json:"meta_pages"`
TotalView int `json:"total_view"`
TotalBookmarks int `json:"total_bookmarks"`
IsBookmarked bool `json:"is_bookmarked"`
Visible bool `json:"visible"`
IsMuted bool `json:"is_muted"`
} `json:"illusts"`
SearchSpanLimit int `json:"search_span_limit"`
}
Loading

0 comments on commit f06a611

Please sign in to comment.