mirror of
https://github.com/by-jp/www.byjp.me.git
synced 2025-08-09 01:35:56 +01:00
Refactor
This commit is contained in:
parent
00283bec87
commit
f0499ba2ed
3 changed files with 22 additions and 17 deletions
|
@ -6,21 +6,21 @@ import (
|
|||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/by-jp/www.byjp.me/tools/syndicate/poster"
|
||||
"github.com/by-jp/www.byjp.me/tools/syndicate/shared"
|
||||
"github.com/mmcdole/gofeed"
|
||||
)
|
||||
|
||||
type toPostMap map[shared.SyndicationID]shared.Post
|
||||
type toBackfeedMap map[string]string
|
||||
|
||||
func parseFeed(urlToPath func(string) string, feedReader io.Reader, tagMatcher *regexp.Regexp, syndicationMatchers map[string]*regexp.Regexp) ([]string, toPostMap, toBackfeedMap, error) {
|
||||
func parseFeed(urlToPath func(string) string, feedReader io.Reader, tagMatcher *regexp.Regexp, syndicationMatchers map[string]*regexp.Regexp) ([]string, poster.ToPostList, toBackfeedMap, error) {
|
||||
fp := gofeed.NewParser()
|
||||
feed, err := fp.Parse(feedReader)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
|
||||
toPost := make(toPostMap)
|
||||
toPost := make(poster.ToPostList)
|
||||
toBackfeed := make(toBackfeedMap)
|
||||
services := make(map[string]struct{})
|
||||
|
||||
|
|
|
@ -43,12 +43,10 @@ func main() {
|
|||
}
|
||||
|
||||
fmt.Fprintf(os.Stderr, "Found %d new syndications to post in %s\n", len(toPost), feed)
|
||||
for k, p := range toPost {
|
||||
if err := pstr.Post(k, p); err == nil {
|
||||
fmt.Printf("Posted '%s' to %s: %s\n", p.Title, k.Source, pstr.PostedURL(k))
|
||||
} else {
|
||||
fmt.Fprintf(os.Stderr, "Couldn't post %s to %s: %v\n", p.URL, k.Source, err)
|
||||
}
|
||||
posted, err := pstr.PostAll(toPost)
|
||||
_ = posted
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Couldn't post syndications: %v\n", err)
|
||||
}
|
||||
|
||||
for _, fname := range cfg.content {
|
||||
|
|
|
@ -11,6 +11,8 @@ import (
|
|||
"github.com/by-jp/www.byjp.me/tools/syndicate/shared"
|
||||
)
|
||||
|
||||
type ToPostList map[shared.SyndicationID]shared.Post
|
||||
|
||||
type poster struct {
|
||||
services *services.List
|
||||
done map[shared.SyndicationID]string
|
||||
|
@ -23,6 +25,19 @@ func New(services *services.List) *poster {
|
|||
}
|
||||
}
|
||||
|
||||
func (p *poster) PostAll(toPost ToPostList) (map[string]string, error) {
|
||||
posted := make(map[string]string)
|
||||
for sid, post := range toPost {
|
||||
if err := p.Post(sid, post); err == nil {
|
||||
posted[post.URL] = p.done[sid]
|
||||
} else {
|
||||
return posted, fmt.Errorf("couldn't post %s to %s: %w", post.URL, sid.Source, err)
|
||||
}
|
||||
}
|
||||
|
||||
return posted, nil
|
||||
}
|
||||
|
||||
func (p *poster) Post(sid shared.SyndicationID, post shared.Post) error {
|
||||
if _, ok := p.done[sid]; ok {
|
||||
return nil
|
||||
|
@ -38,14 +53,6 @@ func (p *poster) Post(sid shared.SyndicationID, post shared.Post) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (p *poster) PostedURL(sid shared.SyndicationID) string {
|
||||
if url, ok := p.done[sid]; ok {
|
||||
return url
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func (p *poster) ReplaceReferences(fname string, tagMatcher *regexp.Regexp) error {
|
||||
f, err := os.ReadFile(fname)
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in a new issue