Browse Source

gets tweets and count word frequency

master
arnaucode 7 years ago
parent
commit
a7a888cc8f
5 changed files with 117 additions and 54 deletions
  1. +50
    -0
      analyzeUsername.go
  2. BIN
      build/goTweetsAnalyze
  3. +1
    -54
      main.go
  4. +40
    -0
      readConfigTokensAndConnect.go
  5. +26
    -0
      sortMap.go

+ 50
- 0
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)
}

BIN
build/goTweetsAnalyze


+ 1
- 54
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()

+ 40
- 0
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
}

+ 26
- 0
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
}

Loading…
Cancel
Save