twitter-api.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. try {
  50. const res = await this.client.get("statuses/user_timeline", {
  51. user_id: userId,
  52. max_id: maxId,
  53. include_entities: true,
  54. tweet_mode: "extended",
  55. count: 20
  56. });
  57. return res.data;
  58. } catch (e) {
  59. console.error(e);
  60. return [];
  61. }
  62. }
  63. public async createFriendship(userId) {
  64. return await this.client.post("friendships/create", { user_id: userId });
  65. }
  66. public async destroyFriendship(userId) {
  67. return await this.client.post("friendships/destroy", { user_id: userId });
  68. }
  69. public async muteUser(userId) {
  70. return await this.client.post("mutes/users/create", { user_id: userId });
  71. }
  72. public async unmuteUser(userId) {
  73. return await this.client.post("mutes/users/destroy", { user_id: userId });
  74. }
  75. public async blockUser(userId) {
  76. return await this.client.post("blocks/create", { user_id: userId });
  77. }
  78. public async unblockUser(userId) {
  79. return await this.client.post("blocks/destroy", { user_id: userId });
  80. }
  81. public async tweet(status, retweet?, replyToStatusId?) {
  82. if (status.length === 0 && retweet) {
  83. // Simple retweet
  84. return await this.client.post("statuses/retweet", {
  85. id: retweet.data.id_str
  86. });
  87. } else if (!retweet) {
  88. // Simple tweet
  89. return await this.client.post("statuses/update", {
  90. status: status,
  91. in_reply_to_status_id: replyToStatusId
  92. });
  93. } else if (status.length > 0 && retweet) {
  94. // Quoted tweet
  95. const url =
  96. "https://twitter.com/" +
  97. retweet.data.user.screen_name +
  98. "/status/" +
  99. retweet.data.id_str;
  100. return await this.client.post("statuses/update", {
  101. status: status,
  102. attachment_url: url
  103. });
  104. } else {
  105. return;
  106. }
  107. }
  108. public async fetchFriends(userId) {
  109. let friends = [];
  110. let cursor = -1;
  111. while (cursor != 0) {
  112. const res = await this.client.get("friends/list", {
  113. user_id: userId,
  114. count: 200,
  115. include_user_entities: false,
  116. cursor: cursor
  117. });
  118. cursor = res.data["next_cursor"];
  119. friends = friends.concat(res.data["users"]);
  120. }
  121. return friends;
  122. }
  123. public async likeTweet(tweetId) {
  124. return await this.client.post("favorites/create", { id: tweetId });
  125. }
  126. public async unlikeTweet(tweetId) {
  127. return await this.client.post("favorites/destroy", { id: tweetId });
  128. }
  129. public async fetchTweet(tweetId) {
  130. return await this.client.get("statuses/show", {
  131. id: tweetId,
  132. tweet_mode: "extended"
  133. });
  134. }
  135. public async searchRecentTweets(keyword: string, maxId?: string) {
  136. const res = await this.client.get("search/tweets", {
  137. q: keyword,
  138. result_type: "recent",
  139. count: 20,
  140. include_entities: true,
  141. tweet_mode: "extended",
  142. max_id: maxId
  143. });
  144. return res.data;
  145. }
  146. public async searchPopularTweets(keyword: string, maxId?: string) {
  147. const res = await this.client.get("search/tweets", {
  148. q: keyword,
  149. result_type: "popular",
  150. count: 20,
  151. include_entities: true,
  152. tweet_mode: "extended",
  153. max_id: maxId
  154. });
  155. return res.data;
  156. }
  157. public async searchUsers(keyword: string, page?: number) {
  158. const res = await this.client.get("users/search", {
  159. q: keyword,
  160. count: 10,
  161. include_entities: true,
  162. page: page
  163. });
  164. return res.data;
  165. }
  166. public async updateProfileDescription(description: string) {
  167. const res = await this.client.post("account/update_profile", {
  168. description: description
  169. });
  170. return res.data;
  171. }
  172. }