-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathmodel.go
More file actions
170 lines (157 loc) · 3.46 KB
/
model.go
File metadata and controls
170 lines (157 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package asoularticle
import (
"encoding/json"
"fmt"
"math"
"math/rand"
"os"
"strings"
"github.com/FloatTech/floatbox/web"
"github.com/glebarez/sqlite"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
)
const (
articlesURL = "https://asoul.icu/v/articles?pageNum=%v&pageSize=48"
detailURL = "https://asoul.icu/v/articles/%v"
)
type articleReply struct {
Articles []Articles `json:"articles"`
}
// adb 小作文数据库全局变量
var adb *articledb
// articledb 小作文数据库
type articledb gorm.DB
// initialize 初始化
func initialize(dbpath string) *articledb {
var err error
if _, err = os.Stat(dbpath); err != nil || os.IsNotExist(err) {
// 生成文件
f, err := os.Create(dbpath)
if err != nil {
return nil
}
defer f.Close()
}
db, err := gorm.Open(sqlite.Open(dbpath), &gorm.Config{})
if err != nil {
panic(err)
}
adb := (*articledb)(db)
err = db.AutoMigrate(&Article{})
if err != nil {
panic(err)
}
var count int64
err = db.Model(&Article{}).Count(&count).Error
if err != nil {
panic(err)
}
if count == 0 {
err = adb.truncateAndInsert()
if err != nil {
panic(err)
}
}
err = db.Model(&Article{}).Count(&count).Error
if err != nil {
panic(err)
}
logrus.Infoln("[asoularticle]加载", count, "条小作文")
return (*articledb)(db)
}
// Article 小作文数据
type Article struct {
ID int `gorm:"primary_key;AUTO_INCREMENT"`
Title string `gorm:"column:title"`
Author string `gorm:"column:author"`
Brief string `gorm:"column:brief"`
CreateTime int64 `gorm:"column:create_time"`
Tags string `gorm:"column:tags"`
}
// Articles 小作文数据
type Articles struct {
ID int `json:"id"`
Title string `json:"title"`
Author string `json:"author"`
Brief string `json:"brief"`
CreateTime int64 `json:"createTime"`
Tags []string `json:"tags"`
}
// TableName 表名
func (Article) TableName() string {
return "article"
}
func (adb *articledb) truncateAndInsert() (err error) {
db := (*gorm.DB)(adb)
var (
data []byte
r articleReply
pageNum = 1
)
err = db.Exec("drop table article").Error
if err != nil {
return
}
err = db.AutoMigrate(&Article{})
if err != nil {
return
}
data, err = web.GetData(fmt.Sprintf(articlesURL, pageNum))
if err != nil {
return
}
err = json.Unmarshal(data, &r)
if err != nil {
return
}
alist := make([]Article, 0, 100)
for len(r.Articles) > 0 {
for _, v := range r.Articles {
alist = append(alist, Article{
ID: v.ID,
Title: v.Title,
Author: v.Author,
Brief: v.Brief,
CreateTime: v.CreateTime,
Tags: strings.Join(v.Tags, "|"),
})
}
pageNum++
data, err = web.GetData(fmt.Sprintf(articlesURL, pageNum))
if err != nil {
return
}
err = json.Unmarshal(data, &r)
if err != nil {
return
}
}
partition := Partition(alist, 100)
for _, v := range partition {
err = db.Create(&v).Error
if err != nil {
return
}
}
return
}
func (adb *articledb) randomArticle() (a Article, err error) {
db := (*gorm.DB)(adb)
var count int64
err = db.Model(&Article{}).Count(&count).Offset(rand.Intn(int(count))).Take(&a).Error
return
}
// Partition 分组函数
func Partition[T any](list []T, size int) (plist [][]T) {
plen := int(math.Ceil(float64(len(list)) / float64(size)))
plist = make([][]T, plen)
for i := 0; i < plen; i++ {
if i == plen-1 {
plist[i] = list[i*size:]
break
}
plist[i] = list[i*size : (i+1)*size]
}
return
}