Ability to write artwork's tags into file xattrs.
This commit is contained in:
		
							
								
								
									
										6
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								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: <illust_id>_p<page_number>.<file_ext>, illust_<illust_id>_*")
 | 
			
		||||
	flag.BoolVar(&cmdline.setXAttrTags, "set-xattr", false, "also write tags to xattrs")
 | 
			
		||||
	flag.Parse()
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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 {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user