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.

110 lines
3.0 KiB

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "sort"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/dghubble/go-twitter/twitter"
  11. )
  12. type DateRT struct {
  13. Retweets []twitter.Tweet
  14. Date string
  15. }
  16. func printDateRT(v DateRT) {
  17. fmt.Print("\x1b[32;1m" + v.Date + "\x1b[0m")
  18. fmt.Println(" " + strconv.Itoa(len(v.Retweets)) + " Retweets at this date:")
  19. for _, retweet := range v.Retweets {
  20. source := strings.Split(strings.Split(retweet.Source, ">")[1], "<")[0]
  21. if len(v.Retweets) > 1 {
  22. fmt.Print("\x1b[31;1m") //red
  23. }
  24. fmt.Print(" @" + retweet.User.ScreenName)
  25. fmt.Print("\x1b[0m") //defaultColor
  26. fmt.Print(" (ID: ")
  27. fmt.Print("\x1b[31;1m" + retweet.User.IDStr + "\x1b[0m)")
  28. if source == "TweetDeck" {
  29. fmt.Print(", source: \x1b[33;1m" + source + "\x1b[0m")
  30. } else {
  31. fmt.Print(", source: " + source)
  32. }
  33. fmt.Print(", user created at: \x1b[32;1m" + retweet.User.CreatedAt + "\x1b[0m,")
  34. fmt.Print(" \x1b[34;1m" + strconv.Itoa(retweet.User.FollowersCount) + "\x1b[0m followers")
  35. fmt.Print(", \x1b[34;1m" + strconv.Itoa(retweet.User.FriendsCount) + "\x1b[0m following")
  36. fmt.Println("")
  37. }
  38. fmt.Println("")
  39. }
  40. func optionAnalyzeTweet(client *twitter.Client) {
  41. newcommand := bufio.NewReader(os.Stdin)
  42. fmt.Print("enter link of the tweet: ")
  43. link, _ := newcommand.ReadString('\n')
  44. link = strings.TrimSpace(link)
  45. fmt.Print("link selected: ")
  46. c.Cyan(link)
  47. fmt.Println("-----------------------")
  48. linkParam := strings.Split(link, "/")
  49. tweetIdStr := linkParam[len(linkParam)-1]
  50. c.Cyan(tweetIdStr)
  51. tweetId, err := strconv.ParseInt(tweetIdStr, 10, 64)
  52. if err != nil {
  53. fmt.Println(err)
  54. }
  55. tweet, _, err := client.Statuses.Show(tweetId, nil)
  56. if err != nil {
  57. fmt.Println(err)
  58. }
  59. fmt.Print("tweet text: ")
  60. c.Yellow(tweet.Text)
  61. fmt.Print("tweet date: ")
  62. c.Green(tweet.CreatedAt)
  63. retweets := getRetweets(client, tweetId)
  64. var dates = make(map[string]DateRT)
  65. for _, retweet := range retweets {
  66. createdat, err := retweet.CreatedAtTime()
  67. check(err)
  68. createdatRounded := time.Date(createdat.Year(), createdat.Month(), createdat.Day(), createdat.Hour(), createdat.Minute(), 0, 0, createdat.Location())
  69. retws := dates[createdatRounded.String()].Retweets
  70. retws = append(retws, retweet)
  71. var currDate DateRT
  72. currDate.Retweets = retws
  73. currDate.Date = createdatRounded.String()
  74. dates[createdatRounded.String()] = currDate
  75. }
  76. fmt.Println("total of " + strconv.Itoa(len(retweets)) + " retweets")
  77. //puts the map into a slice, to easy sort
  78. var arrayDates []DateRT
  79. for _, v := range dates {
  80. arrayDates = append(arrayDates, v)
  81. }
  82. fmt.Println("")
  83. c.Cyan("Showing accounts that retweeted at the same exact time (they are possible Bots) and the source the RT were made:")
  84. //sort the slice
  85. sort.Slice(arrayDates, func(i, j int) bool {
  86. return len(arrayDates[i].Retweets) < len(arrayDates[j].Retweets)
  87. })
  88. //print the sorted slice
  89. for _, v := range arrayDates {
  90. if len(v.Retweets) > 1 {
  91. printDateRT(v)
  92. }
  93. }
  94. fmt.Println("")
  95. c.Purple("Warning: Twitter API only gives the last 100 Retweets")
  96. }