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

Solution by KarthikMAM - 26.5s #40

Open
wants to merge 10 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
73 changes: 56 additions & 17 deletions crawler.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,57 @@
/**
* Created by tushar on 13/09/17.
*/

'use strict'

/**
* 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'))
const { resetLimits, throttle } = require('./throttle')
const { get } = require('axios')

const parsePage = (page) => {
page = page.slice(264, -96)

let code
let urls = []

while (page.slice(-5) === '</h1>') {
let currentCode = page.slice(-11, -5)

code = code < currentCode ? code : currentCode

page = page.slice(0, -15)
}

while (page[1] !== '/') {
urls.push(page.slice(39, 72))

page = page.slice(88)
}

return { urls, code }
}

module.exports = startUrl => new Promise((resolve) => {
const domain = startUrl
const parsedPages = {}

let result
// let count = 0
resetLimits(true)

const processUrl = (url) => new Promise((resolve, reject) => {
if (parsedPages[url]) { resolve(); return }

parsedPages[url] = true

throttle(() => get(`${domain}${url}`).then(({ data }) => {
const { urls, code } = parsePage(data)

result = code ? (result < code ? result : code) : result

// console.log(result, url, code, count++)

Promise
.all(urls.filter(url => !parsedPages[url]).map(processUrl))
.then(i => resolve())
}).catch(i => {
parsedPages[url] = false
processUrl(url, false).then(i => resolve())
}))
})

processUrl('').then(i => resolve(result))
})
Loading