twitter-api.ts 5.1 KB

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