add GetIndex, GetLogsList, GetLogs and associated models

ensure LogDate has String() method for converting to string with zero padded y, m, d
add/update README
master
Thomas Lynch 2 years ago
parent 5418bc6479
commit 4b5a75477a
  1. 37
      README.md
  2. 88
      app/board.go
  3. 37
      app/models/log.go
  4. 37
      example.go

@ -0,0 +1,37 @@
# jschan-api-go
API client for jschan imageboard software written in go.
jschan repo: [fatchan/jschan](https://gitgud.io/fatchan/jschan/)
API docs repo: [fatchan/jschan-docs](https://gitgud.io/fatchan/jschan-docs/) ([gitgud pages](http://fatchan.gitgud.site/jschan-docs/#introduction))
## License
GNU LGPLv3, see [LICENSE](LICENSE).
## Example
```go
getThreadOptions := &jschan.GetThreadOptions{
Board: "t",
ThreadId: 1392,
}
res3, err3 := jschanClient.GetThread(ctx, getThreadOptions)
if err3 != nil {
fmt.Println(err3)
return
}
fmt.Printf("Fetched /%s/ thread %d with %d replies\n", getThreadOptions.Board, getThreadOptions.ThreadId, len(res3.Replies))
```
## For generous people
Bitcoin (BTC):
[`bc1q4elrlz5puak4m9xy3hfvmpempnpqpu95v8s9m6`](bitcoin:bc1q4elrlz5puak4m9xy3hfvmpempnpqpu95v8s9m6)
Monero (XMR):
[`89J9DXPLUBr5HjNDNZTEo4WYMFTouSsGjUjBnUCCUxJGUirthnii4naZ8JafdnmhPe4NP1nkWsgcK82Uga7X515nNR1isuh`](monero:89J9DXPLUBr5HjNDNZTEo4WYMFTouSsGjUjBnUCCUxJGUirthnii4naZ8JafdnmhPe4NP1nkWsgcK82Uga7X515nNR1isuh)
Oxen (OXEN):
`LBjExqjDKCFT6Tj198CfK8auAzBERJX1ogtcsjuKZ6AYWTFxwEADLgf2zZ8NHvWCa1UW7vrtY8DJmPYFpj3MEE69CryCvN6`

@ -5,6 +5,7 @@ import (
"fmt"
"jschan/app/models"
"net/http"
"strconv"
)
type GetCatalogOptions struct {
@ -33,4 +34,89 @@ func (c *Client) GetCatalog(ctx context.Context, options *GetCatalogOptions) (Ca
}
//TODO: GetIndex, GetLogs, etc
type GetIndexOptions struct {
Board string
Page int
}
type GetIndexResponse struct {
*models.Post
}
func (c *Client) GetIndex(ctx context.Context, options *GetIndexOptions) (*GetIndexResponse, error) {
pageString := strconv.Itoa(options.Page)
if pageString == "1" {
pageString = "index"
}
url := fmt.Sprintf("%s/%s/%s.json", c.BaseURL, options.Board, pageString)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
res := GetIndexResponse{}
if err := c.sendRequest(req, &res, nil); err != nil {
return nil, err
}
return &res, nil
}
type GetLogsListOptions struct {
Board string
}
type GetLogsListResponse []models.LogList
func (c *Client) GetLogsList(ctx context.Context, options *GetLogsListOptions) (GetLogsListResponse, error) {
url := fmt.Sprintf("%s/%s/logs.json", c.BaseURL, options.Board)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
res := GetLogsListResponse{}
if err := c.sendRequest(req, &res, nil); err != nil {
return nil, err
}
return res, nil
}
type GetLogsOptions struct {
Board string
Date models.LogDate
}
type GetLogsResponse []models.Log
func (c *Client) GetLogs(ctx context.Context, options *GetLogsOptions) (GetLogsResponse, error) {
url := fmt.Sprintf("%s/%s/logs/%s.json", c.BaseURL, options.Board, options.Date.String())
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
res := GetLogsResponse{}
if err := c.sendRequest(req, &res, nil); err != nil {
return nil, err
}
return res, nil
}

@ -0,0 +1,37 @@
package models
import (
"fmt"
)
type LogList struct {
Date LogDate `json:"date"`
Count int `json:"count"`
}
type Log struct {
ID string `json:"_id"`
ShowLinks bool `json:"showLinks"`
PostLinks []LogPostLinks `json:"postLinks"`
Actions []string `json:"actions"`
Date ScuffedTime `json:"date"`
ShowUser bool `json:"showUser"`
Message string `json:"message"`
User string `json:"user"`
Board string `json:"board"`
}
type LogPostLinks struct {
PostID int `json:"postId"`
Thread interface{} `json:"thread"`
}
type LogDate struct {
Year int `json:"year"`
Month int `json:"month"`
Day int `json:"day"`
}
func (m *LogDate) String() string {
return fmt.Sprintf("%02d-%02d-%02d", m.Month, m.Day, m.Year)
}

@ -4,6 +4,7 @@ import (
"context"
"fmt"
"jschan/app"
"jschan/app/models"
)
func main() {
@ -45,5 +46,41 @@ func main() {
}
fmt.Printf("Fetched board %s catalog with %d threads\n", getCatalogOptions.Board, len(res2))
getThreadOptions := &jschan.GetThreadOptions{
Board: "t",
ThreadId: 1392,
}
res3, err3 := jschanClient.GetThread(ctx, getThreadOptions)
if err3 != nil {
fmt.Println(err3)
return
}
fmt.Printf("Fetched /%s/ thread %d with %d replies\n", getThreadOptions.Board, getThreadOptions.ThreadId, len(res3.Replies))
getLogsListOptions := &jschan.GetLogsListOptions{
Board: "t",
}
res4, err4 := jschanClient.GetLogsList(ctx, getLogsListOptions)
if err4 != nil {
fmt.Println(err4)
return
}
fmt.Printf("Fetched /%s/ logs with %d entries\n", getLogsListOptions.Board, len(res4))
getLogsOptions := &jschan.GetLogsOptions{
Board: "t",
Date: models.LogDate{
Year: 2022,
Month: 11,
Day: 02,
},
}
res5, err5 := jschanClient.GetLogs(ctx, getLogsOptions)
if err5 != nil {
fmt.Println(err5)
return
}
fmt.Printf("Fetched /%s/ logs for date %s with %d entries\n", getLogsOptions.Board, getLogsOptions.Date.String(), len(res5))
return
}

Loading…
Cancel
Save