-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
100 lines (88 loc) · 1.99 KB
/
main.go
File metadata and controls
100 lines (88 loc) · 1.99 KB
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
package main
import (
"fmt"
"go.code.as/writeas.v2"
"log"
"os"
"os/signal"
"syscall"
"time"
)
const (
checkInterval = 60 * time.Second
)
func main() {
// BEGIN CONFIGURATION //////////////////////////////////////////
// TODO: replace this with the post ID you want to have self-destruct
id := "8wyuivd67md9mmjf"
// TODO: replace these with your Write.as login credentials
username := "USERNAME"
password := "PASSWORD"
// END CONFIGURATION ////////////////////////////////////////////
// Begin the work
log.Println("Starting up...")
// Log in to Write.as
c := writeas.NewClient()
_, err := c.LogIn(username, password)
if err != nil {
log.Printf("Unable to log in: %s", err)
os.Exit(1)
} else {
log.Println("Authenticated.")
}
// Automatically invalidate auth token when finished
defer logout(c)
// Clean up when we receive an interrupt
qc := make(chan os.Signal, 2)
signal.Notify(qc, os.Interrupt, syscall.SIGTERM)
go func() {
<-qc
log.Printf("Packing up without any destruction.")
logout(c)
os.Exit(0)
}()
// Check for the post on a regular interval, starting now
ticker := time.NewTicker(checkInterval)
quit := make(chan struct{})
err = checkPost(c, id, quit)
if err != nil {
log.Printf("%s", err)
return
}
for {
select {
case <-ticker.C:
err = checkPost(c, id, quit)
if err != nil {
log.Printf("%s", err)
return
}
case <-quit:
log.Printf("Packing up.")
ticker.Stop()
return
}
}
}
func checkPost(c *writeas.Client, id string, quit chan struct{}) error {
log.Println("Inspecting post...")
p, err := c.GetPost(id)
if err != nil {
return fmt.Errorf("Unable to fetch post: %s", err)
}
log.Printf("Post %s has %d view(s)", id, p.Views)
if p.Views > 0 {
log.Println("Preparing to self-destruct...")
err = c.DeletePost(id, "")
if err != nil {
return fmt.Errorf("Unable to delete post: %s", err)
}
log.Println("BOOM!")
close(quit)
}
return nil
}
func logout(c *writeas.Client) {
c.LogOut()
log.Println("Logged out.")
}