arnaucube
arnaucube/argosPublic Self-hosted repository
analyzes week days frequency and hour frequency
arnaucode idoctnef@openmailbox.org
Thu, 13 Apr 2017 17:36:40 +0200
commit

0d10f80cefa51f276e311703afeb9e97d6db6391

parent

c48bf605787463b4569ef417f812a785735cbdf7

8 files changed, 157 insertions(+), 78 deletions(-)

jump to
M README.md → README.md

@@ -1,7 +1,7 @@

# goTweetsAnalyze twitter analyzer written in Go lang -needs a .json file with the content: +needs a twitterConfig.json file on the /build folder with the content: ``` { "consumer_key": "xxxxxxxxxxxxxxxx",
A analyzeDates.go

@@ -0,0 +1,72 @@

+package main + +import ( + "fmt" + "strconv" + "strings" + + "github.com/dghubble/go-twitter/twitter" +) + +var week = [7]string{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"} + +func printBar(n int) { + for i := 0; i < n; i++ { + fmt.Print("█") + } + fmt.Println(" ") +} + +func printDays(days map[string]int) { + for i := 0; i < len(week); i++ { + fmt.Print(week[i] + " - " + strconv.Itoa(days[week[i]])) + fmt.Print(" ") + printBar(days[week[i]]) + } +} +func analyzeDays(tweets []twitter.Tweet) { + var days = make(map[string]int) + for _, v := range tweets { + //fmt.Println(v.CreatedAt) + day := strings.Split(v.CreatedAt, " ")[0] + if _, ok := days[day]; ok { + days[day] = days[day] + 1 + } else { + days[day] = 1 + } + } + printDays(days) +} + +func printHours(hours map[string]int) { + for i := 0; i < 24; i++ { + var h string + if i < 10 { + h = "0" + strconv.Itoa(i) + } else { + h = strconv.Itoa(i) + } + fmt.Print(h + "h - " + strconv.Itoa(hours[h]) + "tw") + fmt.Print(" ") + printBar(hours[h]) + } +} +func analyzeHours(tweets []twitter.Tweet) { + var hours = make(map[string]int) + for _, v := range tweets { + time := strings.Split(v.CreatedAt, " ")[3] + hour := strings.Split(time, ":")[0] + if _, ok := hours[hour]; ok { + hours[hour] = hours[hour] + 1 + } else { + hours[hour] = 1 + } + } + printHours(hours) +} + +func analyzeDates(tweets []twitter.Tweet) { + analyzeDays(tweets) + fmt.Println(" ") + analyzeHours(tweets) +}
D analyzeUsername.go

@@ -1,50 +0,0 @@

-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) -}
A analyzeWords.go

@@ -0,0 +1,52 @@

+package main + +import ( + "fmt" + "sort" + "strings" + + "github.com/dghubble/go-twitter/twitter" +) + +func sortMapWords(m map[string]int) map[int][]string { + n := map[int][]string{} + var a []int + for k, v := range m { + if v > minNumWords { + 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("%d - %s,\n", k, s) + } + } + return n +} + +func mapWords(text string, words map[string]int) { + s := strings.Split(text, " ") + + for _, v := range s { + if _, ok := words[v]; ok { + words[v] = words[v] + 1 + } else { + words[v] = 1 + } + } +} +func analyzeWords(tweets []twitter.Tweet) { + var words = make(map[string]int) + + for _, v := range tweets { + mapWords(v.Text, words) + } + + fmt.Println(len(words)) + //get sorted list of frequency words + _ = sortMapWords(words) +}
A getUser.go

@@ -0,0 +1,29 @@

+package main + +import ( + "bufio" + "fmt" + "os" + "strings" + + "github.com/dghubble/go-twitter/twitter" +) + +func getUser(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, + }) + + analyzeWords(tweets) + + analyzeDates(tweets) +}
M main.go → main.go

@@ -7,6 +7,8 @@ "os"

"strings" ) +const minNumWords = 4 + func main() { fmt.Println("---------------") fmt.Println("goTweetsAnalyze initialized")

@@ -28,7 +30,7 @@

switch option { case "1": fmt.Println("selected 1 - Analyze username") - analyzeUsername(client) + getUser(client) break case "2": fmt.Println("selected 2 - Delete all Favs")
D sortMap.go

@@ -1,26 +0,0 @@

-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 -}