|
| 1 | +package ymgal |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/antchfx/htmlquery" |
| 6 | + _ "github.com/fumiama/sqlite3" // import sql |
| 7 | + "github.com/jinzhu/gorm" |
| 8 | + log "github.com/sirupsen/logrus" |
| 9 | + "math/rand" |
| 10 | + "net/url" |
| 11 | + "os" |
| 12 | + "regexp" |
| 13 | + "strconv" |
| 14 | + "sync" |
| 15 | + "time" |
| 16 | +) |
| 17 | + |
| 18 | +// gdb 得分数据库 |
| 19 | +var gdb *ymgaldb |
| 20 | + |
| 21 | +// ymgaldb galgame图片数据库 |
| 22 | +type ymgaldb gorm.DB |
| 23 | + |
| 24 | +var mu sync.RWMutex |
| 25 | + |
| 26 | +// ymgal gal图片储存结构体 |
| 27 | +type ymgal struct { |
| 28 | + ID int64 `gorm:"column:id" ` |
| 29 | + Title string `gorm:"column:title" ` |
| 30 | + PictureType string `gorm:"column:picture_type" ` |
| 31 | + PictureDescription string `gorm:"column:picture_description;type:varchar(1024)" ` |
| 32 | + PictureList string `gorm:"column:picture_list;type:varchar(20000)" ` |
| 33 | +} |
| 34 | + |
| 35 | +// TableName ... |
| 36 | +func (ymgal) TableName() string { |
| 37 | + return "ymgal" |
| 38 | +} |
| 39 | + |
| 40 | +// initialize 初始化ymgaldb数据库 |
| 41 | +func initialize(dbpath string) *ymgaldb { |
| 42 | + var err error |
| 43 | + if _, err = os.Stat(dbpath); err != nil || os.IsNotExist(err) { |
| 44 | + // 生成文件 |
| 45 | + f, err := os.Create(dbpath) |
| 46 | + if err != nil { |
| 47 | + return nil |
| 48 | + } |
| 49 | + defer f.Close() |
| 50 | + } |
| 51 | + gdb, err := gorm.Open("sqlite3", dbpath) |
| 52 | + if err != nil { |
| 53 | + panic(err) |
| 54 | + } |
| 55 | + gdb.AutoMigrate(&ymgal{}) |
| 56 | + return (*ymgaldb)(gdb) |
| 57 | +} |
| 58 | + |
| 59 | +func (gdb *ymgaldb) insertOrUpdateYmgalByID(id int64, title, pictureType, pictureDescription, pictureList string) (err error) { |
| 60 | + db := (*gorm.DB)(gdb) |
| 61 | + y := ymgal{ |
| 62 | + ID: id, |
| 63 | + Title: title, |
| 64 | + PictureType: pictureType, |
| 65 | + PictureDescription: pictureDescription, |
| 66 | + PictureList: pictureList, |
| 67 | + } |
| 68 | + if err = db.Debug().Model(&ymgal{}).First(&y, "id = ? ", id).Error; err != nil { |
| 69 | + if gorm.IsRecordNotFoundError(err) { |
| 70 | + err = db.Debug().Model(&ymgal{}).Create(&y).Error // newUser not user |
| 71 | + } |
| 72 | + } else { |
| 73 | + err = db.Debug().Model(&ymgal{}).Where("id = ? ", id).Update(map[string]interface{}{ |
| 74 | + "title": title, |
| 75 | + "picture_type": pictureType, |
| 76 | + "picture_description": pictureDescription, |
| 77 | + "picture_list": pictureList, |
| 78 | + }).Error |
| 79 | + } |
| 80 | + return |
| 81 | +} |
| 82 | + |
| 83 | +func (gdb *ymgaldb) getYmgalByID(id string) (y ymgal) { |
| 84 | + db := (*gorm.DB)(gdb) |
| 85 | + db.Debug().Model(&ymgal{}).Where("id = ?", id).Take(&y) |
| 86 | + return |
| 87 | +} |
| 88 | + |
| 89 | +func (gdb *ymgaldb) randomYmgal(pictureType string) (y ymgal) { |
| 90 | + db := (*gorm.DB)(gdb) |
| 91 | + var count int |
| 92 | + s := db.Debug().Model(&ymgal{}).Where("picture_type = ?", pictureType).Count(&count) |
| 93 | + if count == 0 { |
| 94 | + return |
| 95 | + } |
| 96 | + s.Offset(rand.Intn(count)).Take(&y) |
| 97 | + return |
| 98 | +} |
| 99 | + |
| 100 | +func (gdb *ymgaldb) getYmgalByKey(pictureType, key string) (y ymgal) { |
| 101 | + db := (*gorm.DB)(gdb) |
| 102 | + var count int |
| 103 | + s := db.Debug().Model(&ymgal{}).Where("picture_type = ? and (picture_description like ? or title like ?) ", pictureType, "%"+key+"%", "%"+key+"%").Count(&count) |
| 104 | + if count == 0 { |
| 105 | + return |
| 106 | + } |
| 107 | + s.Offset(rand.Intn(count)).Take(&y) |
| 108 | + return |
| 109 | +} |
| 110 | + |
| 111 | +const ( |
| 112 | + webURL = "https://www.ymgal.com" |
| 113 | + cgType = "Gal CG" |
| 114 | + emoticonType = "其他" |
| 115 | + webPicURL = webURL + "/co/picset/" |
| 116 | + reNumber = `\d+` |
| 117 | +) |
| 118 | + |
| 119 | +var ( |
| 120 | + cgURL = webURL + "/search?type=picset&sort=default&category=" + url.QueryEscape(cgType) + "&page=" |
| 121 | + emoticonURL = webURL + "/search?type=picset&sort=default&category=" + url.QueryEscape(emoticonType) + "&page=" |
| 122 | + commonPageNumberExpr = "//*[@id='pager-box']/div/a[@class='icon item pager-next']/preceding-sibling::a[1]/text()" |
| 123 | + cgIDList []string |
| 124 | + emoticonIDList []string |
| 125 | +) |
| 126 | + |
| 127 | +func initPageNumber() (maxCgPageNumber, maxEmoticonPageNumber int) { |
| 128 | + doc, err := htmlquery.LoadURL(cgURL + "1") |
| 129 | + if err != nil { |
| 130 | + log.Errorln("[ymgal]:", err) |
| 131 | + } |
| 132 | + maxCgPageNumber, err = strconv.Atoi(htmlquery.FindOne(doc, commonPageNumberExpr).Data) |
| 133 | + if err != nil { |
| 134 | + log.Errorln("[ymgal]:", err) |
| 135 | + } |
| 136 | + doc, err = htmlquery.LoadURL(emoticonURL + "1") |
| 137 | + if err != nil { |
| 138 | + log.Errorln("[ymgal]:", err) |
| 139 | + } |
| 140 | + maxEmoticonPageNumber, err = strconv.Atoi(htmlquery.FindOne(doc, commonPageNumberExpr).Data) |
| 141 | + if err != nil { |
| 142 | + log.Errorln("[ymgal]:", err) |
| 143 | + } |
| 144 | + return |
| 145 | +} |
| 146 | + |
| 147 | +func getPicID(pageNumber int, pictureType string) { |
| 148 | + var picURL string |
| 149 | + if pictureType == cgType { |
| 150 | + picURL = cgURL + strconv.Itoa(pageNumber) |
| 151 | + } else if pictureType == emoticonType { |
| 152 | + picURL = emoticonURL + strconv.Itoa(pageNumber) |
| 153 | + } |
| 154 | + doc, err := htmlquery.LoadURL(picURL) |
| 155 | + if err != nil { |
| 156 | + log.Errorln("[ymgal]:", err) |
| 157 | + } |
| 158 | + list := htmlquery.Find(doc, "//*[@id='picset-result-list']/ul/div/div[1]/a") |
| 159 | + for i := 0; i < len(list); i++ { |
| 160 | + re := regexp.MustCompile(reNumber) |
| 161 | + picID := re.FindString(list[i].Attr[0].Val) |
| 162 | + if pictureType == cgType { |
| 163 | + cgIDList = append(cgIDList, picID) |
| 164 | + } else if pictureType == emoticonType { |
| 165 | + emoticonIDList = append(emoticonIDList, picID) |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | +} |
| 170 | + |
| 171 | +func updatePic() { |
| 172 | + maxCgPageNumber, maxEmoticonPageNumber := initPageNumber() |
| 173 | + for i := 1; i <= maxCgPageNumber; i++ { |
| 174 | + getPicID(i, cgType) |
| 175 | + time.Sleep(time.Millisecond * 500) |
| 176 | + } |
| 177 | + for i := 1; i <= maxEmoticonPageNumber; i++ { |
| 178 | + getPicID(i, emoticonType) |
| 179 | + time.Sleep(time.Millisecond * 500) |
| 180 | + } |
| 181 | +CGLOOP: |
| 182 | + for i := len(cgIDList) - 1; i >= 0; i-- { |
| 183 | + mu.RLock() |
| 184 | + y := gdb.getYmgalByID(cgIDList[i]) |
| 185 | + mu.RUnlock() |
| 186 | + if y.PictureList == "" { |
| 187 | + mu.Lock() |
| 188 | + storeCgPic(cgIDList[i]) |
| 189 | + mu.Unlock() |
| 190 | + } else { |
| 191 | + break CGLOOP |
| 192 | + } |
| 193 | + time.Sleep(time.Millisecond * 500) |
| 194 | + } |
| 195 | +EMOTICONLOOP: |
| 196 | + for i := len(emoticonIDList) - 1; i >= 0; i-- { |
| 197 | + mu.RLock() |
| 198 | + y := gdb.getYmgalByID(emoticonIDList[i]) |
| 199 | + mu.RUnlock() |
| 200 | + if y.PictureList == "" { |
| 201 | + mu.Lock() |
| 202 | + storeEmoticonPic(emoticonIDList[i]) |
| 203 | + mu.Unlock() |
| 204 | + } else { |
| 205 | + break EMOTICONLOOP |
| 206 | + } |
| 207 | + time.Sleep(time.Millisecond * 500) |
| 208 | + } |
| 209 | +} |
| 210 | + |
| 211 | +func storeCgPic(picIDStr string) { |
| 212 | + picID, err := strconv.ParseInt(picIDStr, 10, 64) |
| 213 | + if err != nil { |
| 214 | + log.Errorln("[ymgal]:", err) |
| 215 | + } |
| 216 | + pictureType := cgType |
| 217 | + doc, err := htmlquery.LoadURL(webPicURL + picIDStr) |
| 218 | + if err != nil { |
| 219 | + log.Errorln("[ymgal]:", err) |
| 220 | + } |
| 221 | + title := htmlquery.FindOne(doc, "//meta[@name='name']").Attr[1].Val |
| 222 | + pictureDescription := htmlquery.FindOne(doc, "//meta[@name='description']").Attr[1].Val |
| 223 | + pictureNumberStr := htmlquery.FindOne(doc, "//div[@class='meta-info']/div[@class='meta-right']/span[2]/text()").Data |
| 224 | + re := regexp.MustCompile(reNumber) |
| 225 | + pictureNumber, err := strconv.Atoi(re.FindString(pictureNumberStr)) |
| 226 | + if err != nil { |
| 227 | + log.Errorln("[ymgal]:", err) |
| 228 | + } |
| 229 | + pictureList := "" |
| 230 | + for i := 1; i <= pictureNumber; i++ { |
| 231 | + picURL := htmlquery.FindOne(doc, fmt.Sprintf("//*[@id='main-picset-warp']/div/div[2]/div/div[@class='swiper-wrapper']/div[%d]", i)).Attr[1].Val |
| 232 | + if i == 1 { |
| 233 | + pictureList += picURL |
| 234 | + } else { |
| 235 | + pictureList += "," + picURL |
| 236 | + } |
| 237 | + } |
| 238 | + err = gdb.insertOrUpdateYmgalByID(picID, title, pictureType, pictureDescription, pictureList) |
| 239 | + if err != nil { |
| 240 | + log.Errorln("[ymgal]:", err) |
| 241 | + } |
| 242 | + |
| 243 | +} |
| 244 | + |
| 245 | +func storeEmoticonPic(picIDStr string) { |
| 246 | + picID, err := strconv.ParseInt(picIDStr, 10, 64) |
| 247 | + if err != nil { |
| 248 | + log.Errorln("[ymgal]:", err) |
| 249 | + } |
| 250 | + pictureType := emoticonType |
| 251 | + doc, err := htmlquery.LoadURL(webPicURL + picIDStr) |
| 252 | + if err != nil { |
| 253 | + log.Errorln("[ymgal]:", err) |
| 254 | + } |
| 255 | + title := htmlquery.FindOne(doc, "//meta[@name='name']").Attr[1].Val |
| 256 | + pictureDescription := htmlquery.FindOne(doc, "//meta[@name='description']").Attr[1].Val |
| 257 | + pictureNumberStr := htmlquery.FindOne(doc, "//div[@class='meta-info']/div[@class='meta-right']/span[2]/text()").Data |
| 258 | + re := regexp.MustCompile(reNumber) |
| 259 | + pictureNumber, err := strconv.Atoi(re.FindString(pictureNumberStr)) |
| 260 | + if err != nil { |
| 261 | + log.Errorln("[ymgal]:", err) |
| 262 | + } |
| 263 | + pictureList := "" |
| 264 | + for i := 1; i <= pictureNumber; i++ { |
| 265 | + picURL := htmlquery.FindOne(doc, fmt.Sprintf("//*[@id='main-picset-warp']/div/div[@class='stream-list']/div[%d]/img", i)).Attr[1].Val |
| 266 | + if i == 1 { |
| 267 | + pictureList += picURL |
| 268 | + } else { |
| 269 | + pictureList += "," + picURL |
| 270 | + } |
| 271 | + } |
| 272 | + err = gdb.insertOrUpdateYmgalByID(picID, title, pictureType, pictureDescription, pictureList) |
| 273 | + if err != nil { |
| 274 | + log.Errorln("[ymgal]:", err) |
| 275 | + } |
| 276 | +} |
0 commit comments