twitter-api.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. consumer_key: string = 'UxZkbKotkr8Uc6seupnaZ1kDE';
  8. consumer_secret: string = 'fEAas8iugR60FOEXsFG0iajq6oyfIIXRBVMlTgWxBd1stWIKHq';
  9. access_token_key: string;
  10. access_token_secret: string;
  11. client;
  12. constructor(public http: HttpClient, private storage: Storage) {
  13. Promise.all([this.storage.get('accessTokenKey'), this.storage.get('accessTokenSecret')]).then(values => {
  14. this.access_token_key = values[0];
  15. this.access_token_secret = values[1];
  16. this.initApi();
  17. });
  18. }
  19. private initApi() {
  20. this.client = new Twit({
  21. consumer_key: this.consumer_key,
  22. consumer_secret: this.consumer_secret,
  23. access_token: this.access_token_key,
  24. access_token_secret: this.access_token_secret,
  25. timeout_ms: 60 * 1000, // optional HTTP request timeout to apply to all requests.
  26. })
  27. }
  28. public fetchHomeFeed() {
  29. return this.client.get('statuses/home_timeline', { count: 10 })
  30. .then(res => {
  31. return res;
  32. })
  33. .catch(err => {
  34. console.log(err);
  35. });
  36. }
  37. public fetchHomeFeedSince(id) {
  38. return this.client.get('statuses/home_timeline', { max_id: id, count: 15 })
  39. .then(res => {
  40. return res;
  41. })
  42. .catch(err => {
  43. console.log(err);
  44. });
  45. }
  46. public fetchUser(userId) {
  47. return this.client.get('users/lookup', { user_id: userId })
  48. .then(res => {
  49. return res.data[0];
  50. })
  51. .catch(err => {
  52. console.log(err);
  53. });
  54. }
  55. public fetchUserTimeline(userId) {
  56. return this.client.get('statuses/user_timeline', { user_id: userId })
  57. .then(res => {
  58. return res.data;
  59. })
  60. .catch(err => {
  61. console.log(err);
  62. });
  63. }
  64. public fetchUserTimelineSince(userId, maxId) {
  65. return this.client.get('statuses/user_timeline', { user_id: userId, max_id: maxId })
  66. .then(res => {
  67. return res.data;
  68. })
  69. .catch(err => {
  70. console.log(err);
  71. });
  72. }
  73. }