mirror of
https://github.com/Xevion/todoist-late-reset.git
synced 2025-12-14 12:13:24 -06:00
Add essential request parsing, get scheduling and task reading working
This commit is contained in:
47
api.go
47
api.go
@@ -1,12 +1,35 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getRecentlyCompleted() (string, error) {
|
var (
|
||||||
|
syncToken string
|
||||||
|
)
|
||||||
|
|
||||||
|
type ActivityLog struct {
|
||||||
|
Count int `json:"count"`
|
||||||
|
Events []Event `json:"events"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Event struct {
|
||||||
|
EventDate time.Time `json:"event_date"`
|
||||||
|
EventType string `json:"event_type"`
|
||||||
|
ExtraData map[string]any `json:"extra_data"`
|
||||||
|
ExtraDataID int64 `json:"extra_data_id"`
|
||||||
|
ID int64 `json:"id"`
|
||||||
|
|
||||||
|
InitiatorID *int64 `json:"initiator_id"`
|
||||||
|
ObjectID string `json:"object_id"`
|
||||||
|
ObjectType string `json:"object_type"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func getRecentlyCompleted() (*ActivityLog, error) {
|
||||||
baseURL := "https://api.todoist.com/sync/v9/activity/get"
|
baseURL := "https://api.todoist.com/sync/v9/activity/get"
|
||||||
params := url.Values{}
|
params := url.Values{}
|
||||||
params.Add("event_type", "completed")
|
params.Add("event_type", "completed")
|
||||||
@@ -15,21 +38,35 @@ func getRecentlyCompleted() (string, error) {
|
|||||||
|
|
||||||
req, err := http.NewRequest("GET", fullURL, nil)
|
req, err := http.NewRequest("GET", fullURL, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Set("Authorization", "Bearer "+todoistApiToken)
|
req.Header.Set("Authorization", "Bearer "+todoistApiToken)
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
body, err := io.ReadAll(resp.Body)
|
body, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return string(body), nil
|
var activityLog *ActivityLog
|
||||||
|
if err := json.Unmarshal(body, &activityLog); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return activityLog, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// $ curl https://api.todoist.com/sync/v9/sync \
|
||||||
|
// -H "Authorization: Bearer 0123456789abcdef0123456789abcdef01234567" \
|
||||||
|
// -d sync_token='*' \
|
||||||
|
// -d resource_types='["all"]'
|
||||||
|
|
||||||
|
func synchronize() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
69
main.go
69
main.go
@@ -15,13 +15,12 @@ import (
|
|||||||
|
|
||||||
"github.com/go-co-op/gocron/v2"
|
"github.com/go-co-op/gocron/v2"
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
"github.com/redis/go-redis/v9"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
todoistApiToken = os.Getenv("TODOIST_API_TOKEN")
|
todoistApiToken string
|
||||||
redisURL = os.Getenv("REDIS_URL")
|
redisURL string
|
||||||
cronSchedule = os.Getenv("CRON_SCHEDULE")
|
cronSchedule string
|
||||||
client = &http.Client{}
|
client = &http.Client{}
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -33,34 +32,72 @@ func init() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func primary() error {
|
||||||
|
log, err := getRecentlyCompleted()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error getting recently completed tasks:", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
now := time.Now()
|
||||||
|
startTime := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
|
||||||
|
endTime := startTime.Add(3 * time.Hour)
|
||||||
|
|
||||||
|
for _, event := range log.Events {
|
||||||
|
if event.EventDate.After(startTime) && event.EventDate.Before(endTime) {
|
||||||
|
fmt.Printf("%s - %s\n", event.EventDate.Format("Monday, January 2, 3:04 PM"), event.ExtraData["content"])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
opt, _ := redis.ParseURL(redisURL)
|
todoistApiToken = os.Getenv("TODOIST_API_KEY")
|
||||||
client := redis.NewClient(opt)
|
redisURL = os.Getenv("REDIS_URL")
|
||||||
|
cronSchedule = os.Getenv("CRON_SCHEDULE")
|
||||||
|
|
||||||
|
// opt, _ := redis.ParseURL(redisURL)
|
||||||
|
// client := redis.NewClient(opt)
|
||||||
|
|
||||||
// create a scheduler
|
// create a scheduler
|
||||||
s, err := gocron.NewScheduler()
|
s, err := gocron.NewScheduler()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO: handle error
|
fmt.Println("Error creating scheduler:", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// add a job to the scheduler
|
// add a job to the scheduler
|
||||||
_, err = s.NewJob(
|
j, err := s.NewJob(
|
||||||
gocron.CronJob(cronSchedule, false),
|
gocron.CronJob(cronSchedule, false),
|
||||||
gocron.NewTask(
|
gocron.NewTask(primary),
|
||||||
func(a string, b int) {
|
|
||||||
fmt.Println("Current time:", time.Now().Format(time.RFC3339))
|
|
||||||
},
|
|
||||||
"hello",
|
|
||||||
1,
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// handle error
|
fmt.Println("Error adding job to scheduler:", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start the scheduler
|
// Start the scheduler
|
||||||
s.Start()
|
s.Start()
|
||||||
|
|
||||||
|
nextRun, err := j.NextRun()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error getting next run time:", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
durationUntilNextRun := time.Until(nextRun).Seconds()
|
||||||
|
fmt.Printf("startup: next run in %.2f seconds: %v\n", durationUntilNextRun, nextRun.Format(time.RFC3339))
|
||||||
|
|
||||||
|
if durationUntilNextRun > 1 {
|
||||||
|
// Run the job immediately
|
||||||
|
err = j.RunNow()
|
||||||
|
fmt.Println("startup: running job immediately")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error running job immediately:", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Setup signal handler channel
|
// Setup signal handler channel
|
||||||
stop := make(chan os.Signal, 1)
|
stop := make(chan os.Signal, 1)
|
||||||
signal.Notify(stop, os.Interrupt) // Ctrl+C signal
|
signal.Notify(stop, os.Interrupt) // Ctrl+C signal
|
||||||
|
|||||||
Reference in New Issue
Block a user