0

I am new to working with CasperJS and JavaScript in general. At the moment, I am trying to learn how to download a file from a git repo. While reading the reference page, I stumbled upon the download method that CasperJS has. As a result, I have tried the following:

var casper = require('casper').create();
var url = null;
var utils = require('utils');
var http = require('http');
var fs = require('fs');

// A test to make sure that we can go into a github without any authentication
casper.start('https://github.com/gabegomez014/Test2', function(){
    this.echo(this.getTitle(), 'INFO');
});

// A test for how the download function works
casper.then(function(){
    url = 'https://github.com/gabegomez014/Test2/blob/master/.gitignore';
    var cwd = fs.absolute('.');
    var parent = fs.absolute("../");
    var path = cwd + parent;
    this.download(url, path);
});

// A test in order to get the current HTTP status of links that have been put into the function
// Attempt 3 works and cleaner
casper.thenOpen('https://github.com/gabegomez014/Test2/blob/master/.gitignore', function(){
    var res = this.status(false);
    this.echo(res.currentHTTPStatus);
});

The problem with this is that instead of downloading just the file to my computer at the specified path, it instead copies a partial directory path that is identical to the absolute path that was created (without their content as well). I can not tell if something I did was wrong (I can only assume so), or what, but can someone please help me?

P.S. : Would it just be easier to download these files in another way? Cause if it is necessary, it will be done. Thanks in advance for the time and help.

Gabe Gomez
  • 81
  • 2
  • 11
  • Also, after using the waitForUrl method, it said that there was a wait timeout of 5000ms so it exited. I can only assume this is related to the problem. Still looking around though. – Gabe Gomez May 24 '17 at 19:05
  • ^ Tried to increase the wait timeout to 20 seconds and still does not work. So I assume that is not the problem. – Gabe Gomez May 24 '17 at 19:29

1 Answers1

0

Alright, after taking a long break and coming back to it, I realized it was an easy mistake that I made. Firstly, I concatenated two absolute paths together, which is why it was copying the directories 2 times over.

In terms of the download of the file, I have learned that I do not need to specify a path for the method within CasperJS and that it will put it in the current working directory. Be careful when adding hidden files though, because as you can tell, they're hidden. For example, I downloaded a .gitignore, and since it was hidden, I could not see it until I had done "ls -a" within the terminal for the directory that I was doing this work in.

I would also like to say that even though I specify a certain extension like .java for a file or .txt, this method will only download the HTML, so if you want the actual file associated within the git repo, you'll have to do something else that I am still currently looking for. Here's the code with the fix:

var casper = require('casper').create();
var url = null;
var utils = require('utils');
var http = require('http');
var fs = require('fs');
casper.options.waitTimeout = 20000;

// A test to make sure that we can go into a github without any authentication
casper.start('https://github.com/gabegomez014/Test2', function(){
    this.echo(this.getTitle(), 'INFO');
});

// A test for how the download function works
casper.thenOpen('https://github.com/gabegomez014/SSS', function() {
    url = 'https://github.com/gabegomez014/SSS/tree/master/src/SolarSystem/'
    this.download(url, 'Algorithms.java');
});

// A test in order to get the current HTTP status of links that have been put into the function
// Attempt 3 works and cleaner
casper.thenOpen('https://github.com/gabegomez014/Test2/blob/master/.gitignore', function(){
    var res = this.status(false);
    this.echo(res.currentHTTPStatus);
});

// To let me know that all of the functions have finished and all done
casper.then(function(){
    console.log('complete');
});

casper.run();
Gabe Gomez
  • 81
  • 2
  • 11