twitter-api.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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() {
  23. return await this.client.get("statuses/home_timeline", {
  24. count: 10,
  25. include_entities: true,
  26. tweet_mode: "extended"
  27. });
  28. }
  29. public async fetchHomeFeedSince(id) {
  30. return await this.client.get("statuses/home_timeline", {
  31. max_id: id,
  32. count: 15,
  33. include_entities: true,
  34. tweet_mode: "extended"
  35. });
  36. }
  37. public async fetchUser(userId) {
  38. const res = await this.client.get("users/lookup", { user_id: userId });
  39. return res.data[0];
  40. }
  41. public async fetchUserTimeline(userId) {
  42. const res = await this.client.get("statuses/user_timeline", {
  43. user_id: userId,
  44. include_entities: true,
  45. tweet_mode: "extended",
  46. count: 20
  47. });
  48. return res.data;
  49. }
  50. public async fetchUserTimelineSince(userId, maxId) {
  51. const res = await this.client.get("statuses/user_timeline", {
  52. user_id: userId,
  53. max_id: maxId,
  54. include_entities: true,
  55. tweet_mode: "extended",
  56. count: 20
  57. });
  58. return res.data;
  59. }
  60. public async destroyFriendship(userId) {
  61. return await this.client.post("friendships/destroy", { user_id: userId });
  62. }
  63. public async muteUser(userId) {
  64. return await this.client.post("mutes/users/create", { user_id: userId });
  65. }
  66. public async blockUser(userId) {
  67. return await this.client.post("blocks/create", { user_id: userId });
  68. }
  69. public async tweet(status) {
  70. return await this.client.post("statuses/update", { status: status });
  71. }
  72. public async fetchFriends(userId) {
  73. let friends = [];
  74. let cursor = -1;
  75. while (cursor != 0) {
  76. const res = await this.client.get("friends/list", {
  77. user_id: userId,
  78. count: 200,
  79. include_user_entities: false,
  80. cursor: cursor
  81. });
  82. cursor = res.data["next_cursor"];
  83. friends = friends.concat(res.data["users"]);
  84. }
  85. return friends;
  86. }
  87. }