From 1b5a30b99094d27d899cefc544b870ac9cc028b7 Mon Sep 17 00:00:00 2001 From: Mirror Date: Mon, 22 Feb 2021 21:57:19 +0300 Subject: [PATCH] Ability to write artwork's tags into file xattrs. --- main.go | 6 +++++- pixiv/downloader.go | 46 ++++++++++++++++++++++++++++++--------------- pixiv/new.go | 4 +++- 3 files changed, 39 insertions(+), 17 deletions(-) diff --git a/main.go b/main.go index a1247b5..5e6879f 100644 --- a/main.go +++ b/main.go @@ -22,6 +22,8 @@ type cmdlineT struct { test bool logErrorsFile string threads int + setXAttrTags bool + setTagsFile string } var ( @@ -32,7 +34,7 @@ var ( func main() { flags() - pix = pixiv.New(cmdline.setCookies, cmdline.logErrorsFile, cmdline.threads) + pix = pixiv.New(cmdline.setCookies, cmdline.logErrorsFile, cmdline.threads, cmdline.setXAttrTags) defer pix.Close() if len(cmdline.setProxy) > 0 { pix.SetProxy(cmdline.setProxy) @@ -155,5 +157,7 @@ func flags() { flag.BoolVar(&cmdline.test, "test", false, "test") flag.StringVar(&cmdline.logErrorsFile, "log-errors", "", "file to strore failed items") flag.IntVar(&cmdline.threads, "threads", 1, "threads number") + flag.StringVar(&cmdline.setTagsFile, "set-xattr-file", "", "[WIP] fetch illustration's tags and write them to xattrs of the specified file. Filename SHOULD be in following formats: _p., illust__*") + flag.BoolVar(&cmdline.setXAttrTags, "set-xattr", false, "also write tags to xattrs") flag.Parse() } diff --git a/pixiv/downloader.go b/pixiv/downloader.go index ca12644..d18da74 100644 --- a/pixiv/downloader.go +++ b/pixiv/downloader.go @@ -7,8 +7,32 @@ import ( "net/http" "os" "strings" + "syscall" ) +type artworkFile struct { + directory string + filename string + url string +} +type artwork struct { + tags []string + files []artworkFile +} + +func (a *artwork) tagsToString() (r string) { + for _, tag := range a.tags { + r = fmt.Sprintf("%s,%s", r, tag) + } + return +} + +func getExtension(fullpath string) (ext string) { + parts := strings.Split(fullpath, ".") + ext = parts[len(parts)-1] + return +} + //DownloadIllust . func (p *Pixiv) downloadIllust(i Illust) (err error) { if !i.Complited() { @@ -17,20 +41,7 @@ func (p *Pixiv) downloadIllust(i Illust) (err error) { return } } - getExtension := func(fullpath string) (ext string) { - parts := strings.Split(fullpath, ".") - ext = parts[len(parts)-1] - return - } - type artworkFile struct { - directory string - filename string - url string - } - type artwork struct { - tags []string - files []artworkFile - } + var art artwork for _, tag := range i.Tags.Tags { if len(tag.Translation.En) > 0 { @@ -66,7 +77,12 @@ func (p *Pixiv) downloadIllust(i Illust) (err error) { if err != nil { return err } - + if p.setxattr { + err = syscall.Setxattr(outfile.Name(), "user.xdg.tags", []byte(art.tagsToString()), 0) + if err != nil { + return err + } + } } return } diff --git a/pixiv/new.go b/pixiv/new.go index 0968ea8..30dcd5b 100644 --- a/pixiv/new.go +++ b/pixiv/new.go @@ -19,16 +19,18 @@ type Pixiv struct { WorkDirectory string logChannel chan string DownloadChannel chan Illust + setxattr bool } //New returns object with methods to access API functions -func New(cookies string, logFilePath string, threads int) (p Pixiv) { +func New(cookies string, logFilePath string, threads int, xattrs bool) (p Pixiv) { 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 if len(logFilePath) > 0 { logfile, err := os.OpenFile(logFilePath, os.O_APPEND, 664) if err != nil {