argos/analyzeDates.go
analyzeDates.goRaw
1package main
2
3import (
4 "fmt"
5 "sort"
6 "strconv"
7 "strings"
8
9 "github.com/dghubble/go-twitter/twitter"
10)
11
12var week = [7]string{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}
13
14func printBar(n int, lowest int, highest int) {
15 bar := int((float64(n) / float64(highest)) * 40)
16
17 if n == lowest {
18 fmt.Print("\x1b[36;1m") //cyan
19 }
20 if n == highest {
21 fmt.Print("\x1b[31;1m") //red
22 }
23
24 for i := 0; i < bar; i++ {
25 fmt.Print("█")
26 }
27
28 if bar == 0 && n > 0 {
29 fmt.Print("█")
30 }
31 if n == lowest {
32 fmt.Print(" lowest")
33 }
34 if n == highest {
35 fmt.Print(" highest")
36 }
37 fmt.Print("\x1b[0m") //defaultColor
38 fmt.Println(" ")
39}
40func getHigherValueOfMap(m map[string]int) (int, int) {
41 var values []int
42 for _, v := range m {
43 values = append(values, v)
44 }
45 sort.Ints(values)
46 //returns lower, higher values
47 return values[0], values[len(values)-1]
48}
49
50func printDays(days map[string]int) {
51 lowest, highest := getHigherValueOfMap(days)
52
53 for i := 0; i < len(week); i++ {
54 fmt.Print(week[i] + " - " + strconv.Itoa(days[week[i]]) + "tw")
55 fmt.Print(" ")
56 printBar(days[week[i]], lowest, highest)
57 }
58}
59func analyzeDays(tweets []twitter.Tweet) {
60 var days = make(map[string]int)
61 for _, v := range tweets {
62 day := strings.Split(v.CreatedAt, " ")[0]
63 if _, ok := days[day]; ok {
64 days[day] = days[day] + 1
65 } else {
66 days[day] = 1
67 }
68 }
69 printDays(days)
70}
71
72func printHours(hours map[string]int) {
73 lowest, highest := getHigherValueOfMap(hours)
74
75 for i := 0; i < 24; i++ {
76 var h string
77 if i < 10 {
78 h = "0" + strconv.Itoa(i)
79 } else {
80 h = strconv.Itoa(i)
81 }
82 fmt.Print(h + "h - " + strconv.Itoa(hours[h]) + "tw")
83 fmt.Print(" ")
84 printBar(hours[h], lowest, highest)
85 }
86}
87func analyzeHours(tweets []twitter.Tweet) {
88 var hours = make(map[string]int)
89 for _, v := range tweets {
90 time := strings.Split(v.CreatedAt, " ")[3]
91 hour := strings.Split(time, ":")[0]
92 if _, ok := hours[hour]; ok {
93 hours[hour] = hours[hour] + 1
94 } else {
95 hours[hour] = 1
96 }
97 }
98 printHours(hours)
99}
100
101func analyzeDates(tweets []twitter.Tweet) {
102 fmt.Println("Weekly activity distribution (per day)")
103 analyzeDays(tweets)
104 fmt.Println(" ")
105 fmt.Println("Daily activity distribution (per hour)")
106 analyzeHours(tweets)
107}