twitter-api.ts 7.2 KB

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