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. @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. const access_token_key = await this.storage.get("accessTokenKey");
  16. const access_token_secret = await this.storage.get("accessTokenSecret");
  17. if (access_token_key && access_token_secret) {
  18. this.client = new Twit({
  19. consumer_key: "UxZkbKotkr8Uc6seupnaZ1kDE",
  20. consumer_secret: "fEAas8iugR60FOEXsFG0iajq6oyfIIXRBVMlTgWxBd1stWIKHq",
  21. access_token: access_token_key,
  22. access_token_secret: access_token_secret,
  23. timeout_ms: 60 * 1000 // optional HTTP request timeout to apply to all requests.
  24. });
  25. } else {
  26. console.error(
  27. "Access Token Key and Secret not set. Creating Twit-client not possible."
  28. );
  29. console.info("This error can be ignored if no user is logged in.");
  30. }
  31. }
  32. /**
  33. * fetch home feed in batches of 20 tweets starting at maxId
  34. * @param maxId tweet id
  35. */
  36. public async fetchHomeFeed(maxId?) {
  37. const res = await this.client.get("statuses/home_timeline", {
  38. count: 20,
  39. include_entities: true,
  40. tweet_mode: "extended",
  41. max_id: maxId
  42. },function(req,reply,response){
  43. console.log('response from callback is :',response);
  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. console.log('params are:',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. }
  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. }