twitter-api.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import { Injectable } from "@angular/core";
  2. import { HttpClient } from "@angular/common/http";
  3. import { Storage } from "@ionic/storage";
  4. import Twit from "twit";
  5. @Injectable()
  6. export class TwitterApiProvider {
  7. client: Twit;
  8. constructor(public http: HttpClient, private storage: Storage) {
  9. this.initApi();
  10. }
  11. private async initApi() {
  12. const access_token_key = await this.storage.get("accessTokenKey");
  13. const access_token_secret = await this.storage.get("accessTokenSecret");
  14. this.client = new Twit({
  15. consumer_key: "UxZkbKotkr8Uc6seupnaZ1kDE",
  16. consumer_secret: "fEAas8iugR60FOEXsFG0iajq6oyfIIXRBVMlTgWxBd1stWIKHq",
  17. access_token: access_token_key,
  18. access_token_secret: access_token_secret,
  19. timeout_ms: 60 * 1000 // optional HTTP request timeout to apply to all requests.
  20. });
  21. }
  22. public async fetchHomeFeed(maxId?) {
  23. const res = await this.client.get("statuses/home_timeline", {
  24. count: 20,
  25. include_entities: true,
  26. tweet_mode: "extended",
  27. max_id: maxId
  28. });
  29. return res.data;
  30. }
  31. public async fetchUser(userId) {
  32. const res = await this.client.get("users/show", { user_id: userId });
  33. return res.data;
  34. }
  35. public async fetchUserFromScreenName(screenName) {
  36. const res = await this.client.get("users/lookup", {
  37. screen_name: screenName
  38. });
  39. return res.data;
  40. }
  41. public async fetchUserTimeline(userId, maxId?) {
  42. const res = await this.client.get("statuses/user_timeline", {
  43. user_id: userId,
  44. max_id: maxId,
  45. include_entities: true,
  46. tweet_mode: "extended",
  47. count: 20
  48. });
  49. return res.data;
  50. }
  51. public async createFriendship(userId) {
  52. return await this.client.post("friendships/create", { user_id: userId });
  53. }
  54. public async destroyFriendship(userId) {
  55. return await this.client.post("friendships/destroy", { user_id: userId });
  56. }
  57. public async muteUser(userId) {
  58. return await this.client.post("mutes/users/create", { user_id: userId });
  59. }
  60. public async unmuteUser(userId) {
  61. return await this.client.post("mutes/users/destroy", { user_id: userId });
  62. }
  63. public async blockUser(userId) {
  64. return await this.client.post("blocks/create", { user_id: userId });
  65. }
  66. public async unblockUser(userId) {
  67. return await this.client.post("blocks/destroy", { user_id: userId });
  68. }
  69. public async tweet(status, retweet?, replyToStatusId?) {
  70. if (status.length === 0 && retweet) {
  71. // Simple retweet
  72. return await this.client.post("statuses/retweet", {
  73. id: retweet.data.id_str
  74. });
  75. } else if (!retweet) {
  76. // Simple tweet
  77. return await this.client.post("statuses/update", {
  78. status: status,
  79. in_reply_to_status_id: replyToStatusId
  80. });
  81. } else if (status.length > 0 && retweet) {
  82. // Quoted tweet
  83. const url =
  84. "https://twitter.com/" +
  85. retweet.data.user.screen_name +
  86. "/status/" +
  87. retweet.data.id_str;
  88. return await this.client.post("statuses/update", {
  89. status: status,
  90. attachment_url: url
  91. });
  92. } else {
  93. return;
  94. }
  95. }
  96. public async fetchFriends(userId) {
  97. let friends = [];
  98. let cursor = -1;
  99. while (cursor != 0) {
  100. const res = await this.client.get("friends/list", {
  101. user_id: userId,
  102. count: 200,
  103. include_user_entities: false,
  104. cursor: cursor
  105. });
  106. cursor = res.data["next_cursor"];
  107. friends = friends.concat(res.data["users"]);
  108. }
  109. return friends;
  110. }
  111. public async likeTweet(tweetId) {
  112. return await this.client.post("favorites/create", { id: tweetId });
  113. }
  114. public async unlikeTweet(tweetId) {
  115. return await this.client.post("favorites/destroy", { id: tweetId });
  116. }
  117. public async fetchTweet(tweetId) {
  118. return await this.client.get("statuses/show", {
  119. id: tweetId,
  120. tweet_mode: "extended"
  121. });
  122. }
  123. public async searchRecentTweets(keyword: string, maxId?: string) {
  124. const res = await this.client.get("search/tweets", {
  125. q: keyword,
  126. result_type: "recent",
  127. count: 20,
  128. include_entities: true,
  129. tweet_mode: "extended",
  130. max_id: maxId
  131. });
  132. return res.data;
  133. }
  134. public async searchPopularTweets(keyword: string, maxId?: string) {
  135. const res = await this.client.get("search/tweets", {
  136. q: keyword,
  137. result_type: "popular",
  138. count: 20,
  139. include_entities: true,
  140. tweet_mode: "extended",
  141. max_id: maxId
  142. });
  143. return res.data;
  144. }
  145. public async searchUsers(keyword: string, page?: number) {
  146. const res = await this.client.get("users/search", {
  147. q: keyword,
  148. count: 10,
  149. include_entities: true,
  150. page: page
  151. });
  152. return res.data;
  153. }
  154. }