gets tweets and count word frequency
arnaucode idoctnef@openmailbox.org
Wed, 12 Apr 2017 19:16:40 +0200
5 files changed,
117 insertions(+),
54 deletions(-)
A
analyzeUsername.go
@@ -0,0 +1,50 @@
+package main + +import ( + "bufio" + "fmt" + "os" + "strings" + + "github.com/dghubble/go-twitter/twitter" +) + +type Tweet struct { + text string `json: "Text"` +} + +var words = make(map[string]int) + +func mapWords(text string) { + s := strings.Split(text, " ") + + for _, v := range s { + if _, ok := words[v]; ok { + words[v] = words[v] + 1 + } else { + words[v] = 1 + } + } +} + +func analyzeUsername(client *twitter.Client) { + newcommand := bufio.NewReader(os.Stdin) + fmt.Print("enter username: @") + username, _ := newcommand.ReadString('\n') + username = strings.TrimSpace(username) + fmt.Println("user selected: " + username) + + fmt.Println("-----------------------") + + tweets, _, _ := client.Timelines.UserTimeline(&twitter.UserTimelineParams{ + ScreenName: username, + Count: 200, + }) + + for _, v := range tweets { + mapWords(v.Text) + } + + fmt.Println(len(words)) + _ = sortMap(words) +}
M
main.go
→
main.go
@@ -2,67 +2,14 @@ package main
import ( "bufio" - "encoding/json" "fmt" - "io/ioutil" "os" "strings" - - "github.com/dghubble/go-twitter/twitter" - "github.com/dghubble/oauth1" ) -type Config struct { - Consumer_key string `json:"consumer_key"` - Consumer_secret string `json:"consumer_secret"` - Access_token_key string `json:"access_token_key"` - Access_token_secret string `json:"access_token_secret"` -} - -func readConfigTokensAndConnect() (client *twitter.Client) { - var config Config - file, e := ioutil.ReadFile("twitterConfig.json") - if e != nil { - fmt.Println("error:", e) - } - content := string(file) - json.Unmarshal([]byte(content), &config) - //fmt.Printf("%+v\n", config) - fmt.Println("twitterConfig.json read comlete") - - fmt.Print("connecting to twitter api --> ") - configu := oauth1.NewConfig(config.Consumer_key, config.Consumer_secret) - token := oauth1.NewToken(config.Access_token_key, config.Access_token_secret) - httpClient := configu.Client(oauth1.NoContext, token) - // twitter client - client = twitter.NewClient(httpClient) - - fmt.Println("connection successfull") - - return client -} - -func analyzeUsername(client *twitter.Client) { - newcommand := bufio.NewReader(os.Stdin) - fmt.Print("enter username: @") - username, _ := newcommand.ReadString('\n') - username = strings.TrimSpace(username) - fmt.Println("user selected: " + username) - - // Home Timeline - user, resp, err := client.Users.Show(&twitter.UserShowParams{ - ScreenName: username, - }) - if err != nil { - fmt.Println("error:", err) - } - fmt.Println("%+v\n", user) - fmt.Println(resp) -} - func main() { fmt.Println("---------------") - fmt.Println("Tweets and favs delete script. Starting.") + fmt.Println("goTweetsAnalyze initialized") fmt.Println("Reading twitterConfig.json file") client := readConfigTokensAndConnect()
A
readConfigTokensAndConnect.go
@@ -0,0 +1,40 @@
+package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + + "github.com/dghubble/go-twitter/twitter" + "github.com/dghubble/oauth1" +) + +type Config struct { + Consumer_key string `json:"consumer_key"` + Consumer_secret string `json:"consumer_secret"` + Access_token_key string `json:"access_token_key"` + Access_token_secret string `json:"access_token_secret"` +} + +func readConfigTokensAndConnect() (client *twitter.Client) { + var config Config + file, e := ioutil.ReadFile("twitterConfig.json") + if e != nil { + fmt.Println("error:", e) + } + content := string(file) + json.Unmarshal([]byte(content), &config) + //fmt.Printf("%+v\n", config) + fmt.Println("twitterConfig.json read comlete") + + fmt.Print("connecting to twitter api --> ") + configu := oauth1.NewConfig(config.Consumer_key, config.Consumer_secret) + token := oauth1.NewToken(config.Access_token_key, config.Access_token_secret) + httpClient := configu.Client(oauth1.NoContext, token) + // twitter client + client = twitter.NewClient(httpClient) + + fmt.Println("connection successfull") + + return client +}
A
sortMap.go
@@ -0,0 +1,26 @@
+package main + +import ( + "fmt" + "sort" +) + +func sortMap(m map[string]int) map[int][]string { + n := map[int][]string{} + var a []int + for k, v := range m { + if v > 2 { + n[v] = append(n[v], k) + } + } + for k := range n { + a = append(a, k) + } + sort.Sort(sort.Reverse(sort.IntSlice(a))) + for _, k := range a { + for _, s := range n[k] { + fmt.Printf("%s, %d\n", s, k) + } + } + return n +}