parent
d5749b982c
commit
97b3a747af
2 changed files with 201 additions and 8 deletions
@ -0,0 +1,188 @@ |
||||
package jschan |
||||
|
||||
import ( |
||||
"context" |
||||
"fmt" |
||||
"net/http" |
||||
"net/url" |
||||
"strings" |
||||
"time" |
||||
) |
||||
|
||||
type Geometry struct { |
||||
Width int `json:"width"` |
||||
Height int `json:"height"` |
||||
Thumbwidth int `json:"thumbwidth"` |
||||
Thumbheight int `json:"thumbheight"` |
||||
} |
||||
type Files struct { |
||||
Spoiler interface{} `json:"spoiler"` |
||||
Hash string `json:"hash"` |
||||
Filename string `json:"filename"` |
||||
OriginalFilename string `json:"originalFilename"` |
||||
Mimetype string `json:"mimetype"` |
||||
Size int `json:"size"` |
||||
Extension string `json:"extension"` |
||||
Thumbextension string `json:"thumbextension"` |
||||
Geometry Geometry `json:"geometry"` |
||||
GeometryString string `json:"geometryString"` |
||||
HasThumb bool `json:"hasThumb"` |
||||
SizeString string `json:"sizeString"` |
||||
} |
||||
type Backlinks struct { |
||||
ID string `json:"_id"` |
||||
PostID int `json:"postId"` |
||||
} |
||||
type Country struct { |
||||
Name string `json:"name"` |
||||
Code string `json:"code"` |
||||
Src string `json:"src"` |
||||
Custom bool `json:"custom"` |
||||
} |
||||
type Quotes struct { |
||||
ID string `json:"_id"` |
||||
Thread int `json:"thread"` |
||||
PostID int `json:"postId"` |
||||
} |
||||
type Edited struct { |
||||
Username string `json:"username"` |
||||
Date time.Time `json:"date"` |
||||
} |
||||
type Replies struct { |
||||
ID string `json:"_id"` |
||||
Date time.Time `json:"date"` |
||||
U int64 `json:"u"` |
||||
Name string `json:"name"` |
||||
Country Country `json:"country"` |
||||
Board string `json:"board"` |
||||
Tripcode interface{} `json:"tripcode"` |
||||
Capcode interface{} `json:"capcode"` |
||||
Subject string `json:"subject"` |
||||
Message string `json:"message"` |
||||
Messagehash string `json:"messagehash"` |
||||
Nomarkup string `json:"nomarkup"` |
||||
Thread int `json:"thread"` |
||||
Email string `json:"email"` |
||||
Spoiler bool `json:"spoiler"` |
||||
Banmessage interface{} `json:"banmessage"` |
||||
UserID interface{} `json:"userId"` |
||||
Files []interface{} `json:"files"` |
||||
Quotes []Quotes `json:"quotes"` |
||||
Crossquotes []interface{} `json:"crossquotes"` |
||||
Backlinks []Backlinks `json:"backlinks"` |
||||
PostID int `json:"postId"` |
||||
Edited Edited `json:"edited,omitempty"` |
||||
} |
||||
type Threads struct { |
||||
ID string `json:"_id"` |
||||
Date time.Time `json:"date"` |
||||
Name string `json:"name"` |
||||
Country interface{} `json:"country"` |
||||
Board string `json:"board"` |
||||
Tripcode interface{} `json:"tripcode"` |
||||
Capcode string `json:"capcode"` |
||||
Subject string `json:"subject"` |
||||
Message string `json:"message"` |
||||
Messagehash string `json:"messagehash"` |
||||
Nomarkup string `json:"nomarkup"` |
||||
Thread interface{} `json:"thread"` |
||||
Email string `json:"email"` |
||||
Spoiler bool `json:"spoiler"` |
||||
Banmessage interface{} `json:"banmessage"` |
||||
UserID interface{} `json:"userId"` |
||||
Files []Files `json:"files"` |
||||
Quotes []interface{} `json:"quotes"` |
||||
Crossquotes []interface{} `json:"crossquotes"` |
||||
Backlinks []Backlinks `json:"backlinks"` |
||||
Replyposts int `json:"replyposts"` |
||||
Replyfiles int `json:"replyfiles"` |
||||
Sticky int `json:"sticky"` |
||||
Locked int `json:"locked"` |
||||
Bumplocked int `json:"bumplocked"` |
||||
Cyclic int `json:"cyclic"` |
||||
Bumped time.Time `json:"bumped"` |
||||
PostID int `json:"postId"` |
||||
U int64 `json:"u"` |
||||
Replies []Replies `json:"replies"` |
||||
Previewbacklinks []interface{} `json:"previewbacklinks,omitempty"` |
||||
Omittedfiles int `json:"omittedfiles,omitempty"` |
||||
Omittedposts int `json:"omittedposts,omitempty"` |
||||
Edited Edited `json:"edited,omitempty"` |
||||
} |
||||
|
||||
type GetOverboardOptions struct { |
||||
AddBoards []string `json:"search"` |
||||
RemoveBoards []string `json:"sort"` |
||||
IncludeDefault bool `json:"include_default"` |
||||
} |
||||
|
||||
type GetOverboardResponse struct { |
||||
Threads []Threads `json:"threads"` |
||||
} |
||||
|
||||
func getOverboardQuery(options *GetOverboardOptions) (*url.Values, error) { |
||||
include_default := false |
||||
add := []string{} |
||||
rem := []string{} |
||||
if options != nil { |
||||
add = options.AddBoards |
||||
rem = options.RemoveBoards |
||||
} |
||||
query := url.Values{} |
||||
query.Set("add", strings.Join(add, ",")) |
||||
query.Set("rem", strings.Join(rem, ",")) |
||||
if include_default { |
||||
query.Set("include_default", "true") |
||||
} |
||||
return &query, nil |
||||
} |
||||
|
||||
func (c *Client) GetOverboardIndex(ctx context.Context, options *GetOverboardOptions) (*GetOverboardResponse, error) { |
||||
|
||||
query, err := getOverboardQuery(options) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
url := fmt.Sprintf("%s/overboard.json?%s", c.BaseURL, query.Encode()) |
||||
|
||||
req, err := http.NewRequest("GET", url, nil) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
req = req.WithContext(ctx) |
||||
|
||||
res := GetOverboardResponse{} |
||||
if err := c.sendRequest(req, &res); err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
return &res, nil |
||||
|
||||
} |
||||
|
||||
func (c *Client) GetOverboardCatalog(ctx context.Context, options *GetOverboardOptions) (*GetOverboardResponse, error) { |
||||
|
||||
query, err := getOverboardQuery(options) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
url := fmt.Sprintf("%s/catalog.json?%s", c.BaseURL, query.Encode()) |
||||
|
||||
req, err := http.NewRequest("GET", url, nil) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
req = req.WithContext(ctx) |
||||
|
||||
res := GetOverboardResponse{} |
||||
if err := c.sendRequest(req, &res); err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
return &res, nil |
||||
|
||||
} |
@ -1,27 +1,32 @@ |
||||
package main |
||||
|
||||
import ( |
||||
"fmt" |
||||
"context" |
||||
"fmt" |
||||
"jschan/app" |
||||
) |
||||
|
||||
func main() { |
||||
|
||||
jschanClient := jschan.NewClient("https://ptchan.org") |
||||
ctx := context.Background() |
||||
options := &jschan.GetBoardsPublicOptions{ |
||||
Search: "internet", |
||||
Sites: []string{"Test", "TEst2"}, |
||||
options := &jschan.GetOverboardOptions{ |
||||
IncludeDefault: true, |
||||
} |
||||
res, err := jschanClient.GetBoardsPublic(ctx, options) |
||||
|
||||
res, err := jschanClient.GetOverboardCatalog(ctx, options) |
||||
if err != nil { |
||||
fmt.Println("an error occurred") |
||||
return |
||||
} |
||||
if len(res.Boards) > 0 { |
||||
fmt.Printf("Description = %s\n", res.Boards[0].Settings.Description) |
||||
|
||||
if len(res.Threads) > 0 { |
||||
firstThread := res.Threads[0] |
||||
fmt.Printf("Name = %s\n", firstThread.Name) |
||||
fmt.Printf("Message = %s\n", firstThread.Nomarkup) |
||||
} else { |
||||
fmt.Println("no results") |
||||
fmt.Println("No threads") |
||||
} |
||||
|
||||
return |
||||
} |
||||
|
Loading…
Reference in new issue