arnaucube
arnaucube/argosPublic Self-hosted repository
reorganization and a little bit of code robustness, and implemented TweetRandom functionallity
arnaucode idoctnef@openmailbox.org
Thu, 20 Apr 2017 23:08:09 +0200
commit

2e1d99c387a445b14889a6631ab118b6aeb60780

parent

0002787cb03754405de7a12f0a3ec32259e23527

M .gitignore → .gitignore

@@ -25,5 +25,5 @@ *.prof

twitterConfig.json -build/twitterConfig.json +#build/twitterConfig.json temp
M README.md → README.md

@@ -1,3 +1,5 @@

+http://arnaucode.com/argos/ + # argos twitter analyzer written in Go lang, insipired on the Python tweets_analyzer by x0rz https://github.com/x0rz/tweets_analyzer

@@ -13,18 +15,18 @@ [under development]

#### Current features 1. User analyzer - - word count - - weekly activity distribution - - daily activity distribution - - devices used - - hashtags most used count - - user mentions coun [not implemented yet] -2. Delete Tweets and Favs -3. Unfollow all [not implemented yet] -4. Random follow [not implemented yet] + - Word count + - Weekly activity distribution + - Daily activity distribution + - Devices used + - Hashtags most used count + - Interactions with other users count +2. Delete Tweets +3. Unfollow all +4. Random follow - selects n number of accounts to follow, and follows n random accounts -5. Random tweet [not implemented yet] - - post a tweet with random content (from newspaper) +5. Random tweet + - post a tweet with content from a selected account ![screen](https://raw.githubusercontent.com/arnaucode/argos/master/screen3.png "screen")
A analyzeUserInteractions.go

@@ -0,0 +1,17 @@

+package main + +import ( + "strings" + + "github.com/dghubble/go-twitter/twitter" +) + +func analyzeUserInteractions(tweets []twitter.Tweet, words map[string]int) map[string]int { + var userInteractions = make(map[string]int) + for v, k := range words { + if strings.Contains(v, "@") { + userInteractions[v] = k + } + } + return (userInteractions) +}
M analyzeWords.go → analyzeWords.go

@@ -26,7 +26,6 @@ for _, v := range tweets {

words = mapWords(v.Text, words) } - fmt.Println(len(words)) //get sorted list of frequency words _ = printSortedMapStringInt(words, minNumWords) fmt.Println(" ")
A build/twitterConfig_DEMO.json

@@ -0,0 +1,6 @@

+{ + "consumer_key": "xxxxxxxxxxxxxxxx", + "consumer_secret": "xxxxxxxxxxxxxxxx", + "access_token_key": "xxxxxxxxxxxxxxxx", + "access_token_secret": "xxxxxxxxxxxxxxxx" +}
M followRandom.go → optionFollowRandom.go

@@ -11,10 +11,13 @@

"github.com/dghubble/go-twitter/twitter" ) -/*func getUserByScreenName(ScreenName string) *twitter.User{ - - -}*/ +func waitTime(minutes int) { + //wait to avoid the twitter api limitation + timeToSleep := time.Duration(minutes) * time.Minute + fmt.Println("waiting " + strconv.Itoa(minutes) + " min to avoid twitter api limitation") + fmt.Println(time.Now().Local()) + time.Sleep(timeToSleep) +} func getUserToFollowFromUser(client *twitter.Client, fromUser string) string { friends, httpResp, err := client.Friends.List(&twitter.FriendListParams{ ScreenName: fromUser,

@@ -27,14 +30,16 @@ if httpResp.Status != "200 OK" {

c.Red(httpResp.Status) } //fmt.Println(friends.Users) - c.Green(friends.Users[0].ScreenName) + //c.Green("@" + friends.Users[0].ScreenName) return friends.Users[0].ScreenName } func followUser(client *twitter.Client, screenName string) { - _, _, _ = client.Friendships.Create(&twitter.FriendshipCreateParams{ + user, _, _ := client.Friendships.Create(&twitter.FriendshipCreateParams{ ScreenName: screenName, }) + fmt.Print("followed: ") + c.Green("@" + user.ScreenName + " - " + user.Description) } func followRandom(client *twitter.Client, nFollow int, screenName string) {

@@ -44,10 +49,7 @@ for i := 0; i < nFollow; i++ {

userToFollow := getUserToFollowFromUser(client, screenName) followUser(client, userToFollow) screenName = userToFollow - //wait to avoid the twitter api limitation - fmt.Println("waiting 1 min to avoid twitter api limitation") - fmt.Println(time.Now().Local()) - time.Sleep(1 * time.Minute) + waitTime(1) } }
M getUserTweets.go → optionGetUserTweets.go

@@ -57,6 +57,11 @@ hashtags := analyzeHashtags(tweets, words)

printSortedMapStringInt(hashtags, minNumHashtag) fmt.Println("") + fmt.Println("Interactions with other users (more than " + strconv.Itoa(minNumUserInteractions) + " times): ") + userInteractions := analyzeUserInteractions(tweets, words) + printSortedMapStringInt(userInteractions, minNumUserInteractions) + fmt.Println("") + analyzeDates(tweets) fmt.Println("") fmt.Println("Devices:")
M main.go → main.go

@@ -10,7 +10,8 @@

const version = "0.1-dev" const minNumWords = 10 const minNumHashtag = 2 -const iterationsCount = 3 +const minNumUserInteractions = 1 +const iterationsCount = 1 func main() { c.Yellow("Argos Panoptes")

@@ -25,9 +26,10 @@ newcommand := bufio.NewReader(os.Stdin)

fmt.Print("Please select command number") options := ` 1 - Analyze username - 2 - Delete Tweets & Rretweets - 3 - Unfollow all - 4 - Follow random + 2 - Unfollow all + 3 - Follow random + 4 - Delete Tweets + 5 - Tweet Random 0 - Exit script option to select: ` for {

@@ -42,16 +44,20 @@ fmt.Println("selected 1 - Analyze username")

optionGetUserTweets(client) break case "2": - fmt.Println("selected 2 - Delete Tweets") - optionDeleteTweets(client) + fmt.Println("selected 2 - Unfollow all") + optionUnfollowAll(client) break case "3": - fmt.Println("selected 3 - Unfollow all") - optionUnfollowAll(client) + fmt.Println("selected 3 - Follow random") + optionFollowRandom(client) break case "4": - fmt.Println("selected 4 - Follow random") - optionFollowRandom(client) + fmt.Println("selected 4 - Delete Tweets") + optionDeleteTweets(client) + break + case "5": + fmt.Println("selected 5 - Tweet random") + optionTweetRandom(client) break case "0": fmt.Println("selected 0 - exit script")
A optionTweetRandom.go

@@ -0,0 +1,73 @@

+package main + +import ( + "bufio" + "fmt" + "os" + "strconv" + "strings" + + "github.com/dghubble/go-twitter/twitter" +) + +func postTweet(client *twitter.Client, content string) { + tweet, httpResp, err := client.Statuses.Update(content, nil) + if err != nil { + fmt.Println(err) + } + if httpResp.Status != "200 OK" { + c.Red("error: " + httpResp.Status) + } + fmt.Print("tweet posted: ") + c.Green(tweet.Text) +} +func tweetRandom(client *twitter.Client, nTweets int, screenName string) { + fmt.Println("Starting to publish " + strconv.Itoa(nTweets) + " tweets") + + tweets := getTweets(client, screenName, 1) + fmt.Println("the selected account have more than " + strconv.Itoa(len(tweets))) + + for i := 0; i < nTweets && i < len(tweets); i++ { + postTweet(client, tweets[i].Text) + waitTime(1) + } + +} + +func optionTweetRandom(client *twitter.Client) { + c.Red("ATTENTION!") + c.Purple("Publishing tweets from a bot can be banned by twitter!") + c.Cyan("Twitter can consider your account an spam account") + c.Red("Maybe you can publish the random tweets by hand") + c.Red("how many tweets to publish?") + newcommand := bufio.NewReader(os.Stdin) + answer, _ := newcommand.ReadString('\n') + answer = strings.TrimSpace(answer) + fmt.Print("Number of tweets to publish: ") + c.Purple(answer) + nTweets, err := strconv.Atoi(answer) + if err != nil { + fmt.Println("incorrect entry, need a positive number") + } + + fmt.Print("entry @username of a twitter account, the content of the tweets will be from the account tweets: @") + newcommand = bufio.NewReader(os.Stdin) + screenName, _ := newcommand.ReadString('\n') + screenName = strings.TrimSpace(screenName) + fmt.Print("user to get tweets content: @") + c.Purple(screenName) + + 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") + tweetRandom(client, nTweets, screenName) + break + default: + fmt.Println("Operation cancelled") + break + } +}
M printSortedMapStringInt.go → printSortedMapStringInt.go

@@ -3,9 +3,12 @@

import ( "fmt" "sort" + "strconv" ) func printSortedMapStringInt(m map[string]int, threshold int) map[int][]string { + total := len(m) + fmt.Println("total: " + strconv.Itoa(total)) n := map[int][]string{} var a []int for k, v := range m {

@@ -19,7 +22,8 @@ }

sort.Sort(sort.Reverse(sort.IntSlice(a))) for _, k := range a { for _, s := range n[k] { - fmt.Printf("%d - %s,\n", k, s) + //fmt.Printf("%d (%d)- %s,\n", k, tantPerCent, s) + fmt.Println(strconv.Itoa(k) + " - " + s) } } return n