twitter-api.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. /**
  12. * initialize Twitter API provider
  13. */
  14. public async initApi() {
  15. console.log('initAPi iis initting');
  16. const access_token_key = await this.storage.get("accessTokenKey");
  17. const access_token_secret = await this.storage.get("accessTokenSecret");
  18. console.log('inside twitter initapi access_token_key:', access_token_key, 'access_token_secret', access_token_secret);
  19. // if (access_token_key && access_token_secret) {
  20. if (1) {
  21. this.client = new Twit({
  22. consumer_key: "ewAyFOepelB1Dgczl7LReD3pN",
  23. consumer_secret: "xblu59EZCTrNnW5waDvPjkpaiothQBjDCaJlLaxuezfIdol9Ot",
  24. access_token: "1205934370546241541-Ma5zbQIMPTVnbRIYTCaUGgpQnqwApu",
  25. access_token_secret: "mnCbVPV0IuQ8UeRCzZSfM6qnn29m41tkAjXVHTBpySP5i",
  26. timeout_ms: 60 * 1000 // optional HTTP request timeout to apply to all requests.
  27. });
  28. console.log('new twit client created successfully');
  29. } else {
  30. console.error(
  31. "Access Token Key and Secret not set. Creating Twit-client not possible."
  32. );
  33. console.info("This error can be ignored if no user is logged in.");
  34. }
  35. }
  36. /**
  37. * fetch home feed in batches of 20 tweets starting at maxId
  38. * @param maxId tweet id
  39. */
  40. public async fetchHomeFeed(maxId ? ) {
  41. console.log('fetchine home feed', this.client);
  42. const res = await this.client.get("statuses/home_timeline", {
  43. count: 20,
  44. include_entities: true,
  45. tweet_mode: "extended",
  46. max_id: maxId
  47. })
  48. console.log('res is:', res);
  49. return res.data;
  50. }
  51. /**
  52. * fetch user object to given id
  53. * @param userId user id
  54. */
  55. public async fetchUser(userId) {
  56. const res = await this.client.get("users/show", { user_id: userId });
  57. return res.data;
  58. }
  59. /**
  60. * lookup user for screenname
  61. */
  62. public async fetchUserFromScreenName(screenName) {
  63. const res = await this.client.get("users/lookup", {
  64. screen_name: screenName
  65. });
  66. return res.data;
  67. }
  68. /**
  69. * fetch user feed in batches of 20 tweets starting at maxId
  70. * @param userId user id
  71. * @param maxId max tweet id
  72. */
  73. public async fetchUserTimeline(userId, maxId ? ) {
  74. try {
  75. const res = await this.client.get("statuses/user_timeline", {
  76. user_id: userId,
  77. max_id: maxId,
  78. include_entities: true,
  79. tweet_mode: "extended",
  80. count: 20
  81. });
  82. return res.data;
  83. } catch (e) {
  84. console.error(e);
  85. return [];
  86. }
  87. }
  88. /**
  89. * Follow user
  90. * @param userId user id
  91. */
  92. public async createFriendship(userId) {
  93. return await this.client.post("friendships/create", { user_id: userId });
  94. }
  95. /**
  96. * Unfollow user
  97. * @param userId user id
  98. */
  99. public async destroyFriendship(userId) {
  100. return await this.client.post("friendships/destroy", { user_id: userId });
  101. }
  102. /**
  103. * Mute user
  104. * @param userId user id
  105. */
  106. public async muteUser(userId) {
  107. return await this.client.post("mutes/users/create", { user_id: userId });
  108. }
  109. /**
  110. * Unmute user
  111. * @param userId user id
  112. */
  113. public async unmuteUser(userId) {
  114. return await this.client.post("mutes/users/destroy", { user_id: userId });
  115. }
  116. /**
  117. * Block user
  118. * @param userId user id
  119. */
  120. public async blockUser(userId) {
  121. return await this.client.post("blocks/create", { user_id: userId });
  122. }
  123. /**
  124. * Unblock user
  125. * @param userId user id
  126. */
  127. public async unblockUser(userId) {
  128. return await this.client.post("blocks/destroy", { user_id: userId });
  129. }
  130. /**
  131. * Post tweet
  132. * @param status tweet message
  133. * @param retweet tweet object to retweet
  134. * @param replyToStatusId tweet id to reply to
  135. */
  136. public async tweet(status, retweet ? , replyToStatusId ? ) {
  137. if (status.length === 0 && retweet) {
  138. // Simple retweet
  139. return await this.client.post("statuses/retweet", {
  140. id: retweet.data.id_str
  141. });
  142. } else if (!retweet) {
  143. // Simple tweet
  144. return await this.client.post("statuses/update", {
  145. status: status,
  146. in_reply_to_status_id: replyToStatusId
  147. });
  148. } else if (status.length > 0 && retweet) {
  149. // Quoted tweet
  150. const url =
  151. "https://twitter.com/" +
  152. retweet.data.user.screen_name +
  153. "/status/" +
  154. retweet.data.id_str;
  155. return await this.client.post("statuses/update", {
  156. status: status,
  157. attachment_url: url
  158. });
  159. } else {
  160. return;
  161. }
  162. }
  163. /**
  164. * Fetch friends for user id
  165. * @param userId user id
  166. */
  167. public async fetchFriends(userId) {
  168. let friends = [];
  169. let cursor = -1;
  170. while (cursor != 0) {
  171. const res = await this.client.get("friends/list", {
  172. user_id: userId,
  173. count: 200,
  174. include_user_entities: false,
  175. cursor: cursor
  176. });
  177. cursor = res.data["next_cursor"];
  178. friends = friends.concat(res.data["users"]);
  179. }
  180. return friends;
  181. }
  182. /**
  183. * Like tweet
  184. * @param tweetId tweet id
  185. */
  186. public async likeTweet(tweetId) {
  187. return await this.client.post("favorites/create", { id: tweetId });
  188. }
  189. /**
  190. * Remove like from tweet
  191. * @param tweetId tweet id
  192. */
  193. public async unlikeTweet(tweetId) {
  194. return await this.client.post("favorites/destroy", { id: tweetId });
  195. }
  196. /**
  197. * Retrieve a single tweet
  198. * @param tweetId tweet id
  199. */
  200. public async fetchTweet(tweetId) {
  201. return await this.client.get("statuses/show", {
  202. id: tweetId,
  203. tweet_mode: "extended"
  204. });
  205. }
  206. /**
  207. * Search Twitter for recent tweets
  208. * Since the results are loaded in batches of 20 tweets, max tweet id is a reference for the next batch
  209. * @param keyword keyword
  210. * @param maxId max tweet id
  211. */
  212. public async searchRecentTweets(keyword: string, maxId ? : string) {
  213. const res = await this.client.get("search/tweets", {
  214. q: keyword,
  215. result_type: "recent",
  216. count: 20,
  217. include_entities: true,
  218. tweet_mode: "extended",
  219. max_id: maxId
  220. });
  221. return res.data;
  222. }
  223. /**
  224. * Search Twitter for popular tweets
  225. * Since the results are loaded in batches of 20 tweets, max tweet id is a reference for the next batch
  226. * @param keyword keyword
  227. * @param maxId max tweet id
  228. */
  229. public async searchPopularTweets(keyword: string, maxId ? : string) {
  230. const res = await this.client.get("search/tweets", {
  231. q: keyword,
  232. result_type: "popular",
  233. count: 20,
  234. include_entities: true,
  235. tweet_mode: "extended",
  236. max_id: maxId
  237. });
  238. return res.data;
  239. }
  240. /**
  241. * Search Twitter for users
  242. * Since the API returns users paginated, a page has to be provided
  243. * @param keyword keyword
  244. * @param page page number
  245. */
  246. public async searchUsers(keyword: string, page ? : number) {
  247. const res = await this.client.get("users/search", {
  248. q: keyword,
  249. count: 10,
  250. include_entities: true,
  251. page: page
  252. });
  253. return res.data;
  254. }
  255. /**
  256. * Updates the profile description
  257. * @param description profile description
  258. */
  259. public async updateProfileDescription(description: string) {
  260. const res = await this.client.post("account/update_profile", {
  261. description: description
  262. });
  263. return res.data;
  264. }
  265. }