Skip to content

Instantly share code, notes, and snippets.

@doorbash
Created July 2, 2020 09:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doorbash/11235c1c478e650e13dda080686feac8 to your computer and use it in GitHub Desktop.
Save doorbash/11235c1c478e650e13dda080686feac8 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"net/http"
"time"
"github.com/dghubble/go-twitter/twitter"
"github.com/dghubble/oauth1"
)
const (
API_KEY = "*************************"
API_SECRET = "*************************"
ACCESS_TOKEN = "*************************"
ACCESS_SECRET = "*************************"
USERNAME = "doorbash"
TIMEZONE = "Asia/Tehran"
)
var lastGetTime time.Time
var lastTweetTime time.Time
var followersCount int
var client *twitter.Client
func main() {
config := oauth1.NewConfig(API_KEY, API_SECRET)
token := oauth1.NewToken(ACCESS_TOKEN, ACCESS_SECRET)
httpClient := config.Client(oauth1.NoContext, token)
client = twitter.NewClient(httpClient)
go func() {
for now := range time.Tick(30 * time.Second) {
loc, _ := time.LoadLocation(TIMEZONE)
sfTime := now.In(loc)
if lastTweetTime.IsZero() || time.Now().Sub(lastTweetTime) > 10*time.Minute {
if sfTime.Hour() == 4 && sfTime.Minute() == 4 {
lastTweetTime = time.Now()
_, _, err := client.Statuses.Update("404", nil)
if err != nil {
fmt.Println(err)
}
}
}
}
}()
http.HandleFunc("/metrics", metrics)
http.ListenAndServe(":8888", nil)
}
func metrics(w http.ResponseWriter, req *http.Request) {
if lastGetTime.IsZero() || time.Now().Sub(lastGetTime) > 10*60*time.Second {
user, _, err := client.Users.Show(&twitter.UserShowParams{
ScreenName: USERNAME,
})
if err != nil {
fmt.Println(err)
fmt.Fprintf(w, "Error %v", err)
return
}
followersCount = user.FollowersCount
lastGetTime = time.Now()
}
fmt.Fprintf(w, "followersCount{} %d", followersCount)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment