pixiv-scrapper/pixiv/new.go

74 lines
1.6 KiB
Go
Raw Normal View History

2021-02-20 17:40:56 +00:00
package pixiv
import (
"fmt"
"log"
"net/http"
"os"
"h12.io/socks"
)
//Pixiv is API
type Pixiv struct {
phpsessid http.Cookie
Ua string
client *http.Client
RetryCount int
ItemsPerRequest int
WorkDirectory string
logChannel chan string
DownloadChannel chan Illust
setxattr bool
2021-02-20 17:40:56 +00:00
}
//New returns object with methods to access API functions
func New(cookies string, logFilePath string, threads int, xattrs bool) (p Pixiv) {
2021-02-20 17:40:56 +00:00
p.phpsessid = http.Cookie{Name: "PHPSESSID", Value: cookies}
p.Ua = "Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101 Firefox/78.0"
p.client = &http.Client{}
p.RetryCount = 5
p.ItemsPerRequest = 100
p.WorkDirectory = fmt.Sprintf("%s/Pictures/pixiv", os.Getenv("HOME"))
p.setxattr = xattrs
2021-02-20 17:40:56 +00:00
if len(logFilePath) > 0 {
logfile, err := os.OpenFile(logFilePath, os.O_APPEND, 664)
if err != nil {
log.Fatal(err.Error())
}
p.logChannel = make(chan string)
go p.logger(logfile)
} else {
p.logChannel = make(chan string)
go p.logger(os.Stdout)
}
p.DownloadChannel = make(chan Illust)
for i := 0; i < threads; i++ {
go p.downloadWorker()
}
return
}
//Close .
func (p *Pixiv) Close() {
close(p.logChannel)
close(p.DownloadChannel)
}
func (p *Pixiv) logger(logfile *os.File) {
for entry := range p.logChannel {
logfile.WriteString(entry)
}
log.Println("Closing log file")
logfile.Close()
}
//SetProxy sets SOCKS proxy for all requests
func (p *Pixiv) SetProxy(proxy string) (err error) {
dialSocksProxy := socks.Dial(proxy)
tr := &http.Transport{Dial: dialSocksProxy}
p.client.Transport = tr
return
}