Beispiel #1
0
func parseResult(twitr twitter.Twitter, tab result) Result {
	var out Result
	out.Source = tab.source

	fn := func(dest *[]User, source []int) {
		for _, id := range source {
			var user User
			bUser, err := twitr.ShowId(id)
			handleError(err)
			err = json.Unmarshal(bUser, &user)
			handleError(err)
			*dest = append(*dest, user)
		}
	}

	if len(tab.lose) < 15 {
		fn(&out.Lose, tab.lose)
	} else {
		out.LoseMessage += "Too many users lost."
	}
	if len(tab.win) < 15 {
		fn(&out.Win, tab.win)
	} else {
		out.WinMessage += "Too many users won."
	}
	return out
}
Beispiel #2
0
func getFollowers(twitr twitter.Twitter, screen_name string) Followers {
	bFollowers, err := twitr.FollowersIds(screen_name, -1)
	handleError(err)

	var curFollowers Followers
	err = json.Unmarshal(bFollowers, &curFollowers)
	handleError(err)
	return curFollowers
}
Beispiel #3
0
func main() {
	var twitr twitter.Twitter
	templates = template.Must(template.New("nma").Parse(templateNMA))
	templates = template.Must(templates.New("console").Parse(templateNotifyConsole))

	username := os.Getenv("TWITTER_USERNAME")
	checkEnvVar(username)
	key := os.Getenv("TWITTER_KEY")
	checkEnvVar(key)
	secret := os.Getenv("TWITTER_SECRET")
	checkEnvVar(secret)
	nmaKey = os.Getenv("NOTIFY_MY_ANDROID_KEY") // this parameter is optional, no need to check
	if nmaKey != "" {
		n := nma.New(nmaKey)
		err := n.Verify(nmaKey)
		handleError(err)
	}

	twitr = twitter.NewTwitter(key, secret)
	defer twitr.Close()

	done := make(chan bool)

	c := make(chan os.Signal)

	signal.Notify(c, os.Interrupt)

	go func(twitr twitter.Twitter, done <-chan bool) {
		var end bool
		var prevFollowers, curFollowers Followers
		prevFollowers = getFollowers(twitr, username)
		fmt.Printf("Initialisation: %v followers for user %v\n", len(prevFollowers.Ids), username)
		tick := time.Tick(1 * time.Minute)
		for !end {
			select {
			case <-done:
				end = true
			case <-tick:
				curFollowers = getFollowers(twitr, username)
				if len(curFollowers.Ids) == 0 {
					continue
				}
				lost, win := diffFollowers(prevFollowers.Ids, curFollowers.Ids)
				res := parseResult(twitr, result{lose: lost, win: win, source: curFollowers})
				notify(res)
				prevFollowers = curFollowers
			}
		}
	}(twitr, done)

	select {
	case <-c:
		done <- true
	}
}