Add mod flag for /modpost, improve error return and handling slightly, add manage/recent.json and example

master
Thomas Lynch 1 year ago
parent 8fb5f2e3a9
commit 71f2ff6f87
  1. 2
      app/board.go
  2. 24
      app/boardlist.go
  3. 9
      app/jschan.go
  4. 78
      app/manage.go
  5. 7
      app/post.go
  6. 71
      example.go

@ -181,7 +181,7 @@ type GetSettingsOptions struct {
}
type GetSettingsResponse struct {
*models.CustomPage
*models.BoardSettings
}
func (c *Client) GetSettings(ctx context.Context, options *GetSettingsOptions) (*GetSettingsResponse, error) {

@ -8,6 +8,30 @@ import (
"net/url"
)
type GetWebringResponse struct {
*models.Webring
}
func (c *Client) GetWebring(ctx context.Context) (*GetWebringResponse, error) {
url := fmt.Sprintf("%s/webring.json", c.BaseURL)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
res := GetWebringResponse{}
if err := c.sendRequest(req, &res, nil); err != nil {
return nil, err
}
return &res, nil
}
type GetBoardsResponse struct {
Boards []models.Board `json:"boards"`
Page int `json:"page"`

@ -66,18 +66,21 @@ func (c *Client) sendRequest(req *http.Request, v interface{}, h *ReturnHeaders)
req.Header.Set("Csrf-Token", c.CsrfToken)
}
fmt.Printf("%s %s\n", req.Method, req.URL)
res, err := c.HTTPClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode < http.StatusOK || res.StatusCode >= http.StatusBadRequest {
var errRes DynamicResponse
if err = json.NewDecoder(res.Body).Decode(&errRes); err == nil {
return errors.New(errRes.Message)
if err = json.NewDecoder(res.Body).Decode(&errRes); err != nil {
return err
}
return fmt.Errorf("unknown error, status code: %d", res.StatusCode)
return errors.New(errRes.Message)
}
h = &ReturnHeaders{

@ -0,0 +1,78 @@
package jschan
import (
"context"
"fmt"
// "jschan/app/models"
"net/http"
"time"
)
type GetManageRecentOptions struct {
Board string
Global bool
}
type AutoGenerated struct {
ID string `json:"_id"`
Date time.Time `json:"date"`
U int64 `json:"u"`
Name string `json:"name"`
Country interface{} `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 string `json:"userId"`
IP struct {
Raw string `json:"raw"`
Cloak string `json:"cloak"`
Pruned bool `json:"pruned,omitempty"`
} `json:"ip,omitempty"`
Files []interface{} `json:"files"`
Reports []interface{} `json:"reports"`
Quotes []struct {
ID string `json:"_id"`
Thread int `json:"thread"`
PostID int `json:"postId"`
} `json:"quotes"`
Crossquotes []interface{} `json:"crossquotes"`
Backlinks []interface{} `json:"backlinks"`
PostID int `json:"postId"`
Bumped time.Time `json:"bumped,omitempty"`
Replyfiles int `json:"replyfiles,omitempty"`
Replyposts int `json:"replyposts,omitempty"`
Sticky int `json:"sticky,omitempty"`
Locked int `json:"locked,omitempty"`
Bumplocked int `json:"bumplocked,omitempty"`
Cyclic int `json:"cyclic,omitempty"`
}
type GetManageRecentResponse []AutoGenerated
func (c *Client) GetManageRecent(ctx context.Context, options *GetManageRecentOptions) (GetManageRecentResponse, error) {
url := fmt.Sprintf("%s/%s/manage/recent.json", c.BaseURL, options.Board)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
res := GetManageRecentResponse{}
if err := c.sendRequest(req, &res, nil); err != nil {
return nil, err
}
return res, nil
}

@ -27,6 +27,7 @@ type MakePostOptions struct {
StripFilename []string
CustomFlag string
Captcha []string //Array for grid captcha, submitted as single param if len()==1
Mod bool
}
func (c *Client) MakePost(ctx context.Context, options *MakePostOptions) error {
@ -68,7 +69,11 @@ func (c *Client) MakePost(ctx context.Context, options *MakePostOptions) error {
}
writer.Close()
url := fmt.Sprintf("%s/forms/board/%s/post", c.BaseURL, options.Board)
suffix := "post"
if options.Mod {
suffix = "modpost"
}
url := fmt.Sprintf("%s/forms/board/%s/%s", c.BaseURL, options.Board, suffix)
req, err := http.NewRequest(http.MethodPost, url, body)
if err != nil {

@ -5,6 +5,7 @@ import (
"fmt"
"jschan/app"
"jschan/app/models"
"strings"
)
func main() {
@ -84,20 +85,68 @@ func main() {
}
fmt.Printf("Fetched /%s/ logs for date %s with %d entries\n", getLogsOptions.Board, getLogsOptions.Date.String(), len(res5))
getSettingsOptions := &jschan.GetSettingsOptions{
Board: "t",
}
res6, err6 := jschanClient.GetSettings(ctx, getSettingsOptions)
if err6 != nil {
fmt.Println(err6)
return
}
fmt.Printf("Fetched /%s/ settings. Custompages are: [\"%s\"]\n", getLogsOptions.Board, strings.Join(res6.CustomPages, `", "`))
getCustomPageOptions := &jschan.GetCustomPageOptions{
Board: "t",
CustomPage: res6.CustomPages[0],
}
res7, err7 := jschanClient.GetCustomPage(ctx, getCustomPageOptions)
if err7 != nil {
fmt.Println(err7)
return
}
fmt.Printf("Fetched /%s/ custompage \"%s\" published on: %s\n", getCustomPageOptions.Board, getCustomPageOptions.CustomPage, res7.Date.String())
getBannersOptions := &jschan.GetBannersOptions{
Board: "t",
}
res8, err8 := jschanClient.GetBanners(ctx, getBannersOptions)
if err8 != nil {
fmt.Println(err8)
return
}
fmt.Printf("Fetched banners for /%s/: [\"%s\"]\n", getCustomPageOptions.Board, strings.Join(res8, `", "`))
res9, err9 := jschanClient.GetWebring(ctx)
if err9 != nil {
fmt.Println(err9)
return
}
fmt.Printf("Fetched webring, following: [\"%s\"]\n", strings.Join(res9.Following, `", "`))
// makePostOptions := &jschan.MakePostOptions{
// Board: "test",
// Thread: 277,
// Name: "Test",
// Message: ">test",
// Email: "sage",
// PostPassword: "123",
// Files: []string{"./image.png", "./image.png"},
// Board: "test",
// Thread: 277,
// Name: "Test",
// Message: ">test",
// Email: "sage",
// PostPassword: "123",
// Files: []string{"./image.png", "./image.png"},
// }
// err6 := jschanClient.MakePost(ctx, makePostOptions)
// if err6 != nil {
// fmt.Println(err)
// return
// err10 := jschanClient.MakePost(ctx, makePostOptions)
// if err10 != nil {
// fmt.Println(err10)
// return
// }
getManageRecentOptions := &jschan.GetManageRecentOptions{
Board: "test",
}
res11, err11 := jschanClient.GetManageRecent(ctx, getManageRecentOptions)
if err11 != nil {
fmt.Println(err11)
return
}
fmt.Printf("Fetched %d manage recent posts for /%s/\n", len(res11), getManageRecentOptions.Board)
return
}

Loading…
Cancel
Save