-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
194 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package fetcher | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/bookstairs/bookhunter/internal/client" | ||
"github.com/bookstairs/bookhunter/internal/sanqiu" | ||
) | ||
|
||
var ( | ||
ErrEmptySanqiu = errors.New("couldn't find available books in sanqiu") | ||
) | ||
|
||
type sanqiuService struct { | ||
config *Config | ||
client *client.Client | ||
} | ||
|
||
func newSanqiuService(config *Config) (service, error) { | ||
// Create the resty client for HTTP handing. | ||
c, err := client.New(config.Config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &sanqiuService{ | ||
config: config, | ||
client: c, | ||
}, nil | ||
} | ||
|
||
func (s *sanqiuService) size() (int64, error) { | ||
resp, err := s.client.R(). | ||
SetQueryParams(map[string]string{ | ||
"orderby": "id", | ||
"order": "desc", | ||
"per_page": "1", | ||
}). | ||
Get("/wp-json/wp/v2/posts") | ||
|
||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
books := make([]sanqiu.BookResp, 0, 1) | ||
err = sanqiu.ParseAPIResponse(resp, books) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
if len(books) < 1 { | ||
return 0, ErrEmptySanqiu | ||
} | ||
|
||
return books[0].ID, nil | ||
} | ||
|
||
func (s *sanqiuService) formats(id int64) (map[Format]string, error) { | ||
// TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (s *sanqiuService) fetch(id int64, format Format, url string) (*fetch, error) { | ||
// TODO implement me | ||
panic("implement me") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,23 @@ | ||
package fetcher | ||
|
||
type telegramService struct { | ||
config *Config | ||
} | ||
|
||
func newTelegramService(config *Config) (service, error) { | ||
return &telegramService{ | ||
config: config, | ||
}, nil | ||
} | ||
|
||
func (s *telegramService) size() (int64, error) { | ||
panic("implement me") | ||
} | ||
|
||
func (s *telegramService) formats(id int64) (map[Format]string, error) { | ||
panic("implement me") | ||
} | ||
|
||
func (s *telegramService) fetch(id int64, format Format, url string) (*fetch, error) { | ||
panic("implement me") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package sanqiu | ||
|
||
import ( | ||
"encoding/json" | ||
"strings" | ||
|
||
"github.com/go-resty/resty/v2" | ||
) | ||
|
||
// ParseAPIResponse will remove the unneeded error str in JSON response and try to parse it. | ||
func ParseAPIResponse(resp *resty.Response, result any) error { | ||
// Remove error messages. | ||
str := resp.String() | ||
str = str[strings.LastIndex(str, "\n")+1:] | ||
decoder := json.NewDecoder(strings.NewReader(str)) | ||
|
||
return decoder.Decode(result) | ||
} | ||
|
||
// BookResp is the response for /wp-json/wp/v2/posts | ||
type BookResp struct { | ||
ID int64 `json:"id"` | ||
Date string `json:"date"` | ||
DateGmt string `json:"date_gmt"` | ||
GUID struct { | ||
Rendered string `json:"rendered"` | ||
} `json:"guid"` | ||
Modified string `json:"modified"` | ||
ModifiedGmt string `json:"modified_gmt"` | ||
Slug string `json:"slug"` | ||
Status string `json:"status"` | ||
Type string `json:"type"` | ||
Link string `json:"link"` | ||
Title struct { | ||
Rendered string `json:"rendered"` | ||
} `json:"title"` | ||
Content struct { | ||
Rendered string `json:"rendered"` | ||
Protected bool `json:"protected"` | ||
} `json:"content"` | ||
Excerpt struct { | ||
Rendered string `json:"rendered"` | ||
Protected bool `json:"protected"` | ||
} `json:"excerpt"` | ||
Author int `json:"author"` | ||
FeaturedMedia int `json:"featured_media"` | ||
CommentStatus string `json:"comment_status"` | ||
PingStatus string `json:"ping_status"` | ||
Sticky bool `json:"sticky"` | ||
Template string `json:"template"` | ||
Format string `json:"format"` | ||
Meta []interface{} `json:"meta"` | ||
Categories []int `json:"categories"` | ||
Tags []int `json:"tags"` | ||
Links struct { | ||
Self []struct { | ||
Href string `json:"href"` | ||
} `json:"self"` | ||
Collection []struct { | ||
Href string `json:"href"` | ||
} `json:"collection"` | ||
About []struct { | ||
Href string `json:"href"` | ||
} `json:"about"` | ||
Author []struct { | ||
Embeddable bool `json:"embeddable"` | ||
Href string `json:"href"` | ||
} `json:"author"` | ||
Replies []struct { | ||
Embeddable bool `json:"embeddable"` | ||
Href string `json:"href"` | ||
} `json:"replies"` | ||
VersionHistory []struct { | ||
Count int `json:"count"` | ||
Href string `json:"href"` | ||
} `json:"version-history"` | ||
WpAttachment []struct { | ||
Href string `json:"href"` | ||
} `json:"wp:attachment"` | ||
WpTerm []struct { | ||
Taxonomy string `json:"taxonomy"` | ||
Embeddable bool `json:"embeddable"` | ||
Href string `json:"href"` | ||
} `json:"wp:term"` | ||
Curies []struct { | ||
Name string `json:"name"` | ||
Href string `json:"href"` | ||
Templated bool `json:"templated"` | ||
} `json:"curies"` | ||
} `json:"_links"` | ||
} |