Guest User

Untitled

a guest
Jul 18th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.72 KB | None | 0 0
  1. #!/usr/bin/env node
  2.  
  3. /**
  4. * fanfou.js
  5. * A node.js Fanfou client
  6. * © 2010 softboysxp.com
  7. */
  8.  
  9. /**
  10. * Copyright 2010 softboysxp.com. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without modification, are
  13. * permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice, this list of
  16. * conditions and the following disclaimer.
  17. *
  18. * 2. Redistributions in binary form must reproduce the above copyright notice, this list
  19. * of conditions and the following disclaimer in the documentation and/or other materials
  20. * provided with the distribution.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND ANY EXPRESS OR IMPLIED
  23. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  24. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
  25. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  28. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  30. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32.  
  33. var http = require('http');
  34. var fs = require('fs');
  35.  
  36. var credentials = {
  37. encodedCredentials: '',
  38.  
  39. encodeCredentials: function(username, password) {
  40. this.encodedCredentials = 'Basic ' + (new Buffer(username + ':' + password)).toString('base64');
  41. return this.encodedCredentials;
  42. },
  43.  
  44. loadCredentials: function() {
  45. try {
  46. this.encodedCredentials = fs.readFileSync(process.env['HOME'] + '/.fanfou_auth', 'utf-8');
  47. } catch (exception) {
  48. return null;
  49. }
  50.  
  51. return this.encodedCredentials;
  52. },
  53.  
  54. saveCredentials: function(username, password) {
  55. fs.writeFileSync(process.env['HOME'] + '/.fanfou_auth', this.encodeCredentials(username, password), 'utf-8');
  56. }
  57. }
  58.  
  59. var fanfouClient = {
  60. FANFOU_API_PROTOCOL: 'http',
  61. FANFOU_API_HOST: 'api.fanfou.com',
  62. FANFOU_API_PUBLIC_TIMELINE: '/statuses/public_timeline.json',
  63. FANFOU_API_USER_TIMELINE: '/statuses/user_timeline.json',
  64. FANFOU_API_FRIENDS_TIMELINE: '/statuses/friends_timeline.json',
  65. FANFOU_API_REPLIES: '/statuses/replies.json',
  66. FANFOU_API_UPDATE: '/statuses/update.json',
  67.  
  68. client: null,
  69.  
  70. callAPI: function(method, api, params, callback) {
  71. if (!this.client) {
  72. this.client = http.createClient(80, this.FANFOU_API_HOST, this.FANFOU_API_PROTOCOL == 'https')
  73. }
  74.  
  75. var request = this.client.request(method, api, {
  76. 'Host': this.FANFOU_API_HOST,
  77. 'Authorization': credentials.encodedCredentials,
  78. 'Content-Type': 'application/x-www-form-urlencoded',
  79. 'Content-Length': params ? params.length : 0
  80. });
  81.  
  82. request.end(params, 'utf-8');
  83.  
  84. request.on('response', function(response) {
  85. var body = '';
  86.  
  87. response.on('data', function(chunk) {
  88. body += chunk;
  89. });
  90.  
  91. response.on('end', function() {
  92. if (response.statusCode != 200) {
  93. var obj = eval('(' + body + ')');
  94.  
  95. if (obj && obj.error) {
  96. console.log("ERROR: " + obj.error);
  97. } else {
  98. console.log("ERROR: " + body);
  99. }
  100.  
  101. process.exit(-1);
  102. }
  103.  
  104. if (body) {
  105. callback(eval('(' + body + ')'));
  106. }
  107. });
  108. });
  109. },
  110.  
  111. getUserTimeline: function(callback) {
  112. this.callAPI('GET', this.FANFOU_API_USER_TIMELINE, null, callback);
  113. },
  114.  
  115. getFriendsTimeline: function(callback) {
  116. this.callAPI('GET', this.FANFOU_API_FRIENDS_TIMELINE, null, callback);
  117. },
  118.  
  119. getPublicTimeline: function(callback) {
  120. this.callAPI('GET', this.FANFOU_API_PUBLIC_TIMELINE, null, callback);
  121. },
  122.  
  123. getReplies: function(callback) {
  124. this.callAPI('GET', this.FANFOU_API_REPLIES, null, callback);
  125. },
  126.  
  127. postStatus: function(status, callback) {
  128. this.callAPI('POST', this.FANFOU_API_UPDATE, 'status=' + encodeURIComponent(status), callback);
  129. }
  130. }
  131.  
  132. var fanfoujs = {
  133. list: function() {
  134. var displayStatus = function(statuses) {
  135. statuses.forEach(function(status) {
  136. console.log(status.user.screen_name + ': ' + status.text);
  137. });
  138. };
  139.  
  140. var type = process.argv[3] ? process.argv[3] : "friends";
  141.  
  142. switch(type) {
  143. case 'friends':
  144. fanfouClient.getFriendsTimeline(displayStatus);
  145. break;
  146.  
  147. case 'public':
  148. fanfouClient.getPublicTimeline(displayStatus);
  149. break;
  150.  
  151. case 'self':
  152. fanfouClient.getUserTimeline(displayStatus);
  153. break;
  154.  
  155. case 'replies':
  156. fanfouClient.getReplies(displayStatus);
  157. break;
  158.  
  159. default:
  160. this.usage();
  161. }
  162. },
  163.  
  164. post: function() {
  165. if (process.argv.length != 4) {
  166. this.usage();
  167. }
  168.  
  169. var status = process.argv[3];
  170.  
  171. fanfouClient.postStatus(status, function(status) {
  172. console.log(status.user.screen_name + ': ' + status.text);
  173. });
  174. },
  175.  
  176. usage: function() {
  177. console.log('Usage: "fanfou.js setup <username> <password>"');
  178. console.log('Usage: "fanfou.js list [friends(Default)/public/self/replies]"');
  179. console.log('Usage: "fanfou.js post <status>"');
  180.  
  181. process.exit(0);
  182. },
  183.  
  184. setup: function() {
  185. if (process.argv.length != 5) {
  186. this.usage();
  187. }
  188.  
  189. credentials.saveCredentials(process.argv[3], process.argv[4]);
  190. },
  191.  
  192. main: function() {
  193. if (process.argv.length < 3) {
  194. this.usage();
  195. }
  196.  
  197. var command = process.argv[2];
  198.  
  199. switch (command) {
  200. case 'setup':
  201. this.setup();
  202. break;
  203.  
  204. case 'list':
  205. case 'post':
  206. if (!credentials.loadCredentials()) {
  207. console.log('Please run "node fanfou.js setup" first');
  208. process.exit(-1);
  209. }
  210. eval('this.' + command + '();');
  211. break;
  212.  
  213. default:
  214. this.usage();
  215. }
  216. }
  217. }
  218.  
  219. fanfoujs.main();
Add Comment
Please, Sign In to add comment