You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
3.2 KiB

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