Last active
December 7, 2016 10:25
-
-
Save rodkranz/cfd36556c67ffd57ced97b70e377f6ad to your computer and use it in GitHub Desktop.
Vote BlackMirror Bot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2016 Kranz. All rights reserved. | |
// Use of this source code is governed by a MIT-style | |
// license that can be found in the LICENSE file. | |
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"github.com/Pallinder/go-randomdata" | |
"io/ioutil" | |
"log" | |
"math/rand" | |
"net/http" | |
"os" | |
"strings" | |
"sync" | |
"time" | |
) | |
// Execute: | |
// $ go run main.go [name] | |
// E.g: | |
// $ go run main.go rodkranz | |
func main() { | |
if len(os.Args) <= 1 { | |
fmt.Print("You need to add a name for add vote.") | |
os.Exit(1) | |
} | |
name := os.Args[1] | |
if len(name) == 0 { | |
fmt.Print("You need to add a name for add vote.") | |
os.Exit(1) | |
} | |
inc := Increment{} | |
var wg sync.WaitGroup | |
for i := 0; i < 5; i++ { | |
wg.Add(1) | |
go func() { | |
go func() { | |
time.Sleep(5 * time.Minute) | |
wg.Done() | |
}() | |
for { | |
Post(name, &inc) | |
time.Sleep(1 * time.Second) | |
} | |
}() | |
} | |
wg.Wait() | |
fmt.Printf("Incremented: %d \nSuccess: %d \nFail: %d\n", inc.Total, inc.SuccessCount, inc.FailCount) | |
os.Exit(0) | |
} | |
var mu = &sync.Mutex{} | |
type Increment struct { | |
Total int | |
SuccessCount int | |
FailCount int | |
} | |
func (i *Increment) Increment() { | |
mu.Lock() | |
i.Total = i.Total + 1 | |
mu.Unlock() | |
} | |
func (i *Increment) Success() { | |
mu.Lock() | |
i.SuccessCount = i.SuccessCount + 1 | |
mu.Unlock() | |
} | |
func (i *Increment) Fail() { | |
mu.Lock() | |
i.FailCount = i.FailCount + 1 | |
mu.Unlock() | |
} | |
type BlackMirrorResponse struct { | |
Success bool `json:"success"` | |
ShortUrl string `json:"shortUrl"` | |
Rate int `json:"rate"` | |
ImgUrl string `json:"imgUrl"` | |
Avatar string `json:"avatar"` | |
} | |
func (b *BlackMirrorResponse) Parser(s []byte) error { | |
return json.Unmarshal(s, b) | |
} | |
type BlackMirrorRequest struct { | |
From string `json:"from"` | |
Rate int `json:"rate"` | |
To string `json:"to"` | |
Msg string `json:"msg"` | |
} | |
func (b *BlackMirrorRequest) UnParser() (*strings.Reader, error) { | |
s, err := json.Marshal(&b) | |
return strings.NewReader(string(s)), err | |
} | |
func random(min, max int) int { | |
rand.Seed(time.Now().Unix()) | |
return rand.Intn(max-min) + min | |
} | |
func Post(voteName string, inc *Increment) { | |
var name string | |
if random(0, 10)%2 == 0 { | |
name = randomdata.SillyName() | |
} else { | |
name = randomdata.FirstName(randomdata.Female) | |
} | |
req := BlackMirrorRequest{ | |
From: name, | |
Rate: 5, | |
To: voteName, | |
Msg: "", | |
} | |
io, err := req.UnParser() | |
if err != nil { | |
return | |
} | |
r, err := http.Post("https://rateme.social/php/BlackMirrorApp.php", "application/json", io) | |
if err != nil { | |
return | |
} | |
robots, err := ioutil.ReadAll(r.Body) | |
r.Body.Close() | |
if err != nil { | |
log.Fatal(err) | |
} | |
resp := &BlackMirrorResponse{} | |
resp.Parser(robots) | |
inc.Increment() | |
if resp.Success { | |
inc.Success() | |
} else { | |
inc.Fail() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment