[WIP] typical filenames matcher

This commit is contained in:
Mirror 2021-02-26 01:05:07 +03:00
parent 70caca9ca0
commit 7ab37e5e10
3 changed files with 31 additions and 1 deletions

View File

@ -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: <illust_id>_p<page_number>.<file_ext>, illust_<illust_id>_*")
flag.BoolVar(&cmdline.setXAttrTags, "set-xattr", false, "also write tags to xattrs")
flag.StringVar(&cmdline.testString, "test-string", "", "used for testing purposes")
flag.Parse()
}

6
pixiv/filenames.go Normal file
View File

@ -0,0 +1,6 @@
package pixiv
const (
// should have subexpressions matching only for IDs
typicalFilenamesRegex = "(?:(?:illust_)(?P<illust_id_a>[0-9]*)(?:_[0-9]{8}_[0-9]{6}\\.(?:jpg|png|ugoira)))|(?:(?P<illust_id_b>[0-9]*)(?:_p[0-9]*\\.(?:jpg|png|ugoira)))"
)

View File

@ -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
}