use account data
This commit is contained in:
parent
5926c09585
commit
fe28bef7b9
19
nun.go
19
nun.go
@ -20,7 +20,7 @@ func newNunWatch(stop chan bool, c *mautrix.Client) *nunWatch {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (n *nunWatch) Main() {
|
func (n *nunWatch) Main() {
|
||||||
ticker := time.NewTicker(30 * time.Second)
|
ticker := time.NewTicker(6 * time.Second)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
@ -45,11 +45,17 @@ func (n *nunWatch) Main() {
|
|||||||
}
|
}
|
||||||
if n.fail != 0 {
|
if n.fail != 0 {
|
||||||
n.fail = 0
|
n.fail = 0
|
||||||
ticker.Reset(30 * time.Second)
|
ticker.Reset(60 * time.Second)
|
||||||
}
|
}
|
||||||
|
|
||||||
if n.curPost.Title != newPost.Title {
|
if n.curPost.Title != newPost.Title {
|
||||||
n.curPost = newPost
|
n.curPost = newPost
|
||||||
|
var cur post
|
||||||
|
err := n.client.GetAccountData("nun.newest", &cur)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("tried to get saved account data but failed %v", err)
|
||||||
|
cur.Number = 0
|
||||||
|
}
|
||||||
roomResp, err := n.client.JoinedRooms()
|
roomResp, err := n.client.JoinedRooms()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error getting joined rooms: %v", err)
|
log.Printf("error getting joined rooms: %v", err)
|
||||||
@ -57,7 +63,16 @@ func (n *nunWatch) Main() {
|
|||||||
}
|
}
|
||||||
rooms := roomResp.JoinedRooms
|
rooms := roomResp.JoinedRooms
|
||||||
for _, room := range rooms {
|
for _, room := range rooms {
|
||||||
|
log.Printf("posting %v", n.curPost.Number)
|
||||||
|
if n.curPost.Number != cur.Number {
|
||||||
n.client.SendText(room, fmt.Sprintf("%v\n%v", n.curPost.Title, n.curPost.Link))
|
n.client.SendText(room, fmt.Sprintf("%v\n%v", n.curPost.Title, n.curPost.Link))
|
||||||
|
if n.curPost.Number > 0 {
|
||||||
|
err := n.client.SetAccountData("nun.newest", n.curPost)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("error saving current post: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
11
reddit.go
11
reddit.go
@ -6,16 +6,20 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type post struct {
|
type post struct {
|
||||||
Title string
|
Title string
|
||||||
Link string
|
Link string
|
||||||
|
Number int
|
||||||
}
|
}
|
||||||
|
|
||||||
func getNewestPost(subreddit string) (post, error) {
|
func getNewestPost(subreddit string) (post, error) {
|
||||||
var resp redditResp
|
var resp redditResp
|
||||||
|
re := regexp.MustCompile("([0-9]+)")
|
||||||
//building request from scratch because reddit api is weird
|
//building request from scratch because reddit api is weird
|
||||||
url := fmt.Sprintf("https://www.reddit.com/r/%v/new.json?sort=new&limit=1", subreddit)
|
url := fmt.Sprintf("https://www.reddit.com/r/%v/new.json?sort=new&limit=1", subreddit)
|
||||||
req, _ := http.NewRequest("GET", url, nil)
|
req, _ := http.NewRequest("GET", url, nil)
|
||||||
@ -43,9 +47,14 @@ func getNewestPost(subreddit string) (post, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return post{}, err
|
return post{}, err
|
||||||
}
|
}
|
||||||
|
numS := re.FindStringSubmatch(resp.Data.Children[0].Data.Title)[0]
|
||||||
|
num, err := strconv.Atoi(numS)
|
||||||
|
if err != nil || numS == "" {
|
||||||
|
num = -1
|
||||||
|
}
|
||||||
return post{
|
return post{
|
||||||
Title: resp.Data.Children[0].Data.Title,
|
Title: resp.Data.Children[0].Data.Title,
|
||||||
Link: resp.Data.Children[0].Data.URL,
|
Link: resp.Data.Children[0].Data.URL,
|
||||||
|
Number: num,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user