twitter-api.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 fetchUserTimeline(userId, maxId?) {
  36. const res = await this.client.get("statuses/user_timeline", {
  37. user_id: userId,
  38. max_id: maxId,
  39. include_entities: true,
  40. tweet_mode: "extended",
  41. count: 20
  42. });
  43. return res.data;
  44. }
  45. public async createFriendship(userId) {
  46. return await this.client.post("friendships/create", { user_id: userId });
  47. }
  48. public async destroyFriendship(userId) {
  49. return await this.client.post("friendships/destroy", { user_id: userId });
  50. }
  51. public async muteUser(userId) {
  52. return await this.client.post("mutes/users/create", { user_id: userId });
  53. }
  54. public async unmuteUser(userId) {
  55. return await this.client.post("mutes/users/destroy", { user_id: userId });
  56. }
  57. public async blockUser(userId) {
  58. return await this.client.post("blocks/create", { user_id: userId });
  59. }
  60. public async unblockUser(userId) {
  61. return await this.client.post("blocks/destroy", { user_id: userId });
  62. }
  63. public async tweet(status, retweet?) {
  64. if (status.length === 0 && retweet) {
  65. // Simple retweet
  66. return await this.client.post("statuses/retweet", {
  67. id: retweet.data.id_str
  68. });
  69. } else if (!retweet) {
  70. // Simple tweet
  71. return await this.client.post("statuses/update", { status: status });
  72. } else if (status.length > 0 && retweet) {
  73. // Quoted tweet
  74. const url =
  75. "https://twitter.com/" +
  76. retweet.data.user.screen_name +
  77. "/status/" +
  78. retweet.data.id_str;
  79. return await this.client.post("statuses/update", {
  80. status: status,
  81. attachment_url: url
  82. });
  83. } else {
  84. return;
  85. }
  86. }
  87. public async fetchFriends(userId) {
  88. let friends = [];
  89. let cursor = -1;
  90. while (cursor != 0) {
  91. const res = await this.client.get("friends/list", {
  92. user_id: userId,
  93. count: 200,
  94. include_user_entities: false,
  95. cursor: cursor
  96. });
  97. cursor = res.data["next_cursor"];
  98. friends = friends.concat(res.data["users"]);
  99. }
  100. return friends;
  101. }
  102. public async likeTweet(tweetId) {
  103. return await this.client.post("favorites/create", { id: tweetId });
  104. }
  105. public async unlikeTweet(tweetId) {
  106. return await this.client.post("favorites/destroy", { id: tweetId });
  107. }
  108. public async fetchTweet(tweetId) {
  109. return await this.client.get("statuses/show", {
  110. id: tweetId,
  111. tweet_mode: "extended"
  112. });
  113. }
  114. }