const phantom = require('x-ray-phantom');
const Xray = require('x-ray');
const fs = require('fs');
const request = require('request');
const async = require('async');

var phantom_opts = {
    webSecurity: false,
    images: false,
    weak: false
};

const scrollNumber = 4000;

console.log("Starting scraper");

var x = Xray().driver(phantom(phantom_opts, function (nightmare, done) {
    done
        .useragent("Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36")
        .goto(nightmare.request.req.url)
        .scrollTo(scrollNumber * 1, 0)
        .wait()
        .scrollTo(scrollNumber * 2, 0)
        .wait()
        .scrollTo(scrollNumber * 3, 0)
        .wait()
        .scrollTo(scrollNumber * 4, 0)
        .wait()
        .scrollTo(scrollNumber * 5, 0)
        .wait()
        .scrollTo(scrollNumber * 6, 0)
        .wait()
        .scrollTo(scrollNumber * 7, 0)
        .wait()
        .scrollTo(scrollNumber * 8, 0)
        .wait()
        .scrollTo(scrollNumber * 9, 0)
        .wait()
        .scrollTo(scrollNumber * 10, 0)
        .wait()
        .scrollTo(scrollNumber * 11, 0)
        .wait()
        .scrollTo(scrollNumber * 12, 0)
        .wait()
        .scrollTo(scrollNumber * 13, 0)
        .wait()
        .scrollTo(scrollNumber * 14, 0)
        .wait()
        .scrollTo(scrollNumber * 15, 0)
        .wait()
        .scrollTo(scrollNumber * 16, 0)
        .wait()
        .scrollTo(scrollNumber * 17, 0)
        .wait()
        .scrollTo(scrollNumber * 18, 0)
        .wait()
        .scrollTo(scrollNumber * 19, 0)
        .wait()
        .scrollTo(scrollNumber * 20, 0)
}));

x('https://soundcloud.com/aethernote/likes', {
  artists : ['.soundTitle__usernameText'],
  trackTitles : ['.soundTitle__title span'], // follow link to google images
  trackArt : ['.soundList .customImage span@style'],
  likes: ['.sc-button-like'],
  reposts: ['.sc-button-repost'],
  plays: ['.sc-ministats-plays .sc-visuallyhidden'],
  comments: ['.sc-ministats-comments .sc-visuallyhidden'],
  url: ['.soundList .soundTitle__title@href'], 
  genreTag: ['.soundTitle__tagContent']
})(function(err, obj) {

	// For each track art – update the object and save the image
	var updatedAlbumURLs = [];

	// The data we got from xray
	var trackData = obj;
	
	var i = 0; // Define a counter
	var length = trackData.trackArt.length; // Define the length

	console.log("Getting " + length + " entries");

	var fn = function() {

        console.log("Requesting");

		// console.log(trackData.artists[i], trackData.trackTitles[i], trackData.trackArt[i]);

		// Regex
		var regExp = /\(([^)]+)\)/;

		// console.log((regExp.exec(txt)[1]).replace("200x200", "500x500"));

		// Save the image file now that we have the URL
		request.get({url: (regExp.exec(trackData.trackArt[i])[1]).replace("200x200", "500x500"), encoding: 'binary'}, function (err, response, body) {
		  fs.writeFile(("images/trackArt" + [i] + ".jpg"), body, 'binary', function(err) {
		    if(err) {
		    	console.log(err);
		    }
		    else {
		      	if (i < length) {
		      		console.log("The image file " + i + " was saved!");

		      		console.log("URL " + i + " was updated!");

		      		// Push the new regexed URL and get the large size
		      		updatedAlbumURLs.push((regExp.exec(trackData.trackArt[i])[1]).replace("200x200", "500x500"));
		      		// console.log(updatedAlbumURLs);

		      		fn();
		      		i++;
		      	}
		      	else {
		      		console.log("Done saving our images!");

		      		console.log("Writing our JSON");
		      		trackData.trackArt = updatedAlbumURLs;

		      		// For some reason right now we have a duplicate
		      		// trackData.trackArt.splice(0, 1);

		      		fs.writeFile("data.json", JSON.stringify(trackData), function(err) {
					    if(err) {
					        return console.log(err);
					    }
					    else {
					    	console.log("JSON saved!");
					    }
					}); 
		      		
		      	}
		    }
		  });
		});
	}

	fn();
})