twitter-api.ts 7.3 KB

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