e6dl/e621/e621.go

165 lines
3.9 KiB
Go
Raw Normal View History

2019-03-14 03:16:40 +08:00
package e621
2019-03-01 03:47:50 +08:00
import (
"encoding/json"
"io/ioutil"
"net/http"
"strconv"
2019-03-14 06:42:12 +08:00
"time"
2019-03-01 03:47:50 +08:00
)
type PostFile struct {
Width int `json:"width"`
Height int `json:"height"`
Ext string `json:"ext"`
Size int `json:"size"`
Md5 string `json:"md5"`
URL string `json:"url"`
}
type PostPreview struct {
Width int `json:"width"`
Height int `json:"height"`
URL string `json:"url"`
}
type PostSample struct {
Has bool `json:"has"`
Height int `json:"height"`
Width int `json:"width"`
URL string `json:"url"`
}
type PostScore struct {
Up int `json:"up"`
Down int `json:"down"`
Total int `json:"total"`
}
type PostTags struct {
General []string `json:"general"`
Species []string `json:"species"`
Character []string `json:"character"`
Copyright []string `json:"copyright"`
Artist []string `json:"artist"`
Invalid []string `json:"invalid"`
Lore []string `json:"lore"`
Meta []string `json:"meta"`
}
func (t *PostTags) All() []string {
allTags := []string{}
groups := [][]string{
t.General,
t.Species,
t.Character,
t.Copyright,
t.Artist,
t.Invalid,
t.Lore,
t.Meta,
}
for _, g := range groups {
allTags = append(allTags, g...)
}
return allTags
}
type PostFlags struct {
Pending bool `json:"pending"`
Flagged bool `json:"flagged"`
NoteLocked bool `json:"note_locked"`
StatusLocked bool `json:"status_locked"`
RatingLocked bool `json:"rating_locked"`
Deleted bool `json:"deleted"`
}
type PostRelationships struct {
ParentID int `json:"parent_id"`
HasChildren bool `json:"has_children"`
HasActiveChildren bool `json:"has_active_children"`
Children []int `json:"children"`
}
2019-03-01 03:47:50 +08:00
type Post struct {
ID int `json:"id"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
File PostFile `json:"file"`
Preview PostPreview `json:"preview"`
Sample PostSample `json:"sample"`
Score PostScore `json:"score"`
Tags PostTags `json:"tags"`
LockedTags []string `json:"locked_tags"`
ChangeSeq int `json:"change_seq"`
Flags PostFlags `json:"flags"`
Rating string `json:"rating"`
FavCount int `json:"fav_count"`
Sources []string `json:"sources"`
Pools []int `json:"pools"`
Relationships PostRelationships `json:"relationships"`
ApproverID int `json:"approver_id"`
UploaderID int `json:"uploader_id"`
Description string `json:"description"`
CommentCount int `json:"comment_count"`
IsFavorited bool `json:"is_favorited"`
2019-03-01 03:47:50 +08:00
}
// SerializedDate represents a serialized date passed via JSON
type SerializedDate struct {
JSONClass string `json:"json_class"`
2019-03-14 06:42:12 +08:00
Seconds int64 `json:"s"`
Nanoseconds int64 `json:"n"`
}
// Time returns a time.Time object representing the SerializedDate
func (date *SerializedDate) Time() time.Time {
return time.Unix(0, date.Nanoseconds)
2019-03-01 03:47:50 +08:00
}
// GetPostsForTags gets a list of e621 Posts
2019-09-06 10:05:44 +08:00
func GetPostsForTags(tags string, limit int, sfw bool, page int) ([]Post, error) {
2019-03-01 03:47:50 +08:00
client := &http.Client{}
var domain string
if sfw {
domain = "e926.net"
} else {
domain = "e621.net"
}
req, _ := http.NewRequest("GET", "https://"+domain+"/posts.json", nil)
2019-03-01 03:47:50 +08:00
req.Header.Set("User-Agent", "e6dl: go edition (@tjhorner on Telegram)")
qs := req.URL.Query()
qs.Add("tags", tags)
qs.Add("limit", strconv.Itoa(limit))
2019-09-06 10:05:44 +08:00
qs.Add("page", strconv.Itoa(page))
2019-03-01 03:47:50 +08:00
req.URL.RawQuery = qs.Encode()
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var respBody struct {
Posts []Post
}
json.Unmarshal(body, &respBody)
2019-03-01 03:47:50 +08:00
return respBody.Posts, nil
2019-03-01 03:47:50 +08:00
}