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.

88 lines
2.3 KiB

7 years ago
7 years ago
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "strconv"
  7. "strings"
  8. "github.com/dghubble/go-twitter/twitter"
  9. )
  10. func getTweets(client *twitter.Client, username string, iterations int) []twitter.Tweet {
  11. var tweets []twitter.Tweet
  12. var maxid int64
  13. for i := 0; i < iterations; i++ {
  14. tweetsRaw, _, _ := client.Timelines.UserTimeline(&twitter.UserTimelineParams{
  15. ScreenName: username,
  16. Count: 200,
  17. MaxID: maxid,
  18. })
  19. //if no tweets, stop getting tweets
  20. if len(tweetsRaw) == 0 {
  21. break
  22. }
  23. maxid = tweetsRaw[len(tweetsRaw)-1].ID
  24. for _, v := range tweetsRaw {
  25. tweets = append(tweets, v)
  26. }
  27. }
  28. return tweets
  29. }
  30. func optionGetUserTweets(client *twitter.Client) {
  31. newcommand := bufio.NewReader(os.Stdin)
  32. fmt.Print("enter username: @")
  33. username, _ := newcommand.ReadString('\n')
  34. username = strings.TrimSpace(username)
  35. fmt.Print("user selected: ")
  36. c.Cyan("@" + username)
  37. fmt.Println("-----------------------")
  38. //get tweets
  39. tweets := getTweets(client, username, iterationsCount)
  40. if len(tweets) == 0 {
  41. fmt.Println("User @" + username + " does not have tweets")
  42. return
  43. }
  44. //now analyze words and dates
  45. fmt.Println("Word frequency (more than " + strconv.Itoa(minNumWords) + " times):")
  46. words := analyzeWords(tweets)
  47. fmt.Println("Hashtags used (more than " + strconv.Itoa(minNumHashtag) + " times): ")
  48. hashtags := analyzeHashtags(tweets, words)
  49. printSortedMapStringInt(hashtags, minNumHashtag)
  50. fmt.Println("")
  51. fmt.Println("Interactions with other users (more than " + strconv.Itoa(minNumUserInteractions) + " times): ")
  52. userInteractions := analyzeUserInteractions(tweets, words)
  53. printSortedMapStringInt(userInteractions, minNumUserInteractions)
  54. fmt.Println("")
  55. analyzeDates(tweets)
  56. fmt.Println("")
  57. fmt.Println("Devices:")
  58. sources := analyzeSource(tweets)
  59. for k, v := range sources {
  60. fmt.Print("\x1b[32;1m") //cyan
  61. fmt.Print(k + ": ")
  62. fmt.Print("\x1b[0m") //defaultColor
  63. fmt.Println(strconv.Itoa(v) + "tw ")
  64. }
  65. fmt.Println(" ")
  66. fmt.Print("first tweet analyzed: ")
  67. fmt.Println(tweets[len(tweets)-1].CreatedAt)
  68. fmt.Print("last tweet analyzed: ")
  69. fmt.Println(tweets[0].CreatedAt)
  70. fmt.Println(" ")
  71. fmt.Println("Total of " + strconv.Itoa(len(tweets)) + " tweets analyzed")
  72. fmt.Println(" ")
  73. fmt.Print("User @")
  74. c.Cyan(username)
  75. fmt.Println(" analysis finished")
  76. }