Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. #!/usr/local/bin/node
  2. // -*- mode: Javascript; -*-
  3.  
  4. var unirest = require( 'unirest' );
  5.  
  6. for (var url of get_urls( 'http://www.example.com/generated-page?n=1' ))
  7. console.log( 'Get url', url );
  8.  
  9. for (var page of get_pages( 'http://www.example.com/generated-page?n=1' ))
  10. console.log( 'Got page', page );
  11.  
  12. function* get_urls( url ) {
  13. do {
  14. yield url;
  15. var rx = url.match( /^(.*?n=)([0-9]+)$/ );
  16. if (rx) {
  17. if (rx[2] >= 3) break;
  18. url = rx[1] + (parseInt(rx[2]) + 1).toString( );
  19. }
  20. } while (rx);
  21. }
  22.  
  23. function* get_pages( url ) {
  24. do {
  25. // *** This is what I want to do, but it's not the right way to do it! ***
  26. // unirest.get( url ).end( function (rsp) { yield rsp; } );
  27. var rx = url.match( /^(.*?n=)([0-9]+)$/ );
  28. if (rx) {
  29. if (rx[2] >= 3) break;
  30. url = rx[1] + (parseInt(rx[2]) + 1).toString( );
  31. }
  32. } while (rx);
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement