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. import { PgpKeyServerProvider } from "../../providers/pgp-key-server/pgp-key-server";
  6. @Injectable()
  7. export class TwitterApiProvider {
  8. client: Twit;
  9. constructor(public http: HttpClient, private storage: Storage, private openpgp: PgpKeyServerProvider,) {
  10. this.initApi();
  11. }
  12. /**
  13. * initialize Twitter API provider
  14. */
  15. public async initApi() {
  16. const access_token_key = await this.storage.get("accessTokenKey");
  17. const access_token_secret = await this.storage.get("accessTokenSecret");
  18. if (access_token_key && access_token_secret) {
  19. this.client = new Twit({
  20. consumer_key: "ewAyFOepelB1Dgczl7LReD3pN",
  21. consumer_secret: "xblu59EZCTrNnW5waDvPjkpaiothQBjDCaJlLaxuezfIdol9Ot",
  22. access_token: access_token_key,
  23. access_token_secret: access_token_secret,
  24. timeout_ms: 60 * 1000 // optional HTTP request timeout to apply to all requests.
  25. });
  26. await this.openpgp.clearStoredKeys();
  27. } else {
  28. console.error(
  29. "Access Token Key and Secret not set. Creating Twit-client not possible."
  30. );
  31. console.info("This error can be ignored if no user is logged in.");
  32. }
  33. }
  34. /**
  35. * fetch home feed in batches of 20 tweets starting at maxId
  36. * @param maxId tweet id
  37. */
  38. public async fetchHomeFeed(maxId ? ) {
  39. const res = await this.client.get("statuses/home_timeline", {
  40. count: 15,
  41. include_entities: true,
  42. tweet_mode: "extended",
  43. max_id: maxId
  44. })
  45. return res.data;
  46. }
  47. /**
  48. * fetch user object to given id
  49. * @param userId user id
  50. */
  51. public async fetchUser(userId) {
  52. const res = await this.client.get("users/show", { user_id: userId });
  53. // console.log("User is:",res.data);
  54. return res.data;
  55. }
  56. /**
  57. * lookup user for screenname
  58. */
  59. public async fetchUserFromScreenName(screenName) {
  60. const res = await this.client.get("users/lookup", {
  61. screen_name: screenName
  62. });
  63. return res.data;
  64. }
  65. /**
  66. * fetch user feed in batches of 20 tweets starting at maxId
  67. * @param userId user id
  68. * @param maxId max tweet id
  69. */
  70. public async fetchUserTimeline(userId, maxId ? ) {
  71. try {
  72. const res = await this.client.get("statuses/user_timeline", {
  73. user_id: userId,
  74. max_id: maxId,
  75. include_entities: true,
  76. tweet_mode: "extended",
  77. count: 20
  78. });
  79. return res.data;
  80. } catch (e) {
  81. console.error(e);
  82. return [];
  83. }
  84. }
  85. /**
  86. * Follow user
  87. * @param userId user id
  88. */
  89. public async createFriendship(userId) {
  90. return await this.client.post("friendships/create", { user_id: userId });
  91. }
  92. /**
  93. * Unfollow user
  94. * @param userId user id
  95. */
  96. public async destroyFriendship(userId) {
  97. return await this.client.post("friendships/destroy", { user_id: userId });
  98. }
  99. /**
  100. * Mute user
  101. * @param userId user id
  102. */
  103. public async muteUser(userId) {
  104. return await this.client.post("mutes/users/create", { user_id: userId });
  105. }
  106. /**
  107. * Unmute user
  108. * @param userId user id
  109. */
  110. public async unmuteUser(userId) {
  111. return await this.client.post("mutes/users/destroy", { user_id: userId });
  112. }
  113. /**
  114. * Block user
  115. * @param userId user id
  116. */
  117. public async blockUser(userId) {
  118. return await this.client.post("blocks/create", { user_id: userId });
  119. }
  120. /**
  121. * Unblock user
  122. * @param userId user id
  123. */
  124. public async unblockUser(userId) {
  125. return await this.client.post("blocks/destroy", { user_id: userId });
  126. }
  127. /**
  128. * Post tweet
  129. * @param status tweet message
  130. * @param retweet tweet object to retweet
  131. * @param replyToStatusId tweet id to reply to
  132. */
  133. public async tweet(status, retweet ? , replyToStatusId ? ) {
  134. if (status.length === 0 && retweet) {
  135. // Simple retweet
  136. return await this.client.post("statuses/retweet", {
  137. id: retweet.data.id_str
  138. });
  139. } else if (!retweet) {
  140. // Simple tweet
  141. return await this.client.post("statuses/update", {
  142. status: status,
  143. in_reply_to_status_id: replyToStatusId
  144. });
  145. } else if (status.length > 0 && retweet) {
  146. // Quoted tweet
  147. const url =
  148. "https://twitter.com/" +
  149. retweet.data.user.screen_name +
  150. "/status/" +
  151. retweet.data.id_str;
  152. return await this.client.post("statuses/update", {
  153. status: status,
  154. attachment_url: url
  155. });
  156. } else {
  157. return;
  158. }
  159. }
  160. /**
  161. * Fetch friends for user id
  162. * @param userId user id
  163. */
  164. public async fetchFriends(userId) {
  165. let friends = [];
  166. let cursor = -1;
  167. while (cursor != 0) {
  168. const res = await this.client.get("friends/list", {
  169. user_id: userId,
  170. count: 200,
  171. include_user_entities: false,
  172. cursor: cursor
  173. });
  174. cursor = res.data["next_cursor"];
  175. friends = friends.concat(res.data["users"]);
  176. console.log("friends are:",friends);
  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. }