Skip to content

Commit

Permalink
fix: sitemap, image
Browse files Browse the repository at this point in the history
  • Loading branch information
fesiong committed Jun 28, 2024
1 parent fabaccb commit c6d77fd
Show file tree
Hide file tree
Showing 12 changed files with 179 additions and 59 deletions.
38 changes: 20 additions & 18 deletions config/aiGenerate.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
package config

type AiGenerateConfig struct {
Open bool `json:"open"` // 是否自动写作
Language string `json:"language"` // zh|en|cr
DoubleTitle bool `json:"double_title"` // 是否生成双标题
DoubleSplit int `json:"double_split"` // 双标题形式
Demand string `json:"demand"` // 通用Demand
InsertImage int `json:"insert_image"` // 是否插入图片, 0 移除图片,2 插入自定义图片
Images []string `json:"images"`
ContentReplace []ReplaceKeyword `json:"content_replace"`
CategoryId uint `json:"category_id"` //默认分类
SaveType uint `json:"save_type"` // 文档处理方式
StartHour int `json:"start_hour"` //每天开始时间
EndHour int `json:"end_hour"` //每天结束时间
DailyLimit int `json:"daily_limit"` //每日限额
AiEngine string `json:"ai_engine"` // ai 引擎,default 官方接口,openai 自定义openai,spark 星火大模型
OpenAIKeys []OpenAIKey `json:"open_ai_keys"` // self openai key
ApiValid bool `json:"api_valid"` // api地址是否可用
KeyIndex int `json:"-"` // 上一次调用的key id
Spark SparkSetting `json:"spark"`
Open bool `json:"open"` // 是否自动写作
Language string `json:"language"` // zh|en|cr
DoubleTitle bool `json:"double_title"` // 是否生成双标题
DoubleSplit int `json:"double_split"` // 双标题形式
Demand string `json:"demand"` // 通用Demand
InsertImage int `json:"insert_image"` // 是否插入图片, 0 移除图片,2 插入自定义图片,3,图片库分类
Images []string `json:"images"`
ImageCategoryId int `json:"image_category_id"` // 选定的图片分类
ContentReplace []ReplaceKeyword `json:"content_replace"`
CategoryId uint `json:"category_id"` //默认分类
CategoryIds []uint `json:"category_ids"` // 默认分类,支持多个
SaveType uint `json:"save_type"` // 文档处理方式
StartHour int `json:"start_hour"` //每天开始时间
EndHour int `json:"end_hour"` //每天结束时间
DailyLimit int `json:"daily_limit"` //每日限额
AiEngine string `json:"ai_engine"` // ai 引擎,default 官方接口,openai 自定义openai,spark 星火大模型
OpenAIKeys []OpenAIKey `json:"open_ai_keys"` // self openai key
ApiValid bool `json:"api_valid"` // api地址是否可用
KeyIndex int `json:"-"` // 上一次调用的key id
Spark SparkSetting `json:"spark"`
}

type OpenAIKey struct {
Expand Down
4 changes: 3 additions & 1 deletion config/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ type CollectorJson struct {
Channels int `json:"channels"` //预留
CollectMode int `json:"collect_mode"` // 0: 采集, 1: 组合, 2: AI 生成
Language string `json:"language"` // zh|en|cr
InsertImage int `json:"insert_image"` // 是否插入图片, 0 移除图片,1 保留图片,2 插入自定义图片
InsertImage int `json:"insert_image"` // 是否插入图片, 0 移除图片,1 保留图片,2 插入自定义图片,3,图片库分类
Images []string `json:"images"`
ImageCategoryId int `json:"image_category_id"` // 选定的图片分类
FromWebsite string `json:"from_website"`
TitleMinLength int `json:"title_min_length"`
ContentMinLength int `json:"content_min_length"`
Expand All @@ -22,6 +23,7 @@ type CollectorJson struct {
AutoTranslate bool `json:"auto_translate"` //是否翻译
ToLanguage string `json:"to_language"` // 支持谷歌翻译列表语言
CategoryId uint `json:"category_id"` //默认分类
CategoryIds []uint `json:"category_ids"` // 默认分类,支持多个
SaveType uint `json:"save_type"` // 文档处理方式
StartHour int `json:"start_hour"` //每天开始时间
EndHour int `json:"end_hour"` //每天结束时间
Expand Down
9 changes: 5 additions & 4 deletions config/constant.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package config

const Version = "3.3.7"
const Version = "3.3.8"

const (
StatusOK = 0
Expand Down Expand Up @@ -114,9 +114,10 @@ const (
CollectModeCollect = 0
CollectModeCombine = 1

CollectImageRemove = 0 // 移除
CollectImageRetain = 1 // 保留
CollectImageInsert = 2 // 自定义插入
CollectImageRemove = 0 // 移除
CollectImageRetain = 1 // 保留
CollectImageInsert = 2 // 自定义插入
CollectImageCategory = 3 // 插入指定分类图片
)

// login platform
Expand Down
2 changes: 1 addition & 1 deletion model/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (attachment *Attachment) GetThumb(storageUrl string) {
return
}
//如果是一个远程地址,则缩略图和原图地址一致
if strings.HasPrefix(attachment.FileLocation, "http") && !strings.HasPrefix(attachment.FileLocation, "//") {
if strings.HasPrefix(attachment.FileLocation, "http") || strings.HasPrefix(attachment.FileLocation, "//") {
attachment.Logo = attachment.FileLocation
attachment.Thumb = attachment.FileLocation
} else {
Expand Down
45 changes: 40 additions & 5 deletions provider/anqi.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,14 +485,35 @@ func (w *Website) AnqiAiGenerateArticle(keyword *model.Keyword) (int, error) {
}
content[index] = imgTag
}
if w.AiGenerateConfig.InsertImage == config.CollectImageCategory {
// 根据分类每次只取其中一张
img := w.GetRandImageFromCategory(w.AiGenerateConfig.ImageCategoryId, keyword.Title)
if len(img) > 0 {
index := len(content) / 3
content = append(content, "")
copy(content[index+1:], content[index:])
imgTag := "<img src='" + img + "' alt='" + req.Title + "' />"
// ![新的图片](http://xxx/xxx.webp)
if w.Content.Editor == "markdown" {
imgTag = fmt.Sprintf("![%s](%s)", req.Title, img)
}
content[index] = imgTag
}
}

categoryId := keyword.CategoryId
if categoryId == 0 {
if w.AiGenerateConfig.CategoryId == 0 {
if len(w.AiGenerateConfig.CategoryIds) > 0 {
categoryId = w.AiGenerateConfig.CategoryIds[rand.New(rand.NewSource(time.Now().UnixNano())).Intn(len(w.AiGenerateConfig.CategoryIds))]
} else if w.AiGenerateConfig.CategoryId > 0 {
categoryId = w.AiGenerateConfig.CategoryId
}
if categoryId == 0 {
var category model.Category
w.DB.Where("module_id = 1").Take(&category)
w.AiGenerateConfig.CategoryId = category.Id
w.AiGenerateConfig.CategoryIds = []uint{category.Id}
categoryId = category.Id
}
categoryId = w.AiGenerateConfig.CategoryId
}

archiveReq := request.Archive{
Expand Down Expand Up @@ -618,8 +639,22 @@ func (w *Website) AnqiSyncAiPlanResult(plan *model.AiArticlePlan) error {
imgTag := "<img src='" + img + "' alt='" + req.Title + "' />"
content[index] = imgTag
}
if w.AiGenerateConfig.InsertImage == config.CollectImageCategory {
// 根据分类每次只取其中一张
img := w.GetRandImageFromCategory(w.AiGenerateConfig.ImageCategoryId, result.Data.Keyword)
if len(img) > 0 {
index := len(content) / 3
content = append(content, "")
copy(content[index+1:], content[index:])
imgTag := "<img src='" + img + "' alt='" + req.Title + "' />"
content[index] = imgTag
}
}
var keyword *model.Keyword
categoryId := w.AiGenerateConfig.CategoryId
if len(w.AiGenerateConfig.CategoryIds) > 0 {
categoryId = w.AiGenerateConfig.CategoryIds[rand.New(rand.NewSource(time.Now().UnixNano())).Intn(len(w.AiGenerateConfig.CategoryIds))]
}
keyword, err = w.GetKeywordByTitle(plan.Keyword)
if err == nil {
if keyword.CategoryId > 0 {
Expand All @@ -629,9 +664,9 @@ func (w *Website) AnqiSyncAiPlanResult(plan *model.AiArticlePlan) error {
if categoryId == 0 {
var category model.Category
w.DB.Where("module_id = 1").Take(&category)
w.AiGenerateConfig.CategoryId = category.Id
w.AiGenerateConfig.CategoryIds = []uint{category.Id}
categoryId = category.Id
}
categoryId = w.AiGenerateConfig.CategoryId

archive := request.Archive{
Title: result.Data.Title,
Expand Down
33 changes: 33 additions & 0 deletions provider/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,39 @@ func (w *Website) AttachmentScanUploads(baseDir string) {
}
}

func (w *Website) GetRandImageFromCategory(categoryId int, title string) string {
var img string
// 根据分类每次只取其中一张
var attach model.Attachment
if categoryId >= 0 {
w.DB.Model(&model.Attachment{}).Where("category_id = ? and is_image = ?", w.CollectorConfig.ImageCategoryId, 1).Order("rand()").Limit(1).Take(&attach)
} else if categoryId == -1 {
// 全部图片,所以每次只取其中一张
w.DB.Model(&model.Attachment{}).Where("is_image = ?", 1).Order("rand()").Limit(1).Take(&attach)
} else if categoryId == -2 {
// 尝试关键词匹配图片名称
// 每次只取其中一张
// 先分词
keywordSplit := library.WordSplit(title, false)
// 查询attachment表,尝试匹配keywordSplit里的关键词
tx := w.DB.Model(&model.Attachment{}).Where("is_image = ?", 1)
var queries []string
var args []interface{}
for _, word := range keywordSplit {
queries = append(queries, "name like ?")
args = append(args, "%"+word+"%")
}
tx = tx.Where(strings.Join(queries, " OR "), args...)

tx.Order("rand()").Limit(1).Take(&attach)
}
if len(attach.FileLocation) > 0 {
img = w.PluginStorage.StorageUrl + "/" + attach.FileLocation
}

return img
}

func encodeImage(img image.Image, imgType string, quality int) ([]byte, error) {
buff := &bytes.Buffer{}

Expand Down
23 changes: 20 additions & 3 deletions provider/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,17 @@ func (w *Website) SaveCollectArticle(archive *request.Archive, keyword *model.Ke
archive.KeywordId = keyword.Id
categoryId := keyword.CategoryId
if categoryId == 0 {
if w.CollectorConfig.CategoryId == 0 {
if len(w.CollectorConfig.CategoryIds) > 0 {
categoryId = w.CollectorConfig.CategoryIds[rand.New(rand.NewSource(time.Now().UnixNano())).Intn(len(w.CollectorConfig.CategoryIds))]
} else if w.CollectorConfig.CategoryId > 0 {
categoryId = w.CollectorConfig.CategoryId
}
if categoryId == 0 {
var category model.Category
w.DB.Where("module_id = 1").Take(&category)
w.CollectorConfig.CategoryId = category.Id
w.CollectorConfig.CategoryIds = []uint{category.Id}
categoryId = category.Id
}
categoryId = w.CollectorConfig.CategoryId
}
archive.CategoryId = categoryId
//log.Println("draft:", w.CollectorConfig.SaveType)
Expand Down Expand Up @@ -363,6 +368,18 @@ func (w *Website) CollectSingleArticle(link *response.WebLink, keyword *model.Ke
content[index] = "<img src='" + img + "' alt='" + archive.Title + "'/>"
archive.Content = strings.Join(content, "")
}
if w.CollectorConfig.InsertImage == config.CollectImageCategory {
// 根据分类每次只取其中一张
img := w.GetRandImageFromCategory(w.CollectorConfig.ImageCategoryId, keyword.Title)
if len(img) > 0 {
content := strings.SplitAfter(archive.Content, ">")
index := len(content) / 3
content = append(content, "")
copy(content[index+1:], content[index:])
content[index] = "<img src='" + img + "' alt='" + archive.Title + "'/>"
archive.Content = strings.Join(content, "")
}
}
//log.Println(archive.Title, len(archive.Content), archive.OriginUrl)

return archive, nil
Expand Down
35 changes: 30 additions & 5 deletions provider/combination.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,29 @@ func (w *Website) GenerateCombination(keyword *model.Keyword) (int, error) {
copy(content[index+1:], content[index:])
content[index] = "<img src='" + img + "' alt='" + title + "' />"
}
if w.CollectorConfig.InsertImage == config.CollectImageCategory {
// 根据分类每次只取其中一张
img := w.GetRandImageFromCategory(w.CollectorConfig.ImageCategoryId, keyword.Title)
if len(img) > 0 {
index := len(content) / 3
content = append(content, "")
copy(content[index+1:], content[index:])
content[index] = "<img src='" + img + "' alt='" + title + "'/>"
}
}
categoryId := keyword.CategoryId
if categoryId == 0 {
if w.CollectorConfig.CategoryId == 0 {
if len(w.CollectorConfig.CategoryIds) > 0 {
categoryId = w.CollectorConfig.CategoryIds[rand.New(rand.NewSource(time.Now().UnixNano())).Intn(len(w.CollectorConfig.CategoryIds))]
} else if w.CollectorConfig.CategoryId > 0 {
categoryId = w.CollectorConfig.CategoryId
}
if categoryId == 0 {
var category model.Category
w.DB.Where("module_id = 1").Take(&category)
w.CollectorConfig.CategoryId = category.Id
w.CollectorConfig.CategoryIds = []uint{category.Id}
categoryId = category.Id
}
categoryId = w.CollectorConfig.CategoryId
}

archive := request.Archive{
Expand Down Expand Up @@ -159,13 +174,23 @@ func (w *Website) GetCombinationArticle(keyword *model.Keyword) (*request.Archiv
content = append(content, "<p>"+text+"</p>")
}
if w.CollectorConfig.InsertImage == config.CollectImageInsert && len(w.CollectorConfig.Images) > 0 {
rand.Seed(time.Now().UnixMicro())
img := w.CollectorConfig.Images[rand.Intn(len(w.CollectorConfig.Images))]
randItem := rand.New(rand.NewSource(time.Now().UnixNano()))
img := w.CollectorConfig.Images[randItem.Intn(len(w.CollectorConfig.Images))]
index := len(content) / 3
content = append(content, "")
copy(content[index+1:], content[index:])
content[index] = "<img src='" + img + "' alt='" + title + "'/>"
}
if w.CollectorConfig.InsertImage == config.CollectImageCategory {
// 根据分类每次只取其中一张
img := w.GetRandImageFromCategory(w.CollectorConfig.ImageCategoryId, keyword.Title)
if len(img) > 0 {
index := len(content) / 3
content = append(content, "")
copy(content[index+1:], content[index:])
content[index] = "<img src='" + img + "' alt='" + title + "'/>"
}
}

archive := request.Archive{
Title: title,
Expand Down
Loading

0 comments on commit c6d77fd

Please sign in to comment.