Catalogs, threads, parse json array response of threads (For catalog), ahndle scuffed time format for bumped/date/edited

master
Thomas Lynch 2 years ago
parent c86ef11ff4
commit 5418bc6479
  1. 36
      app/board.go
  2. 13
      app/jschan.go
  3. 20
      app/models/post.go
  4. 37
      app/thread.go
  5. 20
      example.go

@ -0,0 +1,36 @@
package jschan
import (
"context"
"fmt"
"jschan/app/models"
"net/http"
)
type GetCatalogOptions struct {
Board string
}
type CatalogResponse []models.Post
func (c *Client) GetCatalog(ctx context.Context, options *GetCatalogOptions) (CatalogResponse, error) {
url := fmt.Sprintf("%s/%s/catalog.json", c.BaseURL, options.Board)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
res := CatalogResponse{}
if err := c.sendRequest(req, &res, nil); err != nil {
return nil, err
}
return res, nil
}
//TODO: GetIndex, GetLogs, etc

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"net/http" "net/http"
"time" "time"
) )
@ -92,8 +93,16 @@ func (c *Client) sendRequest(req *http.Request, v interface{}, h *ReturnHeaders)
if v != nil { if v != nil {
fullResponse := v fullResponse := v
if err = json.NewDecoder(res.Body).Decode(&fullResponse); err != nil { err := json.NewDecoder(res.Body).Decode(&fullResponse)
return err if err != nil {
body, err2 := ioutil.ReadAll(res.Body)
if err2 != nil {
return err
}
err3 := json.Unmarshal(body, &fullResponse)
if err3 != nil {
return err
}
} }
} }

@ -1,12 +1,22 @@
package models package models
import ( import (
"encoding/json"
"time" "time"
) )
type ScuffedTime time.Time
func (m *ScuffedTime) UnmarshalJSON(data []byte) error {
if string(data) == "null" || string(data) == `""` || string(data) == "0" {
return nil
}
return json.Unmarshal(data, (*time.Time)(m))
}
type Post struct { type Post struct {
ID string `json:"_id"` ID string `json:"_id"`
Date time.Time `json:"date"` Date ScuffedTime `json:"date"`
U int64 `json:"u"` U int64 `json:"u"`
Name string `json:"name"` Name string `json:"name"`
Country interface{} `json:"country"` Country interface{} `json:"country"`
@ -32,9 +42,9 @@ type Post struct {
Locked int `json:"locked"` Locked int `json:"locked"`
Bumplocked int `json:"bumplocked"` Bumplocked int `json:"bumplocked"`
Cyclic int `json:"cyclic"` Cyclic int `json:"cyclic"`
Bumped time.Time `json:"bumped"` Bumped ScuffedTime `json:"bumped,omitempty"`
PostID int `json:"postId"` PostID int `json:"postId"`
Replies []Post `json:"replies"` Replies []Post `json:"replies,omitempty"`
Previewbacklinks []interface{} `json:"previewbacklinks,omitempty"` Previewbacklinks []interface{} `json:"previewbacklinks,omitempty"`
Omittedfiles int `json:"omittedfiles,omitempty"` Omittedfiles int `json:"omittedfiles,omitempty"`
Omittedposts int `json:"omittedposts,omitempty"` Omittedposts int `json:"omittedposts,omitempty"`
@ -82,6 +92,6 @@ type Backlinks struct {
} }
type Edited struct { type Edited struct {
Username string `json:"username"` Username string `json:"username"`
Date time.Time `json:"date"` Date ScuffedTime `json:"date"`
} }

@ -0,0 +1,37 @@
package jschan
import (
"context"
"fmt"
"jschan/app/models"
"net/http"
)
type GetThreadOptions struct {
Board string
ThreadId int
}
type GetThreadResponse struct {
*models.Post
}
func (c *Client) GetThread(ctx context.Context, options *GetThreadOptions) (*GetThreadResponse, error) {
url := fmt.Sprintf("%s/%s/thread/%d.json", c.BaseURL, options.Board, options.ThreadId)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
res := GetThreadResponse{}
if err := c.sendRequest(req, &res, nil); err != nil {
return nil, err
}
return &res, nil
}

@ -19,7 +19,10 @@ func main() {
err := jschanClient.Login(ctx, loginOptions) err := jschanClient.Login(ctx, loginOptions)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return //return
}
if jschanClient.SessionCookie != "" {
fmt.Printf("Logged in as user %s\n", loginOptions.Username)
} }
overboardOptions := &jschan.GetOverboardOptions{ overboardOptions := &jschan.GetOverboardOptions{
@ -30,14 +33,17 @@ func main() {
fmt.Println(err) fmt.Println(err)
return return
} }
fmt.Printf("Fetched overboard catalog with %d threads\n", len(res.Threads))
if len(res.Threads) > 0 { getCatalogOptions := &jschan.GetCatalogOptions{
firstThread := res.Threads[0] Board: "t",
fmt.Printf("Name = %s\n", firstThread.Name) }
fmt.Printf("Message = %s\n", firstThread.Nomarkup) res2, err2 := jschanClient.GetCatalog(ctx, getCatalogOptions)
} else { if err2 != nil {
fmt.Println("No threads") fmt.Println(err2)
return
} }
fmt.Printf("Fetched board %s catalog with %d threads\n", getCatalogOptions.Board, len(res2))
return return
} }

Loading…
Cancel
Save