From 3e94eb51d24b207bcd4d24d820e553c980476948 Mon Sep 17 00:00:00 2001 From: Mirror Date: Fri, 26 Feb 2021 12:04:44 +0300 Subject: [PATCH] Fetching tags by filename --- main.go | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index d13a263..30132f5 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,8 @@ import ( "flag" "fmt" "log" + "path" + "syscall" "./pixiv" ) @@ -64,6 +66,13 @@ func main() { log.Fatal(err.Error()) } } + + if len(cmdline.setTagsFile) > 0 { + err := setTagsFile(cmdline.setTagsFile) + if err != nil { + fmt.Printf("%s\n", err.Error()) + } + } } func fetchFollows(userID string) (err error) { @@ -111,6 +120,20 @@ func fetchBookmarks(userID string) (err error) { return } +func setTagsFile(filename string) (err error) { + if ok, id := pix.TypicalFilenamesMatcher(path.Base(filename)); ok { + illust, err := pix.GetIllust(id) + if err != nil { + return err + } + err = syscall.Setxattr(filename, "user.xdg.tags", []byte(illust.TagsString()), 0) + if err != nil { + return err + } + } + return +} + func fetchIllusts(userID string) (err error) { illusts, err := pix.GetUserIllustsID(userID) if err != nil { @@ -140,6 +163,13 @@ func fetchIllusts(userID string) (err error) { pix.DownloadChannel <- illust } } + + if len(cmdline.setTagsFile) > 0 { + err = setTagsFile(cmdline.setTagsFile) + if err != nil { + fmt.Printf("Request error or ID not recognized: %s\n", err.Error()) + } + } return } @@ -157,7 +187,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.StringVar(&cmdline.setTagsFile, "set-xattr-file", "", "fetch illustration's tags and write them to xattrs of the specified file.") flag.BoolVar(&cmdline.setXAttrTags, "set-xattr", false, "also write tags to xattrs") flag.StringVar(&cmdline.testString, "test-string", "", "used for testing purposes") flag.Parse()