diff --git a/README.md b/README.md index 1df9555..034a106 100644 --- a/README.md +++ b/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", diff --git a/analyzeDates.go b/analyzeDates.go new file mode 100644 index 0000000..76fa363 --- /dev/null +++ b/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) +} diff --git a/analyzeWords.go b/analyzeWords.go new file mode 100644 index 0000000..51bf281 --- /dev/null +++ b/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) +} diff --git a/build/goTweetsAnalyze b/build/goTweetsAnalyze index 5059a5b..899dbc8 100755 Binary files a/build/goTweetsAnalyze and b/build/goTweetsAnalyze differ diff --git a/analyzeUsername.go b/getUser.go similarity index 53% rename from analyzeUsername.go rename to getUser.go index cceb153..ee2e001 100644 --- a/analyzeUsername.go +++ b/getUser.go @@ -9,25 +9,7 @@ import ( "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) { +func getUser(client *twitter.Client) { newcommand := bufio.NewReader(os.Stdin) fmt.Print("enter username: @") username, _ := newcommand.ReadString('\n') @@ -41,10 +23,7 @@ func analyzeUsername(client *twitter.Client) { Count: 200, }) - for _, v := range tweets { - mapWords(v.Text) - } + analyzeWords(tweets) - fmt.Println(len(words)) - _ = sortMap(words) + analyzeDates(tweets) } diff --git a/main.go b/main.go index 0940848..aa9ea23 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,8 @@ import ( "strings" ) +const minNumWords = 4 + func main() { fmt.Println("---------------") fmt.Println("goTweetsAnalyze initialized") @@ -28,7 +30,7 @@ option to select: ` switch option { case "1": fmt.Println("selected 1 - Analyze username") - analyzeUsername(client) + getUser(client) break case "2": fmt.Println("selected 2 - Delete all Favs") diff --git a/sortMap.go b/sortMap.go deleted file mode 100644 index 31c9849..0000000 --- a/sortMap.go +++ /dev/null @@ -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 -}