From 7ab37e5e10ef945f7f8603ff5ffe163b3deca9e9 Mon Sep 17 00:00:00 2001 From: Mirror Date: Fri, 26 Feb 2021 01:05:07 +0300 Subject: [PATCH] [WIP] typical filenames matcher --- main.go | 3 ++- pixiv/filenames.go | 6 ++++++ pixiv/new.go | 23 +++++++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 pixiv/filenames.go diff --git a/main.go b/main.go index 5e6879f..d13a263 100644 --- a/main.go +++ b/main.go @@ -24,6 +24,7 @@ type cmdlineT struct { threads int setXAttrTags bool setTagsFile string + testString string } var ( @@ -63,7 +64,6 @@ func main() { log.Fatal(err.Error()) } } - } func fetchFollows(userID string) (err error) { @@ -159,5 +159,6 @@ func flags() { 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.StringVar(&cmdline.testString, "test-string", "", "used for testing purposes") flag.Parse() } diff --git a/pixiv/filenames.go b/pixiv/filenames.go new file mode 100644 index 0000000..8177ddc --- /dev/null +++ b/pixiv/filenames.go @@ -0,0 +1,6 @@ +package pixiv + +const ( + // should have subexpressions matching only for IDs + typicalFilenamesRegex = "(?:(?:illust_)(?P[0-9]*)(?:_[0-9]{8}_[0-9]{6}\\.(?:jpg|png|ugoira)))|(?:(?P[0-9]*)(?:_p[0-9]*\\.(?:jpg|png|ugoira)))" +) diff --git a/pixiv/new.go b/pixiv/new.go index 30dcd5b..a213d8d 100644 --- a/pixiv/new.go +++ b/pixiv/new.go @@ -5,6 +5,7 @@ import ( "log" "net/http" "os" + "regexp" "h12.io/socks" ) @@ -20,6 +21,8 @@ type Pixiv struct { logChannel chan string DownloadChannel chan Illust setxattr bool + nameMatcher *regexp.Regexp + idSubexpNumbers []int } //New returns object with methods to access API functions @@ -31,6 +34,7 @@ func New(cookies string, logFilePath string, threads int, xattrs bool) (p Pixiv) p.ItemsPerRequest = 100 p.WorkDirectory = fmt.Sprintf("%s/Pictures/pixiv", os.Getenv("HOME")) p.setxattr = xattrs + p.nameMatcher = regexp.MustCompile(typicalFilenamesRegex) if len(logFilePath) > 0 { logfile, err := os.OpenFile(logFilePath, os.O_APPEND, 664) if err != nil { @@ -71,3 +75,22 @@ func (p *Pixiv) SetProxy(proxy string) (err error) { p.client.Transport = tr return } + +// TypicalFilenamesMatcher matches typical pixiv's filenames. Returns ok if strings match and id extracted from string. +func (p *Pixiv) TypicalFilenamesMatcher(name string) (ok bool, id string) { + ok = p.nameMatcher.MatchString(name) + if ok { + for subexpIndex, subexpName := range p.nameMatcher.SubexpNames() { + if subexpIndex == 0 { + // we don't need main expression here + continue + } + // try to read ID with each subexp and return on first match + id = p.nameMatcher.ReplaceAllString(name, fmt.Sprintf("${%s}", subexpName)) + if len(id) > 0 { + break + } + } + } + return +}