PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/knallgrau/twitter.js

http://github.com/matthias/aida
JavaScript | 662 lines | 251 code | 66 blank | 345 comment | 37 complexity | a49e8b6165bec892a527e0f78777efb8 MD5 | raw file
Possible License(s): Apache-2.0
  1. importModule("javascript.prototype");
  2. importModule("helma.http", "http");
  3. importModule('helma.logging', 'logging');
  4. logging.enableResponseLog();
  5. var logger = logging.getLogger(__name__);
  6. /**
  7. * @fileinfo
  8. * Twitter API for Rhino / Helma-NG
  9. *
  10. * @requires
  11. * @version 0.2
  12. * @author Matthias Platzer AT aida.at
  13. * @see http://groups.google.com/group/twitter-development-talk/web/api-documentation
  14. */
  15. var info = {
  16. name : "Twitter API for Rhino / Helma-NG",
  17. version : "0.2",
  18. status : "development"
  19. }
  20. /** @ignore */
  21. function TwitterApiException(result, options) {
  22. var msg = "TwitterApiException: " + result.code + " " + result.message;
  23. if (result.error) { msg += " (" + result.error + ")" };
  24. if (result.url || result.request) { msg += " : " + result.url || result.request };
  25. if (options) msg += " called with options " + uneval(options);
  26. logger.error(msg);
  27. this.code = result.code;
  28. this.message = result.message;
  29. this.url = result.url;
  30. this.content = result.content;
  31. this.msg = msg;
  32. this.toString = function() {
  33. return msg;
  34. };
  35. }
  36. /** @ignore */
  37. function call(url, options) {
  38. if (!url.startsWith('http://twitter.com')) {
  39. throw new TwitterApiException({
  40. code : 400,
  41. message : 'Bad Request',
  42. url : url,
  43. content : 'API endpoint must start with http://twitter.com'
  44. }, options);
  45. }
  46. var param = Object.clone(options || {});
  47. var credentials = {
  48. username : param.username,
  49. password : param.password
  50. };
  51. var client = new http.Client();
  52. var method = (param.method || "GET").toUpperCase();
  53. client.setMethod(method);
  54. delete param.method;
  55. client.setCredentials(param.username, param.password);
  56. delete param.username;
  57. delete param.password;
  58. var postBody = param.postBody;
  59. delete param.postBody;
  60. if (method == 'GET') {
  61. var query = Object.toQueryString(param);
  62. if (query) url += (url.include('?') ? '&' : '?') + query;
  63. } else if (method == 'POST' || method == 'PUT') {
  64. logger.info("setContent to " + uneval(postBody || param));
  65. client.setContent(postBody || param);
  66. }
  67. try {
  68. var result = client.getUrl(url);
  69. if (options.since && result.code == 304) {
  70. return [];
  71. }
  72. } catch(e) {
  73. throw new TwitterApiException({}, options);
  74. }
  75. if (result.code != 200) {
  76. throw new TwitterApiException(result, options);
  77. }
  78. return result.content.evalJSON();
  79. }
  80. /**
  81. * Status wrapper object.
  82. * Status (and timeline) API calls return object(s) including information about the status and the user.
  83. *
  84. * @return {object} Returns a result object containing the following properties:
  85. * <ul>
  86. * <li><tt>id</tt> {Number} id of the status posting
  87. * <li><tt>text</tt> {String} status posting
  88. * <li><tt>source</tt> {String} txt|web|.. may also contain html markup (a link)
  89. * <li><tt>created_at</tt> <Date>
  90. * <li><tt>user</tt> {User}
  91. * </ul>
  92. *
  93. * @see User
  94. */
  95. function Status(status) {
  96. if (status.created_at) status.created_at = new Date(status.created_at);
  97. if (status.user && !status.user instanceof User) status.user = new User(status.user);
  98. Object.extend(this, status);
  99. }
  100. /**
  101. * Status wrapper object.
  102. * User (and friends) API calls return object(s) with information the user.
  103. * Status objects also contain a user object.
  104. *
  105. * @return {object}
  106. * <ul>
  107. * <li><tt>id</tt> {Number}
  108. * <li><tt>profile_image_url</tt> {Number}
  109. * <li><tt>screen_name</tt> {String}
  110. * <li><tt>url</tt> {String}
  111. * <li><tt>name</tt> {String}
  112. * <li><tt>description</tt> {String}
  113. * <li><tt>followers_count</tt> {Number}
  114. * <li><tt>location</tt> {String}
  115. * <li><tt>protected</tt> {Boolean}
  116. * </ul>
  117. */
  118. function User(user) {
  119. Object.extend(this, user);
  120. }
  121. /**
  122. * Favorite wrapper object.
  123. * Favorite API calls return object(s) with information about the favorite.
  124. *
  125. * @return {object}
  126. * <ul>
  127. * <li><tt>created_at</tt> {Date}
  128. * <li><tt>in_reply_to_status_id</tt> {Number}
  129. * <li><tt>in_reply_to_user_id</tt> {Number}
  130. * <li><tt>favorited</tt> {Boolean}
  131. * <li><tt>source</tt> {String}
  132. * <li><tt>id</tt> {Number}
  133. * <li><tt>user</tt> {User}
  134. * <li><tt>truncated</tt> {Boolean}
  135. * <li><tt>text</tt> {String}
  136. * </ul>
  137. */
  138. function Favorite(favorite) {
  139. if (favorite.created_at) favorite.created_at = new Date(favorite.created_at);
  140. if (favorite.user && !favorite.user instanceof User) favorite.user = new User(favorite.user);
  141. Object.extend(this, favorite);
  142. }
  143. /*** Status Methods ***/
  144. /**
  145. * Returns the 20 most recent statuses from non-protected users who have set a custom user icon.
  146. * Does not require authentication.
  147. *
  148. * @param {object} [options]
  149. * @param {String} [since_id] Returns only public statuses with an ID greater than (that is, more recent than) the specified ID.
  150. * @return {[Status]} Returns an array of twitter Status objects
  151. * @throws TwitterApiException
  152. */
  153. function publicTimeline(options) {
  154. return call(
  155. 'http://twitter.com/statuses/public_timeline.json',
  156. options || {}
  157. );
  158. }
  159. /**
  160. * Returns the 20 most recent statuses posted in the last 24 hours from the authenticating user and that user's friends.
  161. * It's also possible to request another user's friends_timeline via the id parameter below.
  162. *
  163. * @param {object} options
  164. * @param {Number} [options.id] Specifies the ID of the user for whom to return the friends_timeline.
  165. * @param {String} [options.id] Specifies the screen name of the user for whom to return the friends_timeline.
  166. * @param {Date} [options.since] Narrows the returned results to just those statuses created after the specified date.
  167. * Notes: Only works in conjunction with username / password.
  168. * Using the page parameter will result in ignoring the since parameter.
  169. * @param {Number} [options.page] Gets the 20 next most recent statuses from the authenticating user and that user's friends.
  170. * @param {String} [options.username] Screenname or email of user for authentication.
  171. * @param {String} [options.password] plain text password of user for authentication
  172. *
  173. * @return {[Status]} Returns an array of twitter Status objects
  174. * @throws TwitterApiException
  175. */
  176. function friendsTimeline(options) {
  177. return call(
  178. 'http://twitter.com/statuses/friends_timeline' + ((options.id) ? '/' + encodeURIComponent(options.id) : '') + '.json',
  179. options.rejectKeys(/^id$/)
  180. ).map(function(status) {
  181. return new Status(status);
  182. });
  183. }
  184. /**
  185. * Returns the 20 most recent statuses posted in the last 24 hours from the authenticating user.
  186. * It's also possible to request another user's timeline via the id parameter below.
  187. *
  188. * @param {object} options
  189. * @param {Number} [options.id] Specifies the ID or screen name of the user for whom to return the timeline.
  190. * Alternatively you may specify username + password to authenticate a user.
  191. * @param {Date} [options.since] Narrows the returned results to just those statuses created after the specified date.
  192. * @param {Number} [options.count] Specifies the number of statuses to retrieve. May not be greater than 20 for performance purposes.
  193. * @param {String} [options.username] Screenname or email of user for authentication.
  194. * @param {String} [options.password] plain text password of user for authentication
  195. *
  196. * @return {[Status]} Returns an array of twitter Status objects
  197. * @throws TwitterApiException
  198. */
  199. function userTimeline(options) {
  200. return call(
  201. 'http://twitter.com/statuses/user_timeline' + ((options.id) ? '/' + encodeURIComponent(options.id) : '') + '.json',
  202. options.rejectKeys(/^id$/)
  203. ).map(function(status) {
  204. return new Status(status);
  205. });
  206. }
  207. /**
  208. * Returns a single status, specified by the id parameter below.
  209. * The status's author will be returned inline.
  210. *
  211. * @param {object} options
  212. * @param {Number} id The numerical ID of the status you're trying to retrieve.
  213. * @param {String} [options.username] Screenname or email of user for authentication.
  214. * @param {String} [options.password] Plain text password of user for authentication.
  215. *
  216. * @return {Status}
  217. * @throws TwitterApiException
  218. */
  219. function showStatus(options) {
  220. return new Status( call(
  221. 'http://twitter.com/statuses/show/' + encodeURIComponent(options.id) + '.json',
  222. options.rejectKeys(/^id$/)
  223. ));
  224. }
  225. /**
  226. * Updates the authenticating user's status.
  227. * Requires user credentials and the status parameter specified below.
  228. *
  229. * @param {object} options
  230. * @param {String} options.username Screenname or email of user for authentication.
  231. * @param {String} options.password Plain text password of user for authentication.
  232. * @param {String} options.status
  233. * The text of your status update.
  234. * Be sure to URL encode as necessary.
  235. * Must not be more than 160 characters and
  236. * should not be more than 140 characters to ensure optimal display.
  237. *
  238. * @return {Status} Returns the updated status object.
  239. * @throws TwitterApiException
  240. */
  241. function updateStatus(options) {
  242. return new Status( call(
  243. 'http://twitter.com/statuses/show/' + encodeURIComponent(options.id) + '.json',
  244. Object.extend(options.rejectKeys(/^id|status$/), {
  245. method : "POST",
  246. postBody : {
  247. status : options.status
  248. }
  249. })
  250. ));
  251. }
  252. /**
  253. * Returns the 20 most recent replies (status updates prefixed with @username
  254. * posted by users who are friends with the user being replied to) to the authenticating user.
  255. * Replies are only available to the authenticating user;
  256. * you can not request a list of replies to another user whether public or protected.
  257. *
  258. * @param {object} options
  259. * @param {String} options.username Screenname or email of user for authentication.
  260. * @param {String} options.password Plain text password of user for authentication.
  261. * @param {Number} [options.page] Retrieves the 20 next most recent replies.
  262. *
  263. * @return {[status]}
  264. * @throws TwitterApiException
  265. */
  266. function replies(options) {
  267. if (!options) options = {};
  268. return call(
  269. 'http://twitter.com/statuses/replies.json',
  270. options
  271. ).map(function(status) {
  272. return new Status(status);
  273. });
  274. }
  275. /**
  276. * Destroys the status specified by the required ID parameter.
  277. * The authenticating user must be the author of the specified status.
  278. * Returns the data of the destroyed object.
  279. *
  280. * @param {object} options
  281. * @param {String} options.id The ID of the status to destroy.
  282. * @param {String} options.username Screenname or email of user for authentication.
  283. * @param {String} options.password Plain text password of user for authentication.
  284. *
  285. * @return {status}
  286. * @throws TwitterApiException
  287. */
  288. function destroyStatus(options) {
  289. return new Status( call(
  290. 'http://twitter.com/statuses/destroy/' + encodeURIComponent(options.id) + '.json',
  291. options.rejectKeys(/^id$/)
  292. ));
  293. }
  294. /*** User Methods ***/
  295. /**
  296. * Returns up to 100 of the authenticating user's friends who have
  297. * most recently updated, each with current status inline.
  298. * It's also possible to request another user's recent friends list via the id parameter below.
  299. *
  300. * @param {object} options
  301. * @param {Number} [options.id] The ID or screen name of the user for whom to request a list of friends.
  302. * If no id is present the friends list of the authenticating user will be returned
  303. * @param {String} [options.username] Screenname or email of user for authentication.
  304. * @param {String} [options.password] Plain text password of user for authentication.
  305. *
  306. * @return {User}
  307. * @throws TwitterApiException
  308. */
  309. function friends(options) {
  310. if (!options) options = {};
  311. return call(
  312. 'http://twitter.com/statuses/friends' + ((options.id) ? '/' + encodeURIComponent(options.id) : '') + '.json',
  313. options.rejectKeys(/^id$/)
  314. ).map(function(user) {
  315. return new User(user);
  316. });
  317. }
  318. /**
  319. * Returns the authenticating user's followers, each with current status inline.
  320. *
  321. * @param {object} options
  322. * @param {String} options.username Screenname or email of user for authentication.
  323. * @param {String} options.password Plain text password of user for authentication.
  324. *
  325. * @return {User}
  326. * @throws TwitterApiException
  327. */
  328. function followers(options) {
  329. if (!options) options = {};
  330. return call(
  331. 'http://twitter.com/statuses/followers.json',
  332. options
  333. ).map(function(user) {
  334. return new User(user);
  335. });
  336. }
  337. /**
  338. * Returns a list of the users currently featured on the site with their current statuses inline.
  339. *
  340. * @return {User}
  341. * @throws TwitterApiException
  342. */
  343. function featured(options) {
  344. if (!options) options = {};
  345. return call(
  346. 'http://twitter.com/statuses/featured.json',
  347. options
  348. ).map(function(user) {
  349. return new User(user);
  350. });
  351. }
  352. /**
  353. * Returns extended information of a given user, specified by ID or screen name as per the required id parameter below.
  354. * This information includes design settings, so third party developers
  355. * can theme their widgets according to a given user's preferences.
  356. * <p>Notes: If you are trying to fetch data for a user who is only giving updates to friends,
  357. * the returned text will be "You are not authorized to see this user."
  358. *
  359. * @param {object} options
  360. * @param {Number} [options.id] The ID or screen name of a user.
  361. *
  362. * @return {UserSettings}
  363. * @throws TwitterApiException
  364. */
  365. function showUser(options) {
  366. return new User( call(
  367. 'http://twitter.com/users/show/' + encodeURIComponent(options.id) + '.json',
  368. options.rejectKeys(/^id$/)
  369. ));
  370. }
  371. /*** Direct Message Methods ***/
  372. /**
  373. * Returns a list of the 20 most recent direct messages sent to the authenticating user.
  374. * The result includes detailed information about the sending and recipient users.
  375. *
  376. * @param {object} options
  377. * @param {Number} [since_id] Returns only direct messages with an ID greater than (that is, more recent than) the specified ID.
  378. * @param {Date} [options.since] Narrows the resulting list of direct messages to just those sent after the specified
  379. * @param {Number} [options.page] Retrieves the 20 next most recent direct messages.
  380. * @param {String} options.username Screenname or email of user for authentication.
  381. * @param {String} options.password Plain text password of user for authentication.
  382. *
  383. * @return {[DirectMessage]}
  384. * @throws TwitterApiException
  385. */
  386. function directMessages(options) {
  387. if (!options) options = {};
  388. return call(
  389. 'http://twitter.com/direct_messages.json',
  390. options
  391. ).map(function(data) {
  392. return new DirectMessage(data);
  393. });
  394. }
  395. /**
  396. * Returns a list of the 20 most recent direct messages sent by the authenticating user.
  397. * The result includes detailed information about the sending and recipient users.
  398. *
  399. * @param {object} options
  400. * @param {Number} [since_id] Returns only direct messages with an ID greater than (that is, more recent than) the specified ID.
  401. * @param {Date} [options.since] Narrows the resulting list of direct messages to just those sent after the specified
  402. * @param {Number} [options.page] Retrieves the 20 next most recent direct messages.
  403. * @param {String} options.username Screenname or email of user for authentication.
  404. * @param {String} options.password Plain text password of user for authentication.
  405. *
  406. * @return {[DirectMessage]}
  407. * @throws TwitterApiException
  408. */
  409. function sentDirectMessages(options) {
  410. if (!options) options = {};
  411. return call(
  412. 'http://twitter.com/direct_messages/sent.json',
  413. options
  414. ).map(function(data) {
  415. return new DirectMessage(data);
  416. });
  417. }
  418. /**
  419. * Sends a new direct message to the specified user from the authenticating user.
  420. * Requires both the user and text parameters below.
  421. * Returns the sent message when successful.
  422. *
  423. * @param {object} options
  424. * @param {String} options.username Screenname or email of user for authentication.
  425. * @param {String} options.password Plain text password of user for authentication.
  426. * @param {String} user The ID or screen name of the recipient user.
  427. * @param {String} text The text of your direct message. Keep it under 140 characters.
  428. *
  429. * @return {DirectMessage}
  430. * @throws TwitterApiException
  431. */
  432. function newDirectMessage(options) {
  433. return new DirectMessage(call(
  434. 'http://twitter.com/direct_messages/new.json',
  435. Object.extend(options.rejectKeys(/^id|user|text$/), {
  436. method : "POST",
  437. postBody : {
  438. user : options.user,
  439. text : options.text
  440. }
  441. })
  442. ));
  443. }
  444. /**
  445. * Destroys the direct message specified in the required ID parameter.
  446. * The authenticating user must be the recipient of the specified direct message.
  447. *
  448. * @param {object} options
  449. * @param {String} options.id The ID of the direct message to destroy.
  450. * @param {String} options.username Screenname or email of user for authentication.
  451. * @param {String} options.password Plain text password of user for authentication.
  452. *
  453. * @return {DirectMessage}
  454. * @throws TwitterApiException
  455. */
  456. function destroyDirectMessage(options) {
  457. return new DirectMessage( call(
  458. 'http://twitter.com/direct_messages/destroy/' + encodeURIComponent(options.id) + '.json',
  459. options.rejectKeys(/^id$/)
  460. ));
  461. }
  462. /*** Friendship Methods ***/
  463. /**
  464. * Befriends the user specified in the ID parameter as the authenticating user.
  465. * Returns the befriended user when successful.
  466. * ?? Returns a string describing the failure condition when unsuccessful.
  467. *
  468. * @param {object} options
  469. * @param {String} options.username Screenname or email of user for authentication.
  470. * @param {String} options.password Plain text password of user for authentication.
  471. * @param {String} options.id The ID or screen name of the user to befriend.
  472. *
  473. * @return {User}
  474. * @throws TwitterApiException
  475. */
  476. function createFriendship(options) {
  477. return new Friendship( call(
  478. 'http://twitter.com/friendships/create/' + encodeURIComponent(options.id) + '.json',
  479. options.rejectKeys(/^id$/)
  480. ));
  481. }
  482. /**
  483. * Discontinues friendship with the user specified in the ID parameter as the authenticating user.
  484. * Returns the un-friended user when successful.
  485. * ?? Returns a string describing the failure condition when unsuccessful.
  486. *
  487. * @param {object} options
  488. * @param {String} options.id The ID or screen name of the user with whom to discontinue friendship.
  489. * @param {String} options.username Screenname or email of user for authentication.
  490. * @param {String} options.password Plain text password of user for authentication.
  491. *
  492. * @return {User}
  493. * @throws TwitterApiException
  494. */
  495. function destroyFriendship(options) {
  496. return new Friendship( call(
  497. 'http://twitter.com/friendships/destroy/' + encodeURIComponent(options.id) + '.json',
  498. options.rejectKeys(/^id$/)
  499. ));
  500. }
  501. /*** Account Methods ***/
  502. /**
  503. * Returns OK and a response if authentication was successful.
  504. * Use this method to test if supplied user credentials are valid with minimal overhead.
  505. *
  506. * @param {object} options
  507. * @param {String} options.username Screenname or email of user for authentication.
  508. * @param {String} options.password Plain text password of user for authentication.
  509. *
  510. * @return {String}
  511. * @throws TwitterApiException
  512. */
  513. function verifyCredentials(options) {
  514. return call(
  515. 'http://twitter.com/account/verify_credentials.json',
  516. options
  517. );
  518. return true;
  519. }
  520. /**
  521. * Ends the session of the authenticating user, returning a null cookie.
  522. * Use this method to sign users out of client-facing applications like widgets.
  523. *
  524. * @param {Object} options
  525. * @param {String} options.username Screenname or email of user for authentication.
  526. * @param {String} options.password Plain text password of user for authentication.
  527. *
  528. * @return {Boolean}
  529. * @throws TwitterApiException
  530. */
  531. function endSession(options) {
  532. call(
  533. 'http://twitter.com/account/end_session',
  534. options
  535. );
  536. return true;
  537. }
  538. /*** Favorite Methods ***/
  539. /**
  540. * Returns the 20 most recent favorite statuses for the authenticating user
  541. * or user specified by the ID parameter in the requested format.
  542. *
  543. * @param {object} options
  544. * @param {Number} [options.id] The ID or screen name of the user for whom to request a list of favorite statuses.
  545. * @param {Number} [options.page] Retrieves the 20 next most recent favorite statuses.
  546. * @param {String} [options.username] Screenname or email of user for authentication.
  547. * @param {String} [options.password] Plain text password of user for authentication
  548. *
  549. * @return {[Status]} Array of Status objects
  550. * @throws TwitterApiException
  551. */
  552. function favorites(options) {
  553. if (!options) options = {};
  554. return call(
  555. 'http://twitter.com/statuses/favorites' + ((options.id) ? '/' + encodeURIComponent(options.id) : '') + '.json',
  556. options.rejectKeys(/^id$/)
  557. ).map(function(data) {
  558. return new Favorit(data);
  559. });
  560. }
  561. /**
  562. * Favorites the status specified in the ID parameter as the authenticating user.
  563. * Returns the favorite status when successful.
  564. *
  565. * @param {object} options
  566. * @param {String} options.username Screenname or email of user for authentication.
  567. * @param {String} options.password Plain text password of user for authentication.
  568. * @param {String} options.id The ID of the status to favorite.
  569. *
  570. * @return {status}
  571. * @throws TwitterApiException
  572. */
  573. function createFavorit(options) {
  574. return new Favorit( call(
  575. 'http://twitter.com/favorites/create/' + encodeURIComponent(options.id) + '.json',
  576. options.rejectKeys(/^id$/)
  577. ));
  578. }
  579. /**
  580. * Un-favorites the status specified in the ID parameter as the authenticating user.
  581. * Returns the un-favorited status when successful.
  582. *
  583. * @param {object} options
  584. * @param {String} options.id The ID of the status to un-favorite.
  585. * @param {String} options.username Screenname or email of user for authentication
  586. * @param {String} options.password Plain text password of user for authentication
  587. *
  588. * @return {status}
  589. * @throws TwitterApiException
  590. */
  591. function destroyFavorit(options) {
  592. return new Favorit( call(
  593. 'http://twitter.com/favorites/destroy/' + encodeURIComponent(options.id) + '.json',
  594. options.rejectKeys(/^id$/)
  595. ));
  596. }