argos/getUserTweets.go
getUserTweets.goRaw
1package main
2
3import (
4 "bufio"
5 "fmt"
6 "os"
7 "strconv"
8 "strings"
9
10 "github.com/dghubble/go-twitter/twitter"
11)
12
13func getTweets(client *twitter.Client, username string, iterations int) []twitter.Tweet {
14 var tweets []twitter.Tweet
15 var maxid int64
16 for i := 0; i < iterations; i++ {
17 tweetsRaw, _, _ := client.Timelines.UserTimeline(&twitter.UserTimelineParams{
18 ScreenName: username,
19 Count: 200,
20 MaxID: maxid,
21 })
22 //if no tweets, stop getting tweets
23 if len(tweetsRaw) == 0 {
24 break
25 }
26 maxid = tweetsRaw[len(tweetsRaw)-1].ID
27
28 for _, v := range tweetsRaw {
29 tweets = append(tweets, v)
30 }
31 }
32
33 return tweets
34}
35func optionGetUserTweets(client *twitter.Client) {
36 newcommand := bufio.NewReader(os.Stdin)
37 fmt.Print("enter username: @")
38 username, _ := newcommand.ReadString('\n')
39 username = strings.TrimSpace(username)
40 fmt.Print("user selected: ")
41 c.Cyan("@" + username)
42 fmt.Println("-----------------------")
43
44 //get tweets
45 tweets := getTweets(client, username, iterationsCount)
46
47 if len(tweets) == 0 {
48 fmt.Println("User @" + username + " does not have tweets")
49 return
50 }
51 //now analyze words and dates
52 fmt.Println("Word frequency (more than " + strconv.Itoa(minNumWords) + " times):")
53 words := analyzeWords(tweets)
54
55 fmt.Println("Hashtags used (more than " + strconv.Itoa(minNumHashtag) + " times): ")
56 hashtags := analyzeHashtags(tweets, words)
57 printSortedMapStringInt(hashtags, minNumHashtag)
58 fmt.Println("")
59
60 analyzeDates(tweets)
61 fmt.Println("")
62 fmt.Println("Devices:")
63 sources := analyzeSource(tweets)
64 for k, v := range sources {
65 fmt.Print("\x1b[32;1m") //cyan
66 fmt.Print(k + ": ")
67 fmt.Print("\x1b[0m") //defaultColor
68 fmt.Println(strconv.Itoa(v) + "tw ")
69 }
70
71 fmt.Println(" ")
72 fmt.Print("first tweet analyzed: ")
73 fmt.Println(tweets[len(tweets)-1].CreatedAt)
74 fmt.Print("last tweet analyzed: ")
75 fmt.Println(tweets[0].CreatedAt)
76
77 fmt.Println(" ")
78 fmt.Println("Total of " + strconv.Itoa(len(tweets)) + " tweets analyzed")
79 fmt.Println(" ")
80 fmt.Print("User @")
81 c.Cyan(username)
82 fmt.Println(" analysis finished")
83}