Browse Source

streaming keywords implemented, replying from the flock (botnet) to tweets with text generated with Markov chains

pull/1/head
arnaucode 7 years ago
parent
commit
938db1f2cf
6 changed files with 183 additions and 44 deletions
  1. +1
    -1
      README.md
  2. +15
    -27
      flock.go
  3. +18
    -13
      main.go
  4. +6
    -3
      markov.go
  5. +32
    -0
      optionManualTweetFromFlock.go
  6. +111
    -0
      optionMarkovFlockBotnet.go

+ 1
- 1
README.md

@ -1 +1 @@
# twFlock
# projectFlock

tweetFromFlock.go → flock.go

@ -1,15 +1,27 @@
package main package main
import ( import (
"bufio"
"fmt" "fmt"
"os"
"strconv" "strconv"
"strings"
"github.com/dghubble/go-twitter/twitter" "github.com/dghubble/go-twitter/twitter"
) )
func replyTweet(client *twitter.Client, text string, inReplyToStatusID int64) {
tweet, httpResp, err := client.Statuses.Update(text, &twitter.StatusUpdateParams{
InReplyToStatusID: inReplyToStatusID,
})
if err != nil {
fmt.Println(err)
}
if httpResp.Status != "200 OK" {
c.Red("error: " + httpResp.Status)
c.Purple("maybe twitter has blocked the account, CTRL+C, wait 15 minutes and try again")
}
fmt.Print("tweet posted: ")
c.Green(tweet.Text)
}
func postTweet(client *twitter.Client, text string) { func postTweet(client *twitter.Client, text string) {
tweet, httpResp, err := client.Statuses.Update(text, nil) tweet, httpResp, err := client.Statuses.Update(text, nil)
if err != nil { if err != nil {
@ -33,27 +45,3 @@ func tweetFromFlock(flock Flock, text string) {
} }
} }
func optionTweetFromFlock(flock Flock) {
fmt.Print("entry tweet content: ")
newcommand := bufio.NewReader(os.Stdin)
text, _ := newcommand.ReadString('\n')
text = strings.TrimSpace(text)
fmt.Print("tweet content: ")
c.Purple(text)
c.Red("Are you sure? [y/n]")
newcommand = bufio.NewReader(os.Stdin)
answer, _ := newcommand.ReadString('\n')
answer = strings.TrimSpace(answer)
switch answer {
case "y":
fmt.Println("ok, you are sure")
tweetFromFlock(flock, text)
break
default:
fmt.Println("Operation cancelled")
break
}
}

+ 18
- 13
main.go

@ -10,26 +10,22 @@ import (
const version = "0.1-dev" const version = "0.1-dev"
func main() { func main() {
c.Yellow("twFlock")
c.Yellow("projectFlock")
fmt.Println("---------------") fmt.Println("---------------")
c.Cyan("twFlock initialized")
c.Purple("https://github.com/arnaucode/twFlock")
c.Cyan("projectFlock initialized")
c.Purple("https://github.com/arnaucode/projectFlock")
fmt.Println("version " + version) fmt.Println("version " + version)
fmt.Println("Reading flockConfig.json file") fmt.Println("Reading flockConfig.json file")
flock := readConfigTokensAndConnect() flock := readConfigTokensAndConnect()
c.Yellow("generating markov chains (may take some seconds)")
text, _ := readTxt("text.txt")
states := markov.train(text)
c.Green("markov chains generated")
//var flock Flock //var flock Flock
fmt.Println("---------------") fmt.Println("---------------")
newcommand := bufio.NewReader(os.Stdin) newcommand := bufio.NewReader(os.Stdin)
fmt.Print("Please select command number") fmt.Print("Please select command number")
options := ` options := `
1 - Tweet from flock of bots
2 - Markov
1 - Manual tweet from flock of bots
2 - Markov manual text generator
3 - Markov's Flock Botnet
0 - Exit script 0 - Exit script
option to select: ` option to select: `
for { for {
@ -40,13 +36,22 @@ option to select: `
switch option { switch option {
case "1": case "1":
fmt.Println("selected 1 - Tweet from flock of bots")
optionTweetFromFlock(flock)
fmt.Println("selected 1 - Manual tweet from flock of bots")
optionManualTweetFromFlock(flock)
break break
case "2": case "2":
fmt.Println("selected 2 - Markov")
fmt.Println("selected 2 - Markov manual text generator")
c.Yellow("generating markov chains (may take some seconds)")
text, _ := readTxt("text.txt")
states := markov.train(text)
c.Green("markov chains generated")
optionTweetMarkov(states) optionTweetMarkov(states)
break break
case "3":
fmt.Println("selected 3 - Markov Flock Botnet")
optionMarkovFlockBotnet(flock)
break
case "0": case "0":
fmt.Println("selected 0 - exit script") fmt.Println("selected 0 - exit script")
os.Exit(3) os.Exit(3)

+ 6
- 3
markov.go

@ -26,7 +26,7 @@ func printLoading(n int, total int) {
bar = append(bar, "█") bar = append(bar, "█")
} }
progressBar := strings.Join(bar, "") progressBar := strings.Join(bar, "")
fmt.Printf("\r " + progressBar + " - " + strconv.Itoa(tantPerCent) + "%")
fmt.Printf("\r " + progressBar + " - " + strconv.Itoa(tantPerCent) + "")
} }
func addWordToStates(states []State, word string) ([]State, int) { func addWordToStates(states []State, word string) ([]State, int) {
@ -70,7 +70,7 @@ func calcMarkovStates(words []string) []State {
states[i].NextStates[j].Prob = (float64(states[i].NextStates[j].Count) / float64(len(words)) * 100) states[i].NextStates[j].Prob = (float64(states[i].NextStates[j].Count) / float64(len(words)) * 100)
} }
} }
fmt.Println("total words computed: " + strconv.Itoa(len(words)))
fmt.Println("\ntotal words computed: " + strconv.Itoa(len(words)))
//fmt.Println(states) //fmt.Println(states)
return states return states
} }
@ -116,9 +116,12 @@ func (markov Markov) generateText(states []State, initWord string, count int) st
generatedText = append(generatedText, word) generatedText = append(generatedText, word)
for i := 0; i < count; i++ { for i := 0; i < count; i++ {
word = getNextMarkovState(states, word) word = getNextMarkovState(states, word)
if word == "word no exist on the memory" {
return "word no exist on the memory"
}
generatedText = append(generatedText, word) generatedText = append(generatedText, word)
} }
generatedText = append(generatedText, ".")
//generatedText = append(generatedText, ".")
text := strings.Join(generatedText, " ") text := strings.Join(generatedText, " ")
return text return text
} }

+ 32
- 0
optionManualTweetFromFlock.go

@ -0,0 +1,32 @@
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func optionManualTweetFromFlock(flock Flock) {
fmt.Print("entry tweet content: ")
newcommand := bufio.NewReader(os.Stdin)
text, _ := newcommand.ReadString('\n')
text = strings.TrimSpace(text)
fmt.Print("tweet content: ")
c.Purple(text)
c.Red("Are you sure? [y/n]")
newcommand = bufio.NewReader(os.Stdin)
answer, _ := newcommand.ReadString('\n')
answer = strings.TrimSpace(answer)
switch answer {
case "y":
fmt.Println("ok, you are sure")
tweetFromFlock(flock, text)
break
default:
fmt.Println("Operation cancelled")
break
}
}

+ 111
- 0
optionMarkovFlockBotnet.go

@ -0,0 +1,111 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
"github.com/dghubble/go-twitter/twitter"
)
func isRT(text string) bool {
tweetWords := strings.Split(text, " ")
for i := 0; i < len(tweetWords); i++ {
if tweetWords[i] == "RT" {
c.Red(text)
return true
}
}
return false
}
func generateMarkovResponse(states []State, word string) string {
generatedText := markov.generateText(states, word, 15)
return generatedText
}
func processTweet(states []State, flockUser *twitter.Client, botScreenName string, keywords []string, tweet *twitter.Tweet) {
c.Yellow("bot @" + botScreenName + " - New tweet detected:")
fmt.Println(tweet.Text)
tweetWords := strings.Split(tweet.Text, " ")
generatedText := "word no exist on the memory"
for i := 0; i < len(tweetWords) && generatedText == "word no exist on the memory"; i++ {
fmt.Println(strconv.Itoa(i) + " - " + tweetWords[i])
generatedText = generateMarkovResponse(states, tweetWords[i])
}
c.Yellow("bot @" + botScreenName + " posting response")
fmt.Println(tweet.ID)
replyTweet(flockUser, "@"+tweet.User.ScreenName+" "+generatedText, tweet.ID)
waitTime(1)
}
func startStreaming(states []State, flockUser *twitter.Client, botScreenName string, keywords []string) {
// Convenience Demux demultiplexed stream messages
demux := twitter.NewSwitchDemux()
demux.Tweet = func(tweet *twitter.Tweet) {
if isRT(tweet.Text) == false {
processTweet(states, flockUser, botScreenName, keywords, tweet)
}
}
demux.DM = func(dm *twitter.DirectMessage) {
fmt.Println(dm.SenderID)
}
demux.Event = func(event *twitter.Event) {
fmt.Printf("%#v\n", event)
}
fmt.Println("Starting Stream...")
// FILTER
filterParams := &twitter.StreamFilterParams{
Track: keywords,
StallWarnings: twitter.Bool(true),
}
stream, err := flockUser.Streams.Filter(filterParams)
if err != nil {
log.Fatal(err)
}
// Receive messages until stopped or stream quits
demux.HandleChan(stream.Messages)
// Wait for SIGINT and SIGTERM (HIT CTRL-C)
/*ch := make(chan os.Signal)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
log.Println(<-ch)
fmt.Println("Stopping Stream...")
stream.Stop()*/
}
func optionMarkovFlockBotnet(flock Flock) {
c.Green("Starting Markov's Flock botnet")
fmt.Println("generating Markov chains")
inputText, _ := readTxt("text.txt")
states := markov.train(inputText)
//getting the keywords
c.Purple("entry words to stream tweets (separated by comma): ")
newcommand := bufio.NewReader(os.Stdin)
text, _ := newcommand.ReadString('\n')
text = strings.TrimSpace(text)
text = strings.Replace(text, " ", "", -1)
keywords := strings.Split(text, ",")
c.Purple("total keywords: " + strconv.Itoa(len(keywords)))
fmt.Print("keywords to follow: ")
fmt.Println(keywords)
c.Red("Are you sure? [y/n]")
newcommand = bufio.NewReader(os.Stdin)
answer, _ := newcommand.ReadString('\n')
answer = strings.TrimSpace(answer)
switch answer {
case "y":
fmt.Println("ok, you are sure")
for i := 0; i < len(flock.Clients); i++ {
go startStreaming(states, flock.Clients[i], flock.ScreenNames[i], keywords)
}
break
default:
fmt.Println("Operation cancelled")
break
}
}

Loading…
Cancel
Save