var bhttp = require('bhttp');
var Promise = require('bluebird');
var cheerio = require('cheerio');
var fs = require('fs');
var util = require('util');
var i = 3587;
var poemArray = [];
var timer = setInterval(function() {
	i++;
	var URL = "http://www.ljod.is/index.php/ljod/view_poem/" + i;
	Promise.try(function() {
		return bhttp.get(URL);
	}).then(function (shitResponse){
		var response = shitResponse.body.toString();
		if (response != "Ekkert ljóð fannst") {
			var $ = cheerio.load(response, {decodeEntities: false});
			var title =	$(".boxtext").html();
			var poem = $(".poem").html();
			poem = poem.replace(/<br>/g,'').trim();
			poem = poem.replace(/<i>/g,'').trim();
			poem = poem.replace(/<\/i>/g,'').trim();
			poem = poem.replace(/<b>/g,'').trim();
			poem = poem.replace(/<\/b>/g,'').trim();
			var poemObject = {
				"title" : "[ " + title + " ]\n",
				"poem" : "[ " + poem + " ]\n\n\n\n"
			}
			poemArray.push(poemObject);
			fs.appendFile('./titles.txt', poemObject.title, function (err) {
			});
			fs.appendFile('./poems.txt', poemObject.poem, function (err) {
			});
			console.log("==============================================");
			console.log("Number of retreived poems:", poemArray.length);
			console.log("Number of failed attempts:", i - poemArray.length);
		}
		else{
			console.log("==============================================");
			console.log("Number of retreived poems:", poemArray.length);
			console.log("Number of failed attempts:", i - poemArray.length);
		}
	});
}, 1000);
