Some work on unfying actions on different sources.

This commit is contained in:
Mirror 2021-02-27 19:38:43 +03:00
parent 3e94eb51d2
commit e70d1de06f
7 changed files with 217 additions and 4 deletions

1
downloader/main.go Normal file
View File

@ -0,0 +1 @@
package downloader

81
gelbooru/api.go Normal file
View File

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

47
gelbooru/types.go Normal file
View File

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

12
main.go
View File

@ -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) {

View File

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

View File

@ -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"`

37
post/main.go Normal file
View File

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