diff --git a/app/board.go b/app/board.go index cbb1620..8cc2a17 100644 --- a/app/board.go +++ b/app/board.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) { diff --git a/app/boardlist.go b/app/boardlist.go index fa56d2a..d6e4b8a 100644 --- a/app/boardlist.go +++ b/app/boardlist.go @@ -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"` diff --git a/app/jschan.go b/app/jschan.go index fe86ccf..f416ed6 100644 --- a/app/jschan.go +++ b/app/jschan.go @@ -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{ diff --git a/app/manage.go b/app/manage.go new file mode 100644 index 0000000..a8966a6 --- /dev/null +++ b/app/manage.go @@ -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 + +} diff --git a/app/post.go b/app/post.go index 84e26b2..04fcccb 100644 --- a/app/post.go +++ b/app/post.go @@ -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 { diff --git a/example.go b/example.go index 85bd488..bf7306d 100644 --- a/example.go +++ b/example.go @@ -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 }