/
rss-retriver.go
119 lines (93 loc) · 3.07 KB
/
rss-retriver.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
package workflows
import (
"context"
"fmt"
"log"
"time"
"github.com/abserari/golangbot/pkg/rss"
"go.temporal.io/sdk/worker"
"go.temporal.io/sdk/workflow"
)
func RssWorker() {
go func() {
w := worker.New(C, "cron", worker.Options{})
w.RegisterWorkflow(CronRssFeedWorkflow)
err = w.Run(worker.InterruptCh())
if err != nil {
log.Fatalln("Unable to start worker", err)
}
}()
}
// CronResult is used to return data from one cron run to the next
type CronResult struct {
RunTime time.Time
Etag string
LastModified time.Time
}
// SampleCronWorkflow executes on the given schedule
// The schedule is provided when starting the Workflow
func CronRssFeedWorkflow(ctx workflow.Context) (*CronResult, error) {
workflow.GetLogger(ctx).Info("Cron workflow started.", "StartTime", workflow.Now(ctx))
ao := workflow.ActivityOptions{
StartToCloseTimeout: 10 * time.Second,
}
ctx1 := workflow.WithActivityOptions(ctx, ao)
// Start from 0 for first cron job
lastRunTime := time.Time{}
lastEtag := ""
lastModefiedTime := time.Time{}
// Check to see if there was a previous cron job
if workflow.HasLastCompletionResult(ctx) {
var lastResult CronResult
if err := workflow.GetLastCompletionResult(ctx, &lastResult); err == nil {
lastRunTime = lastResult.RunTime
lastEtag = lastResult.Etag
lastModefiedTime = lastResult.LastModified
}
}
thisRunTime := workflow.Now(ctx)
var feed rss.Feed
lao := workflow.LocalActivityOptions{
StartToCloseTimeout: 10 * time.Second,
RetryPolicy: NoRetry,
}
ctx1 = workflow.WithLocalActivityOptions(ctx1, lao)
err := workflow.ExecuteLocalActivity(ctx1, RssFeed, lastRunTime, thisRunTime, lastModefiedTime, lastEtag).Get(ctx, &feed)
if err != nil {
// Cron job failed
// Next cron will still be scheduled by the Server
workflow.GetLogger(ctx).Error("Cron job failed.", "Error", err)
return nil, err
}
// not modified
if feed.ETag == lastEtag {
return &CronResult{RunTime: thisRunTime, Etag: feed.ETag, LastModified: feed.LastModified}, nil
}
var url string
var Localexecutor *TgBot = GetTgBot()
err = workflow.ExecuteLocalActivity(ctx1, Localexecutor.SendMessageToTelegraph, PrintFeedToTelegraph(feed)).Get(ctx, &url)
if err != nil {
workflow.GetLogger(ctx).Error("PrintFeedToTelegraph failed.", "Error", err)
return nil, err
}
workflow.GetLogger(ctx).Info("Success Create Telegraph URL:", url)
return &CronResult{RunTime: thisRunTime, Etag: feed.ETag, LastModified: feed.LastModified}, nil
}
// DoSomething is an Activity
func RssFeed(ctx context.Context, lastRunTime, thisRunTime, lastmodified time.Time, etag string) (*rss.Feed, error) {
return rss.ReadFeedWithContext("https://yusank.space/index.xml", "", time.Time{}, ctx)
}
func PrintFeedToTelegraph(feed rss.Feed) string {
header := `<figure>
<img src="/file/6a5b15e7eb4d7329ca7af.jpg"/>
</figure>
<p><i>Hello</i>, my name is <b>Tech Cats</b>, <u>look at me</u>!</p>
`
article := header
for _, v := range feed.Items {
pub := fmt.Sprintf(`<a href="%s">%s</a>
<p>%s</p>`, v.Link, v.Title, v.Description)
article += pub
}
return article
}