-
Notifications
You must be signed in to change notification settings - Fork 26
/
broker.go
191 lines (162 loc) · 4.14 KB
/
broker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package main
import (
"fmt"
"net/http"
"os"
"path/filepath"
"time"
"github.com/motemen/go-wsse"
"github.com/x-motemen/blogsync/atom"
)
type broker struct {
*atom.Client
*blogConfig
}
func newBroker(bc *blogConfig) *broker {
return &broker{
Client: &atom.Client{
Client: &http.Client{
Transport: &wsse.Transport{
Username: bc.Username,
Password: bc.Password,
},
},
},
blogConfig: bc,
}
}
func (b *broker) FetchRemoteEntries() ([]*entry, error) {
entries := []*entry{}
fixedPageURL := fixedPageEndpointURL(b.blogConfig)
urls := []string{
entryEndPointUrl(b.blogConfig),
fixedPageURL,
}
for url := ""; true; {
if url == "" {
if len(urls) == 0 {
break
}
url, urls = urls[0], urls[1:]
}
feed, err := b.Client.GetFeed(url)
if err != nil {
if url == fixedPageURL {
// Ignore errors in the case of fixed pages, because fixed page is the feature
// only for pro users.
break
}
return nil, err
}
for _, ae := range feed.Entries {
e, err := entryFromAtom(&ae)
if err != nil {
return nil, err
}
entries = append(entries, e)
}
nextLink := feed.Links.Find("next")
if nextLink != nil {
url = nextLink.Href
} else {
url = ""
}
}
return entries, nil
}
func (b *broker) LocalPath(e *entry) string {
extension := ".md" // TODO regard re.ContentType
paths := []string{b.LocalRoot}
if b.OmitDomain == nil || !*b.OmitDomain {
paths = append(paths, b.BlogID)
}
// If possible, for fixed pages, we would like to dig a directory such as page/ to place md files,
// but it is difficult to solve by a simple method such as prepending a "page/" string
// if the path does not begin with an entry string. That is because if you are operating
// a subdirectory in Hatena Blog Media, you do not know where the root of the blog is.
// e.g.
// - https://example.com/subblog/entry/blog-entry
// - https://example.com/subblog/fixed-page
paths = append(paths, e.URL.Path+extension)
return filepath.Join(paths...)
}
func (b *broker) StoreFresh(e *entry, path string) (bool, error) {
var localLastModified time.Time
if fi, err := os.Stat(path); err == nil {
localLastModified = fi.ModTime()
}
if e.LastModified.After(localLastModified) {
logf("fresh", "remote=%s > local=%s", e.LastModified, localLastModified)
if err := b.Store(e, path, ""); err != nil {
return false, err
}
return true, nil
}
return false, nil
}
func (b *broker) Store(e *entry, path, origPath string) error {
logf("store", "%s", path)
dir := filepath.Dir(path)
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
if err := os.WriteFile(path, []byte(e.fullContent()), 0666); err != nil {
return err
}
if err := os.Chtimes(path, *e.LastModified, *e.LastModified); err != nil {
return err
}
if origPath != "" && path != origPath {
return os.Remove(origPath)
}
return nil
}
func (b *broker) UploadFresh(e *entry) (bool, error) {
re, err := asEntry(b.Client.GetEntry(e.EditURL))
if err != nil {
return false, err
}
if e.LastModified.After(*re.LastModified) == false {
return false, nil
}
return true, b.PutEntry(e)
}
func (b *broker) PutEntry(e *entry) error {
newEntry, err := asEntry(b.Client.PutEntry(e.EditURL, e.atom()))
if err != nil {
return err
}
if e.CustomPath != "" {
newEntry.CustomPath = e.CustomPath
}
return b.Store(newEntry, b.LocalPath(newEntry), b.LocalPath(e))
}
func (b *broker) PostEntry(e *entry, isPage bool) error {
var endPoint string
if !isPage {
endPoint = entryEndPointUrl(b.blogConfig)
} else {
endPoint = fixedPageEndpointURL(b.blogConfig)
}
newEntry, err := asEntry(b.Client.PostEntry(endPoint, e.atom()))
if err != nil {
return err
}
if e.CustomPath != "" {
newEntry.CustomPath = e.CustomPath
}
return b.Store(newEntry, b.LocalPath(newEntry), "")
}
func atomEndpointURLRoot(bc *blogConfig) string {
owner := bc.Owner
if owner == "" {
owner = bc.Username
}
return fmt.Sprintf("https://blog.hatena.ne.jp/%s/%s/atom/", owner, bc.BlogID)
}
func entryEndPointUrl(bc *blogConfig) string {
return atomEndpointURLRoot(bc) + "entry"
}
func fixedPageEndpointURL(bc *blogConfig) string {
return atomEndpointURLRoot(bc) + "page"
}