tweets.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package anaconda
  2. import (
  3. "fmt"
  4. "net/url"
  5. "strconv"
  6. )
  7. func (a TwitterApi) GetTweet(id int64, v url.Values) (tweet Tweet, err error) {
  8. v = cleanValues(v)
  9. v.Set("id", strconv.FormatInt(id, 10))
  10. response_ch := make(chan response)
  11. a.queryQueue <- query{a.baseUrl + "/statuses/show.json", v, &tweet, _GET, response_ch}
  12. return tweet, (<-response_ch).err
  13. }
  14. func (a TwitterApi) GetTweetsLookupByIds(ids []int64, v url.Values) (tweet []Tweet, err error) {
  15. var pids string
  16. for w, i := range ids {
  17. pids += strconv.FormatInt(i, 10)
  18. if w != len(ids)-1 {
  19. pids += ","
  20. }
  21. }
  22. v = cleanValues(v)
  23. v.Set("id", pids)
  24. response_ch := make(chan response)
  25. a.queryQueue <- query{a.baseUrl + "/statuses/lookup.json", v, &tweet, _GET, response_ch}
  26. return tweet, (<-response_ch).err
  27. }
  28. func (a TwitterApi) GetRetweets(id int64, v url.Values) (tweets []Tweet, err error) {
  29. response_ch := make(chan response)
  30. a.queryQueue <- query{a.baseUrl + fmt.Sprintf("/statuses/retweets/%d.json", id), v, &tweets, _GET, response_ch}
  31. return tweets, (<-response_ch).err
  32. }
  33. //PostTweet will create a tweet with the specified status message
  34. func (a TwitterApi) PostTweet(status string, v url.Values) (tweet Tweet, err error) {
  35. v = cleanValues(v)
  36. v.Set("status", status)
  37. response_ch := make(chan response)
  38. a.queryQueue <- query{a.baseUrl + "/statuses/update.json", v, &tweet, _POST, response_ch}
  39. return tweet, (<-response_ch).err
  40. }
  41. //DeleteTweet will destroy (delete) the status (tweet) with the specified ID, assuming that the authenticated user is the author of the status (tweet).
  42. //If trimUser is set to true, only the user's Id will be provided in the user object returned.
  43. func (a TwitterApi) DeleteTweet(id int64, trimUser bool) (tweet Tweet, err error) {
  44. v := url.Values{}
  45. if trimUser {
  46. v.Set("trim_user", "t")
  47. }
  48. response_ch := make(chan response)
  49. a.queryQueue <- query{a.baseUrl + fmt.Sprintf("/statuses/destroy/%d.json", id), v, &tweet, _POST, response_ch}
  50. return tweet, (<-response_ch).err
  51. }
  52. //Retweet will retweet the status (tweet) with the specified ID.
  53. //trimUser functions as in DeleteTweet
  54. func (a TwitterApi) Retweet(id int64, trimUser bool) (rt Tweet, err error) {
  55. v := url.Values{}
  56. if trimUser {
  57. v.Set("trim_user", "t")
  58. }
  59. response_ch := make(chan response)
  60. a.queryQueue <- query{a.baseUrl + fmt.Sprintf("/statuses/retweet/%d.json", id), v, &rt, _POST, response_ch}
  61. return rt, (<-response_ch).err
  62. }
  63. //UnRetweet will renove retweet Untweets a retweeted status.
  64. //Returns the original Tweet with retweet details embedded.
  65. //
  66. //https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/post-statuses-unretweet-id
  67. //trim_user: tweet returned in a timeline will include a user object
  68. //including only the status authors numerical ID.
  69. func (a TwitterApi) UnRetweet(id int64, trimUser bool) (rt Tweet, err error) {
  70. v := url.Values{}
  71. if trimUser {
  72. v.Set("trim_user", "t")
  73. }
  74. response_ch := make(chan response)
  75. a.queryQueue <- query{a.baseUrl + fmt.Sprintf("/statuses/unretweet/%d.json", id), v, &rt, _POST, response_ch}
  76. return rt, (<-response_ch).err
  77. }
  78. // Favorite will favorite the status (tweet) with the specified ID.
  79. // https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/post-favorites-create
  80. func (a TwitterApi) Favorite(id int64) (rt Tweet, err error) {
  81. v := url.Values{}
  82. v.Set("id", fmt.Sprint(id))
  83. response_ch := make(chan response)
  84. a.queryQueue <- query{a.baseUrl + fmt.Sprintf("/favorites/create.json"), v, &rt, _POST, response_ch}
  85. return rt, (<-response_ch).err
  86. }
  87. // Un-favorites the status specified in the ID parameter as the authenticating user.
  88. // Returns the un-favorited status in the requested format when successful.
  89. // https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/post-favorites-destroy
  90. func (a TwitterApi) Unfavorite(id int64) (rt Tweet, err error) {
  91. v := url.Values{}
  92. v.Set("id", fmt.Sprint(id))
  93. response_ch := make(chan response)
  94. a.queryQueue <- query{a.baseUrl + fmt.Sprintf("/favorites/destroy.json"), v, &rt, _POST, response_ch}
  95. return rt, (<-response_ch).err
  96. }