From e70d1de06fee52d00e4e7a77e2a8924bd723050f Mon Sep 17 00:00:00 2001 From: Mirror Date: Sat, 27 Feb 2021 19:38:43 +0300 Subject: [PATCH] Some work on unfying actions on different sources. --- downloader/main.go | 1 + gelbooru/api.go | 81 ++++++++++++++++++++++++++++++++++++++++++++++ gelbooru/types.go | 47 +++++++++++++++++++++++++++ main.go | 12 +++++++ pixiv/pixiv.go | 2 +- pixiv/types.go | 41 +++++++++++++++++++++-- post/main.go | 37 +++++++++++++++++++++ 7 files changed, 217 insertions(+), 4 deletions(-) create mode 100644 downloader/main.go create mode 100644 gelbooru/api.go create mode 100644 gelbooru/types.go create mode 100644 post/main.go diff --git a/downloader/main.go b/downloader/main.go new file mode 100644 index 0000000..e518c75 --- /dev/null +++ b/downloader/main.go @@ -0,0 +1 @@ +package downloader diff --git a/gelbooru/api.go b/gelbooru/api.go new file mode 100644 index 0000000..a3d7986 --- /dev/null +++ b/gelbooru/api.go @@ -0,0 +1,81 @@ +package gelbooru + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "time" +) + +type API struct { + apiKey string + userID string + httpClient http.Client +} + +const ( + apiEndpoint = "https://gelbooru.com/index.php" +) + +func newRequest(params map[string]string) (r *http.Request, err error) { + if _, ok := params["page"]; !ok { + params["page"] = "dapi" + } + if _, ok := params["s"]; !ok { + params["s"] = "post" + } + if _, ok := params["q"]; !ok { + params["s"] = "index" + } + if _, ok := params["json"]; !ok { + params["json"] = "1" + } + var addr string + for name, value := range params { + if len(addr) == 0 { + addr = fmt.Sprintf("%s?%s=%s", addr, name, value) + } else { + addr = fmt.Sprintf("%s&%s=%s", addr, name, value) + } + } + addr = fmt.Sprintf("%s%s", apiEndpoint, addr) + + r, err = http.NewRequest(http.MethodGet, addr, nil) + if err != nil { + err = fmt.Errorf("newRequest: %s", err.Error()) + } + return +} + +func New(userID string, APIKey string) (a API) { + a.userID = userID + a.apiKey = APIKey + a.httpClient = http.Client{ + Transport: nil, + CheckRedirect: nil, + Jar: nil, + Timeout: time.Second * 30, + } + return +} + +func (a *API) Search(querry string) (p []Post, err error) { + req, err := newRequest(map[string]string{"tags": querry}) + if err != nil { + err = fmt.Errorf("Search: %s", err.Error()) + return + } + resp, err := a.httpClient.Do(req) + if err != nil { + err = fmt.Errorf("Search: %s", err.Error()) + return + } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + err = json.Unmarshal(body, &p) + if err != nil { + err = fmt.Errorf("Search: %s", err.Error()) + } + return +} diff --git a/gelbooru/types.go b/gelbooru/types.go new file mode 100644 index 0000000..ef04438 --- /dev/null +++ b/gelbooru/types.go @@ -0,0 +1,47 @@ +package gelbooru + +import "strings" + +type Post struct { + Change int `json:"change"` + CreatedAt string `json:"created_at"` + Directory string `json:"directory"` + FileURL string `json:"file_url"` + Hash string `json:"hash"` + Height int `json:"height"` + Width int `json:"width"` + ID int `json:"id"` + Image string `json:"image"` + Owner string `json:"owner"` + ParentID string `json:"parent_id"` + PreviewHeight int `json:"preview_height"` + PreviewWidth int `json:"preview_width"` + Rating string `json:"raiting"` + Sample int `json:"sample"` + SampleHeight int `json:"sample_height"` + SampleWidth int `json:"sample_width"` + Score int `json:"score"` + Source string `json:"source"` + TagsOriginal string `json:"tags"` + Title string `json:"title"` +} + +func (p *Post) Name() string { + return p.Title +} + +func (p *Post) Author() string { + return "" +} + +func (p *Post) Comment() string { + return "" +} + +func (p *Post) Tags() []string { + return strings.Split(p.TagsOriginal, " ") +} + +func (p *Post) Files() []string { + return []string{p.FileURL} +} diff --git a/main.go b/main.go index 30132f5..a577883 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "path" "syscall" + "./gelbooru" "./pixiv" ) @@ -73,6 +74,17 @@ func main() { fmt.Printf("%s\n", err.Error()) } } + + if len(cmdline.testString) > 0 { + gAPI := gelbooru.New("", "") + posts, err := gAPI.Search(cmdline.testString) + if err != nil { + log.Fatal(err.Error()) + } + for _, post := range posts { + fmt.Println(post) + } + } } func fetchFollows(userID string) (err error) { diff --git a/pixiv/pixiv.go b/pixiv/pixiv.go index 3efb7ef..6f4b343 100644 --- a/pixiv/pixiv.go +++ b/pixiv/pixiv.go @@ -102,7 +102,7 @@ func (p *Pixiv) GetIllust(illustID string) (r Illust, err error) { page.Height = r.Height r.Pages = append(r.Pages, page) } - for _, tag := range r.Tags.Tags { + for _, tag := range r.TagsOriginal.Tags { if len(tag.Translation.En) > 0 { r.stringTags = append(r.stringTags, tag.Translation.En) } else if len(tag.Romaji) > 0 { diff --git a/pixiv/types.go b/pixiv/types.go index ddc223d..8ee11ba 100644 --- a/pixiv/types.go +++ b/pixiv/types.go @@ -13,7 +13,7 @@ type Illust struct { IllustType int `json:"illustType"` XRestrict int `json:"xRestrict"` Sl int `json:"sl"` - URL string `json:"url"` + URLoriginal string `json:"url"` Pages []IllustPage URLs struct { Mini string `json:"mini"` @@ -22,8 +22,8 @@ type Illust struct { Regular string `json:"regular"` Original string `json:"original"` } `json:"urls"` - Description string `json:"description"` - Tags struct { + Description string `json:"description"` + TagsOriginal struct { AuthorID string `json:"authorId"` IsLocked bool `json:"isLocked"` Tags []IllustTag `json:"tags"` @@ -57,6 +57,41 @@ func (i *Illust) TagsString() (r string) { return } +func (i *Illust) Name() string { + return i.IllustTitle +} + +func (i *Illust) Author() string { + return i.UserName +} + +func (i *Illust) Comment() string { + return i.IllustComment +} + +func (i *Illust) Tags() []string { + return i.stringTags +} + +func (i *Illust) Files() (urls []string) { + if len(i.Pages) > 0 { + for _, page := range i.Pages { + urls = append(urls, page.URLs.Original) + } + } else { + urls = append(urls, i.URLs.Original) + } + return +} + +func (i *Illust) URL() string { + return i.URLoriginal +} + +func (i *Illust) Type() string { + return "pixiv" +} + //IllustTag . type IllustTag struct { Tag string `json:"tag"` diff --git a/post/main.go b/post/main.go new file mode 100644 index 0000000..d294c4b --- /dev/null +++ b/post/main.go @@ -0,0 +1,37 @@ +package post + +import ( + "fmt" + "syscall" +) + +type UnifiedPost interface { + ID() string + Name() string + Author() string + Comment() string + Tags() []string + Files() []string + URL() string + Stringer() string + Type() string +} + +// WriteAttributes writes info from post to file's metadata +func WriteAttributes(filename string, post UnifiedPost) (err error) { + var tags string + for _, tag := range post.Tags() { + tags = fmt.Sprintf("%s,%s", tags, tag) + } + err = syscall.Setxattr(filename, "user.xdg.tags", []byte(tags), 0) + if err != nil { + err = fmt.Errorf("WriteAttributes: %s", err.Error()) + return + } + err = syscall.Setxattr(filename, "user.xdg.comment", []byte(post.Comment()), 0) + if err != nil { + err = fmt.Errorf("WriteAttributes: %s", err.Error()) + return + } + return +}