func main() { defer log4go.Close() pixiv := pixivspy.NewPixivspy(config.Info.User, config.Info.Password) if !pixiv.Login() { return } downloadPath := util.FixPathName(config.Info.DownloadPath) log4go.Info("检查下载目录: %s", downloadPath) if !util.CheckFileIsExist(downloadPath) { log4go.Info("创建下载目录: %s", downloadPath) os.MkdirAll(downloadPath, 0777) } switch config.Info.Mode { case "id": for _, id := range config.Info.IDS { pixiv.DownloadMemberIllusts(id) } case "star": if len(config.Info.IDS) == 0 { pixiv.DownloadStar("") } else { for _, id := range config.Info.IDS { pixiv.DownloadStar(id) } } default: log4go.Error("Mode必须为[star | id]") } pixiv.WaitDownload() }
func (this *Pixivspy) DownloadMemberIllusts(memberID string) { // 获取P主名称 MemberName := this.parseMemberNameFromMemberID(memberID) log4go.Info("获取P主的昵称: %s", MemberName) // 获取P主图片集 illusts := this.parseMemberIllusts(memberID) if illusts == nil { return } log4go.Info("准备解析[%s]P主的图片集", memberID) ch := make(chan *pixivImage, 8) go func() { defer close(ch) // 匹配图片集并获取图片地址 for _, v := range illusts { page, ok := this.parseIllustPage(v) if ok { query, err := goquery.ParseString(page) if err != nil { log4go.Warn("解析[%s]图片集失败", v.title) return } downloadPath := util.FixPathName(config.Info.DownloadPath + "/" + MemberName + "/" + v.title) log4go.Info("检查图片集目录: %s", downloadPath) if !util.CheckFileIsExist(downloadPath) { log4go.Info("创建图片集目录: %s", downloadPath) os.MkdirAll(downloadPath, 0777) } imageItem := query.Find(".original-image") for i := 0; i < imageItem.Length(); i++ { url := imageItem.Eq(i).Attr("data-src") title := imageItem.Eq(i).Attr("alt") width := imageItem.Eq(i).Attr("width") height := imageItem.Eq(i).Attr("height") ch <- &pixivImage{url: url, title: title, width: width, height: height, downloadPath: downloadPath, href: config.Info.HostPageUrl.Url + v.href} } } } }() for v := range ch { pictureName := v.width + "_" + v.height + "_" + v.url[strings.LastIndex(v.url, "/")+1:] downloadName := util.FixPathName(v.downloadPath + "/" + pictureName) this.waitDownloader.Add(1) atomic.AddInt32(&this.maxTaskNum, 1) log4go.Info("当前任务数:%d, 正在获取空闲任务下载图片[%s]", atomic.LoadInt32(&this.maxTaskNum), downloadName) for atomic.LoadInt32(&this.maxTaskNum) >= config.Info.MaxTaskNum { time.Sleep(time.Second) } go func() { defer this.waitDownloader.Done() defer atomic.AddInt32(&this.maxTaskNum, -1) if util.CheckFileIsExist(downloadName) { log4go.Info("图片[%s]已存在, 跳过本次下载", downloadName) return } log4go.Info("正在下载图片[%s]", pictureName) req, _ := http.NewRequest("GET", v.url, nil) this.setImageHeader(req, v.href) image, err := this.download(req) if err != nil { log4go.Warn("图片[%s]下载失败", pictureName) } else { ioutil.WriteFile(downloadName, image, 0777) } }() } }