-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
157 lines (140 loc) · 4.25 KB
/
main.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
package main
import (
"bytes"
"encoding/json"
"encoding/xml"
"flag"
"fmt"
"log"
"net/http"
"net/url"
"os"
"time"
"github.com/pelletier/go-toml/v2"
"golang.org/x/tools/blog/atom"
)
type NotePayload struct {
AccessToken string `json:"i"`
Visibility string `json:"visibility"`
VisibleUserIds []string `json:"visibleUserIds,omitempty"`
Text string `json:"text"`
CW *string `json:"cw,omitempty"`
LocalOnly bool `json:"localOnly"`
NoExtractMentions bool `json:"noExtractMentions"`
NoExtractHashtags bool `json:"noExtractHashtags"`
NoExtractEmojis bool `json:"noExtractEmojis"`
Poll *interface{} `json:"poll,omitempty"`
FileIds *[]string `json:"fileIds,omitempty"`
MediaIds *[]string `json:"mediaIds,omitempty"`
RenoteId *[]string `json:"renoteId,omitempty"`
ChannelId *[]string `json:"channelId,omitempty"`
}
type UsersNotesRequestPayload struct {
UserId string `json:"userId"`
WithReplies bool `json:"withReplies"`
WithRenotes bool `json:"withRenotes"`
Limit int `json:"limit"`
}
type UsersNotesResponsePayload struct {
Id string `json:"id"`
CreatedAt string `json:"createdAt"`
}
type MisskeyConfig struct {
AccessToken string `toml:"token"`
URL string `toml:"url"`
UserId string `toml:"userId"`
}
type Config struct {
Misskey MisskeyConfig `toml:"misskey"`
}
func main() {
var (
config_file_path = flag.String("config", "config.toml", "config file's path")
)
config_file, err := os.Open(*config_file_path)
if err != nil {
log.Fatalf("config error: %v\n", err)
}
config := new(Config)
if err := toml.NewDecoder(config_file).Decode(config); err != nil {
log.Fatalf("config error: %v\n", err)
}
server_url := new(url.URL)
server_url.Host = config.Misskey.URL
server_url.Scheme = "https"
w := new(bytes.Buffer)
userNotesReqPayload := &UsersNotesRequestPayload{
UserId: config.Misskey.UserId,
WithReplies: false,
WithRenotes: false,
Limit: 1,
}
if err = json.NewEncoder(w).Encode(userNotesReqPayload); err != nil {
log.Fatalf("time err: %v\n", err)
return
}
server_url.Path = "/api/users/notes"
client := new(http.Client)
resp, err := client.Post(server_url.String(), "application/json", w)
if err != nil {
log.Fatalf("user data fetch error: %s\n", resp.Status)
return
}
body := make([]UsersNotesResponsePayload, 1)
if err = json.NewDecoder(resp.Body).Decode(&body); err != nil {
log.Fatalf("decode err: %v\n", err)
return
}
resp, err = client.Get("https://mkobayashime.github.io/twins-announcements/twins-announcements-atom1.xml")
if err != nil {
log.Fatalf("feed error: %s\n", resp.Status)
}
var feed atom.Feed
if err = xml.NewDecoder(resp.Body).Decode(&feed); err != nil {
log.Fatalf("feed read error: %v\n", err)
}
accountLastUpdate := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
if len(body) != 0 {
accountLastUpdate, err = time.Parse(time.RFC3339, body[0].CreatedAt)
if err != nil {
log.Fatalf("time.parse() last_update err: %v\n", err)
}
}
feed_updated, err := time.Parse(time.RFC3339Nano, string(feed.Updated))
if err != nil {
log.Fatalf("time.parse() feed_updated err: %v\n", err)
}
if !accountLastUpdate.Before(feed_updated) {
log.Println("found no new article")
return
}
var entryUpdated time.Time
for _, entry := range feed.Entry {
entryUpdated, err = time.Parse(time.RFC3339Nano, string(entry.Updated))
if err == nil && !accountLastUpdate.Before(entryUpdated) {
continue
}
payload := new(NotePayload)
payload.Visibility = "public"
payload.AccessToken = config.Misskey.AccessToken
payload.Text = fmt.Sprintf("%s\n%s\n", entry.Title, entry.Link[0].Href)
w := new(bytes.Buffer)
if err != json.NewEncoder(w).Encode(payload) {
log.Fatalf("time err: %v\n", err)
os.Exit(1)
return
}
server_url.Path = "/api/notes/create"
resp, err := client.Post(server_url.String(), "application/json", w)
if err != nil {
log.Fatalf("misskey err: %v. id: %s\n", err, entry.ID)
os.Exit(1)
return
}
if resp.StatusCode != http.StatusOK {
log.Fatalf("misskey err: %v. id: %s. status: %d\n", err, entry.ID, resp.StatusCode)
os.Exit(1)
return
}
}
}