Commit 9b76e009 authored by Mehmet Kozan's avatar Mehmet Kozan

all functionality completed.

parent 1ea5af9f
# contribution guide
\ No newline at end of file
Developed using `Visual Studio Code` Version 1.8.1
\ No newline at end of file
# license
\ No newline at end of file
MIT License
Copyright (c) 2017 Mehmet Kozan
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
\ No newline at end of file
# crawler-url-parser
> **An `URL` parser for crawling purpose, has more than 200 unit test cases.**
![avatar of crawler-url-parser](https://assets.gitlab-static.net/uploads/-/system/project/avatar/4809017/crawler-url-parser.png)
[![NPM Package](https://img.shields.io/npm/v/crawler-url-parser.svg?style=flat)](https://www.npmjs.org/package/crawler-url-parser)
[![Downloads Total](https://img.shields.io/npm/dt/crawler-url-parser.svg?style=flat)](https://www.npmjs.org/package/crawler-url-parser)
## crawler-url-parser
**An `URL` parser for crawling purpose.**
## Installation
`npm install crawler-url-parser`
......@@ -46,10 +44,8 @@ console.log(result.length);
```
## Test
* check test folder extra usage.
`mocha`
or
`npm test`
`mocha` or `npm test`
* check test folder and quickstart.js for extra usage.
## API
......@@ -53,18 +53,6 @@ function parse(currentUrlStr,baseUrlStr)
return ret;
}
function isrelative(){
}
function ischild(base,current){
}
function isparent(base,current){
}
function extract(data,sourceUrl){
let urlMap = new Map();
var baseUrl = parse(sourceUrl);
......@@ -117,15 +105,69 @@ function extract(data,sourceUrl){
return Array.from(urlMap.values());
}
function getlevel(current,base){
//samelevel,sublevel,uplevel
//if baseurl "sub.domain.com/aaa/bbb/ccc"
// "sub.domain.com/aaa/bbb/" - uplevel
// "sub.domain.com/aaa/ddd/" - samelevel
// "sub.domain.com/aaa/bbb/ccc/ddd" - sublevel
//else
// null
let ret = null ;
let normlizedCurrent = normalizeUrl(current);
let normlizedBase = normalizeUrl(base);
let parsedCurrentUrl = url.parse(normlizedCurrent);
let parsedBaseUrl = url.parse(normlizedBase);
if(parsedCurrentUrl.host==parsedBaseUrl.host){
let cPath = parsedCurrentUrl.pathname;
let bPath = parsedBaseUrl.pathname;
let cCount = cPath.split("/").length -1;
let bCount = bPath.split("/").length -1;
if(cCount!=bCount){
if(cPath.includes(bPath)){
ret = "uplevel";
}
else if(bPath.includes(cPath)){
ret = "sublevel"
}
}
else if(cCount == bCount){
cPath = cPath.replace(/(\/[^\/]*)[\/]?$/,"");
bPath = bPath.replace(/(\/[^\/]*)[\/]?$/,"");
if(cPath == bPath){
return "samelevel";
}
}
}
return ret;
}
function querycount(current){
let ret = 0 ;
let normlizedCurrent = normalizeUrl(current);
let parsedCurrentUrl = url.parse(normlizedCurrent);
if(parsedCurrentUrl.search!=null){
ret = parsedCurrentUrl.search.split("=").length -1;
}
return ret;
}
module.exports.parse = parse;
module.exports.isrelative = isrelative;
module.exports.ischild = ischild;
module.exports.isparent = isparent;
module.exports.extract = extract;
module.exports.getlevel = getlevel;
module.exports.querycount = querycount;
//for testing purpose
if (!module.parent){
console.log("for testing purpose");
//getlevel("www.domain.com/aaa/bbb/","www.domain.com/aaa/bbb/ccc");
//let res1 = getlevel("sub.domain.com/aaa/bbb/","sub.domain.com/aaa/bbb/ccc");
//let res2 = getlevel("sub.domain.com/aaa/bbb/ccc/ddd","sub.domain.com/aaa/bbb/ccc");
//let res3 = getlevel("sub.domain.com/aaa/bbb/eee","sub.domain.com/aaa/bbb/ccc");
//debugger;
process.exit();
}
\ No newline at end of file
{
"name": "crawler-url-parser",
"version": "1.2.2",
"version": "1.2.3",
"description": "An `URL` parser for crawling purpose.",
"main": "crawler-url-parser.js",
"keywords": [
......@@ -45,18 +45,17 @@
"test": "mocha --recursive",
"start": "node crawler-url-parser.js"
},
"homepage": "https://gitlab.com/mehmet.kozan/crawler-url-parser",
"bugs": {
"url": "https://gitlab.com/mehmet.kozan/crawler-url-parser/issues",
"email": "[email protected]"
},
"repository": {
"type": "git",
"url": "https://gitlab.com/mehmet.kozan/crawler-url-parser.git"
"url": "git://gitlab.com/mehmet.kozan/crawler-url-parser.git"
},
"author": "Mehmet Kozan",
"author": "Mehmet Kozan <[email protected]>",
"license": "MIT",
"engines": {
"node": "8.2.1"
"node": ">=0.6"
}
}
......@@ -3,12 +3,11 @@ const fs = require('fs');
const path = require('path');
const cup = require("../");
describe('extract url internal, subdomain, external', function() {
describe('extract url as internal, subdomain, external', function() {
it('should extract urls at extract-test-html-01.html file', function() {
let htmlPath = path.resolve(__dirname,'extract-test-html-01.html');
let htmlPath = path.resolve(__dirname,'02_extract-test-html-01.html');
let htmlString = fs.readFileSync(htmlPath,'utf-8');
let result = cup.extract(htmlString,"http://www.stackoverflow.com/aaa/bbb/ccc");
assert.equal(result.length,9);
debugger;
});
});
const assert = require('assert');
const cup = require("../");
describe('getlevel url as samelevel, sublevel, uplevel', function() {
it('should getlevel sublevel urls', function() {
let res = cup.getlevel("sub.domain.com/aaa/bbb/","sub.domain.com/aaa/bbb/ccc");
assert.equal(res,"sublevel");
});
it('should getlevel uplevel urls', function() {
let res = cup.getlevel("sub.domain.com/aaa/bbb/ccc/ddd","sub.domain.com/aaa/bbb/ccc");
assert.equal(res,"uplevel");
});
it('should getlevel samelevel urls', function() {
let res = cup.getlevel("sub.domain.com/aaa/bbb/eee","sub.domain.com/aaa/bbb/ccc");
assert.equal(res,"samelevel");;
});
it('should handle unvalid urls', function() {
let res = cup.getlevel("sub.domain.com/aaa/bbb/eee","sub.anotherdomain.com/aaa/bbb/ccc");
assert.equal(res,null);
});
});
const assert = require('assert');
const cup = require("../");
describe('querycount url', function() {
it('should calculate urls query-0', function() {
let res = cup.querycount("sub.domain.com/aaa/bbb");
assert.equal(res,0);
});
it('should calculate urls query-1', function() {
let res = cup.querycount("sub.domain.com/aaa/bbb?q1=data1");
assert.equal(res,1);
});
it('should calculate urls query-2', function() {
let res = cup.querycount("sub.domain.com/aaa/bbb?q1=data1&q2=data2");
assert.equal(res,2);
});
it('should calculate urls query-3', function() {
let res = cup.querycount("sub.domain.com/aaa/bbb?q1=data1&q2=data2&q3=data3");
assert.equal(res,3);
});
it('should calculate urls query-4', function() {
let res = cup.querycount("sub.domain.com/aaa/bbb?q1=data1&q2=data2&q3=data3&q4=data4");
assert.equal(res,4);
});
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment