Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove logs #36

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 64 additions & 6 deletions crawler.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,76 @@
/**
* Created by tushar on 13/09/17.
* Created by Sreedhar M B on 15/09/17.
*/

'use strict'

const request = require('request');
const cheerio = require('cheerio');
const fs = require('fs');

let siteURL = "http://localhost:8080";

let linksMap = {};
let linksVisitedMap = {};
let strLexElem;
let siteVisitCount = 0;

const getSiteDetails = (nextSiteHash, resolveFunc, callback) => {
let nextSite = siteURL + nextSiteHash;
siteVisitCount++;
request(nextSite, function(error, response, body) {
if(error) {
console.log("Error: " + error);
}

// console.log(nextSite);
// console.log("Status code: " + response.statusCode);
let $ = cheerio.load(body);

$('a.link').each(function( index ) {
let linkElement = $(this).attr('href');
if(!linksVisitedMap[linkElement]) {
linksMap[linkElement] = linkElement;
}
});

$('h1').each(function( index ) {
let strElem = $(this).text().trim();
strLexElem = strLexElem ? (strElem < strLexElem) ? strElem : strLexElem : strElem;
});


while(Object.keys(linksMap).length) {
let nextPageHash = linksMap[Object.keys(linksMap)[0]];
getSiteDetails(nextPageHash, resolveFunc, (strLexElem) => {
// console.log(strLexElem);
// console.log(siteVisitCount);
siteVisitCount--;
if(siteVisitCount == 1) {
resolveFunc(strLexElem);
}
});
linksVisitedMap[nextPageHash] = nextPageHash;
delete linksMap[nextPageHash];
}

callback(strLexElem);
});
};


/**
* Crawls a website using a start {url}, and returns the lexicographically smallest string.
* @param url
* @return {Promise.<string>}
*/
module.exports = url =>
new Promise((resolve, reject) => {
/**
* TODO: Write your high performance code here.
*/
reject(new Error('NotImplemented'))
})
siteURL = url;
getSiteDetails('', resolve, (strLexElem) => {
// console.log(strLexElem);
});


// reject(new Error('NotImplemented'))
});
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
"author": "",
"license": "ISC",
"dependencies": {
"cheerio": "^1.0.0-rc.2",
"express": "^4.15.4",
"express-rate-limit": "^2.9.0",
"mocha": "^3.5.3",
"nodemon": "^1.12.0",
"pug": "^2.0.0-rc.4"
"pug": "^2.0.0-rc.4",
"request": "^2.81.0"
}
}