google-img-scrap 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +134 -0
- package/package.json +40 -0
- package/src/constant/GOOGLE_CONSTANT.js +13 -0
- package/src/constant/extensions/IMAGES_EXTENSIONS.json +1 -0
- package/src/constant/query/GOOGLE_COLORS.js +14 -0
- package/src/constant/query/GOOGLE_PARAMS.js +9 -0
- package/src/constant/query/GOOGLE_QUERY.js +50 -0
- package/src/constant/translator/TRANSLATOR.js +3 -0
- package/src/google-img-scrap.js +104 -0
- package/src/utils/UTILS.js +24 -0
- package/test/test.js +23 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 yoannchb-pro
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
# Google-img-scrap v1.0.0
|
|
2
|
+
Scrap images from google image with lot of options
|
|
3
|
+
|
|
4
|
+
## Found a bug ?
|
|
5
|
+
|
|
6
|
+
- Tell it in my github issues dont be afraid :)
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```
|
|
11
|
+
npm i google-img-scrap
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Import
|
|
15
|
+
|
|
16
|
+
```js
|
|
17
|
+
const { GOOGLE_IMG_SCRAP , GOOGLE_QUERY } = require('../src/google-img-scrap');
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Result
|
|
21
|
+
|
|
22
|
+
```js
|
|
23
|
+
[
|
|
24
|
+
{
|
|
25
|
+
url: "...",
|
|
26
|
+
width: 1920,
|
|
27
|
+
height: 1080
|
|
28
|
+
},
|
|
29
|
+
...
|
|
30
|
+
]
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## How to use ?
|
|
34
|
+
|
|
35
|
+
- For the query parameter you need to set the name in upper case !
|
|
36
|
+
|
|
37
|
+
```js
|
|
38
|
+
const { GOOGLE_IMG_SCRAP , GOOGLE_QUERY } = require('../src/google-img-scrap');
|
|
39
|
+
|
|
40
|
+
(async function(){
|
|
41
|
+
const test = await GOOGLE_IMG_SCRAP({
|
|
42
|
+
search: "cats",
|
|
43
|
+
query: {
|
|
44
|
+
TYPE: GOOGLE_QUERY.TYPE.CLIPART,
|
|
45
|
+
DATE: GOOGLE_QUERY.DATE.YEAR,
|
|
46
|
+
COLOR: GOOGLE_QUERY.COLOR.BLACK_AND_WHITE,
|
|
47
|
+
SIZE: GOOGLE_QUERY.SIZE.LARGE,
|
|
48
|
+
LICENCE: GOOGLE_QUERY.LICENCE.COMMERCIAL_AND_OTHER,
|
|
49
|
+
EXTENSION: GOOGLE_QUERY.EXTENSION.JPG
|
|
50
|
+
},
|
|
51
|
+
domains: [],
|
|
52
|
+
custom: "name=content&name2=content2",
|
|
53
|
+
safeSearch: false,
|
|
54
|
+
excludeDomains: []
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
console.log(test, test.result.length);
|
|
58
|
+
})();
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
OR ALSO
|
|
62
|
+
|
|
63
|
+
```js
|
|
64
|
+
const { GOOGLE_IMG_SCRAP , GOOGLE_QUERY } = require('../src/google-img-scrap');
|
|
65
|
+
|
|
66
|
+
(async function(){
|
|
67
|
+
const test = await GOOGLE_IMG_SCRAP({
|
|
68
|
+
search: "cats",
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
console.log(test, test.result.length);
|
|
72
|
+
})();
|
|
73
|
+
```
|
|
74
|
+
## Params
|
|
75
|
+
|
|
76
|
+
- "search" what you want to search
|
|
77
|
+
- "domains" filter by domains
|
|
78
|
+
- "excludeDomains" exclude some domains
|
|
79
|
+
- "safeSearch" active safe search or not
|
|
80
|
+
- "custom" add extra query
|
|
81
|
+
- "query" set a query (can be [TYPE, DATE, COLOR, SIZE, LICENCE, EXTENSION])
|
|
82
|
+
|
|
83
|
+
## Google query
|
|
84
|
+
|
|
85
|
+
```js
|
|
86
|
+
{
|
|
87
|
+
SIZE: {
|
|
88
|
+
LARGE,
|
|
89
|
+
MEDIUM,
|
|
90
|
+
ICON
|
|
91
|
+
},
|
|
92
|
+
COLOR: {
|
|
93
|
+
BLACK_AND_WHITE,
|
|
94
|
+
TRANSPARENT,
|
|
95
|
+
RED,
|
|
96
|
+
BLUE,
|
|
97
|
+
PURPLE,
|
|
98
|
+
ORANGE,
|
|
99
|
+
YELLOW,
|
|
100
|
+
GREEN,
|
|
101
|
+
TEAL,
|
|
102
|
+
PINK,
|
|
103
|
+
WHITE,
|
|
104
|
+
GRAY,
|
|
105
|
+
BLACK,
|
|
106
|
+
BROWN
|
|
107
|
+
},
|
|
108
|
+
TYPE: {
|
|
109
|
+
CLIPART,
|
|
110
|
+
DRAW,
|
|
111
|
+
GIF
|
|
112
|
+
},
|
|
113
|
+
EXTENSION: {
|
|
114
|
+
JPG,
|
|
115
|
+
GIF,
|
|
116
|
+
BMP,
|
|
117
|
+
PNG,
|
|
118
|
+
SVG,
|
|
119
|
+
WEBP,
|
|
120
|
+
ICO,
|
|
121
|
+
RAW
|
|
122
|
+
},
|
|
123
|
+
DATE: {
|
|
124
|
+
DAY,
|
|
125
|
+
WEEK,
|
|
126
|
+
MONTH,
|
|
127
|
+
YEAR
|
|
128
|
+
},
|
|
129
|
+
LICENCE: {
|
|
130
|
+
CREATIVE_COMMONS,
|
|
131
|
+
COMMERCIAL_AND_OTHER
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "google-img-scrap",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Scrap images from google images with a lot of options",
|
|
5
|
+
"main": "./src/google-img-scrap.js",
|
|
6
|
+
"directories": {
|
|
7
|
+
"test": "test"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"test": "node ./test/test.js"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/yoannchb-pro/google-img-scrap.git"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"google",
|
|
18
|
+
"image",
|
|
19
|
+
"scrap",
|
|
20
|
+
"options",
|
|
21
|
+
"query",
|
|
22
|
+
"powerfull",
|
|
23
|
+
"easy",
|
|
24
|
+
"type",
|
|
25
|
+
"color",
|
|
26
|
+
"extension",
|
|
27
|
+
"filter",
|
|
28
|
+
"date",
|
|
29
|
+
"licence"
|
|
30
|
+
],
|
|
31
|
+
"author": "yoannchb",
|
|
32
|
+
"license": "MIT",
|
|
33
|
+
"bugs": {
|
|
34
|
+
"url": "https://github.com/yoannchb-pro/google-img-scrap/issues"
|
|
35
|
+
},
|
|
36
|
+
"homepage": "https://github.com/yoannchb-pro/google-img-scrap#readme",
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"got": "^11.0.0"
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const GOOGLE_CONSTANT = {
|
|
2
|
+
url: "https://images.google.com/search",
|
|
3
|
+
queryParam: "tbs",
|
|
4
|
+
forceGoogleImage: {
|
|
5
|
+
tbm: "isch" //needed to search on google image instead of google
|
|
6
|
+
},
|
|
7
|
+
headers: {
|
|
8
|
+
'User-Agent':
|
|
9
|
+
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
module.exports = { GOOGLE_CONSTANT };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
["jpg", "gif", "bmp", "png", "svg", "webp", "ico", "raw"]
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
const SIZE_PARAM = "isz",
|
|
2
|
+
COLOR_PARAM = "ic",
|
|
3
|
+
SPECIFIC_COLOR_PARAM = "Cisc",
|
|
4
|
+
TYPE_PARAM = "itp",
|
|
5
|
+
DATE_PARAM = "qdr",
|
|
6
|
+
LICENCE_PARAM = "il",
|
|
7
|
+
IMAGE_EXTENSION_PARAM = "ift";
|
|
8
|
+
|
|
9
|
+
module.exports = { SIZE_PARAM, COLOR_PARAM, SPECIFIC_COLOR_PARAM, TYPE_PARAM, DATE_PARAM, LICENCE_PARAM, IMAGE_EXTENSION_PARAM };
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
const { SIZE_PARAM, COLOR_PARAM, SPECIFIC_COLOR_PARAM, TYPE_PARAM, DATE_PARAM, LICENCE_PARAM, IMAGE_EXTENSION_PARAM } = require('./GOOGLE_PARAMS');
|
|
2
|
+
const { COLORS } = require('./GOOGLE_COLORS');
|
|
3
|
+
const EXTENSIONS = require("../extensions/IMAGES_EXTENSIONS.json");
|
|
4
|
+
|
|
5
|
+
const GOOGLE_QUERY = {
|
|
6
|
+
SIZE: {
|
|
7
|
+
LARGE: SIZE_PARAM + ":l",
|
|
8
|
+
MEDIUM: SIZE_PARAM + ":m",
|
|
9
|
+
ICON: SIZE_PARAM + ":i",
|
|
10
|
+
},
|
|
11
|
+
|
|
12
|
+
COLOR: {
|
|
13
|
+
BLACK_AND_WHITE: COLOR_PARAM + ":gray",
|
|
14
|
+
TRANSPARENT: COLOR_PARAM + ":trans",
|
|
15
|
+
},
|
|
16
|
+
|
|
17
|
+
TYPE: {
|
|
18
|
+
CLIPART: TYPE_PARAM + ":clipart",
|
|
19
|
+
DRAW: TYPE_PARAM + ":lineart",
|
|
20
|
+
GIF: TYPE_PARAM + ":animated",
|
|
21
|
+
},
|
|
22
|
+
|
|
23
|
+
EXTENSION: {},
|
|
24
|
+
|
|
25
|
+
DATE: {
|
|
26
|
+
DAY: DATE_PARAM + ":d",
|
|
27
|
+
WEEK: DATE_PARAM + ":w",
|
|
28
|
+
MONTH: DATE_PARAM + ":m",
|
|
29
|
+
YEAR: DATE_PARAM + ":y",
|
|
30
|
+
},
|
|
31
|
+
|
|
32
|
+
LICENCE: {
|
|
33
|
+
CREATIVE_COMMONS: LICENCE_PARAM + ":cl",
|
|
34
|
+
COMMERCIAL_AND_OTHER: LICENCE_PARAM + ":ol",
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
//build extension
|
|
39
|
+
EXTENSIONS.forEach((EXTENSION) => {
|
|
40
|
+
const queryName = EXTENSION.toUpperCase();
|
|
41
|
+
GOOGLE_QUERY.EXTENSION[queryName] = IMAGE_EXTENSION_PARAM + ":" + EXTENSION;
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
//build colors
|
|
45
|
+
COLORS.forEach((COLOR) => {
|
|
46
|
+
const queryName = COLOR.toUpperCase();
|
|
47
|
+
GOOGLE_QUERY.COLOR[queryName] = COLOR_PARAM + ":specific," + SPECIFIC_COLOR_PARAM + ":" + COLOR;
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
module.exports = { GOOGLE_QUERY };
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
const got = require('got');
|
|
2
|
+
const { FastHTMLParser } = require('fast-html-dom-parser');
|
|
3
|
+
|
|
4
|
+
const { GOOGLE_CONSTANT } = require('./constant/GOOGLE_CONSTANT');
|
|
5
|
+
const { GOOGLE_QUERY } = require('./constant/query/GOOGLE_QUERY');
|
|
6
|
+
const { TRANSLATOR } = require("./constant/translator/TRANSLATOR");
|
|
7
|
+
const EXTENSIONS = require('./constant/extensions/IMAGES_EXTENSIONS.json');
|
|
8
|
+
|
|
9
|
+
const { buildQuery, unicodeToChar } = require('./utils/UTILS');
|
|
10
|
+
|
|
11
|
+
//verify good configuration
|
|
12
|
+
function verify(config){
|
|
13
|
+
if(!config.search || config.search.trim() == "") throw "'search' can not be empty";
|
|
14
|
+
|
|
15
|
+
if(config.query){
|
|
16
|
+
const queryToVerify = Object.keys(GOOGLE_QUERY);
|
|
17
|
+
|
|
18
|
+
for(const key of Object.keys(config.query)){
|
|
19
|
+
if(!queryToVerify.includes(key)) throw `Invalide query name '${key}'`;
|
|
20
|
+
|
|
21
|
+
const VALUES = Object.values(GOOGLE_QUERY[key]);
|
|
22
|
+
const ACTUAL_VALUE = config.query[key];
|
|
23
|
+
if(!VALUES.includes(ACTUAL_VALUE)) throw `'${ACTUAL_VALUE}' is not a valide argument for the query : '${key}'`;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
//verify imag extension
|
|
29
|
+
function containImage(content = ""){
|
|
30
|
+
for(const EXTENSION of EXTENSIONS){
|
|
31
|
+
if(content.includes(EXTENSION)) return true;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
//parse HTML
|
|
38
|
+
async function parse(url){
|
|
39
|
+
const result = [];
|
|
40
|
+
|
|
41
|
+
const response = await got(url, {
|
|
42
|
+
headers: GOOGLE_CONSTANT.headers
|
|
43
|
+
});
|
|
44
|
+
const parser = new FastHTMLParser(response.body);
|
|
45
|
+
|
|
46
|
+
const scripts = parser.getElementsByTagName('script');
|
|
47
|
+
|
|
48
|
+
if(!scripts) return result;
|
|
49
|
+
|
|
50
|
+
for(const script of scripts){
|
|
51
|
+
const body = script.innerHTML;
|
|
52
|
+
|
|
53
|
+
const valide = containImage(body);
|
|
54
|
+
|
|
55
|
+
if(valide){
|
|
56
|
+
const regex = /\["(http.+?)",(\d+),(\d+)\]/gi;
|
|
57
|
+
|
|
58
|
+
let res = null;
|
|
59
|
+
|
|
60
|
+
while((res = regex.exec(body)) != null){
|
|
61
|
+
if(res.length >= 4 && res[1].match(/http/gi).length < 2) result.push({
|
|
62
|
+
url: unicodeToChar(res[1]),
|
|
63
|
+
height: res[2],
|
|
64
|
+
width: res[3]
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return result;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
//main
|
|
74
|
+
async function GOOGLE_IMG_SCRAP(config = {}){
|
|
75
|
+
//verify config
|
|
76
|
+
verify(config);
|
|
77
|
+
|
|
78
|
+
//exclude domains
|
|
79
|
+
const EXCLUDE_DOMAINS = [];
|
|
80
|
+
if(config.excludeDomains) config.excludeDomains.forEach((domain) => EXCLUDE_DOMAINS.push(`-site:${domain}`));
|
|
81
|
+
|
|
82
|
+
//domains
|
|
83
|
+
const DOMAINS = [];
|
|
84
|
+
if(config.domains) config.domains.forEach((domain) => DOMAINS.push(`site:"${domain}"`));
|
|
85
|
+
|
|
86
|
+
//building url
|
|
87
|
+
const QUERY = Object.assign(GOOGLE_CONSTANT.forceGoogleImage, {
|
|
88
|
+
[GOOGLE_CONSTANT.queryParam]: Object.values(config.query || {}).join(','),
|
|
89
|
+
q: encodeURIComponent(config.search + " " + EXCLUDE_DOMAINS.join(" ") + " " + DOMAINS.join(' OR ')),
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
const CUSTOM_PARAM = config.custom ? `&${config.custom}` : "";
|
|
93
|
+
const SAFE_SEARCH = config.safeSearch ? `&safe=active` : "";
|
|
94
|
+
|
|
95
|
+
const URL = GOOGLE_CONSTANT.url + buildQuery(QUERY, TRANSLATOR) + CUSTOM_PARAM + SAFE_SEARCH;
|
|
96
|
+
|
|
97
|
+
//parsing
|
|
98
|
+
const result = await parse(URL);
|
|
99
|
+
|
|
100
|
+
//result
|
|
101
|
+
return {url: URL, result: result};
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
module.exports = { GOOGLE_IMG_SCRAP , GOOGLE_QUERY };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
function buildQuery(query, translator){
|
|
2
|
+
let result = [];
|
|
3
|
+
|
|
4
|
+
let params = Object.keys(query);
|
|
5
|
+
let toTranslate = Object.keys(translator);
|
|
6
|
+
|
|
7
|
+
for(const param of params){
|
|
8
|
+
let queryName = param;
|
|
9
|
+
if(toTranslate.includes(param)) queryName = toTranslate[param];
|
|
10
|
+
|
|
11
|
+
result.push(`${queryName}=${query[param]}`);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return "?" + result.join('&');
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function unicodeToChar(text) {
|
|
18
|
+
return text.replace(/\\u[\dA-F]{4}/gi,
|
|
19
|
+
function (match) {
|
|
20
|
+
return String.fromCharCode(parseInt(match.replace(/\\u/g, ''), 16));
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
module.exports = { buildQuery, unicodeToChar };
|
package/test/test.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const { GOOGLE_IMG_SCRAP , GOOGLE_QUERY } = require('../src/google-img-scrap');
|
|
2
|
+
|
|
3
|
+
console.log(GOOGLE_QUERY);
|
|
4
|
+
|
|
5
|
+
(async function(){
|
|
6
|
+
const test = await GOOGLE_IMG_SCRAP({
|
|
7
|
+
search: "cats",
|
|
8
|
+
query: {
|
|
9
|
+
TYPE: GOOGLE_QUERY.TYPE.CLIPART,
|
|
10
|
+
DATE: GOOGLE_QUERY.DATE.YEAR,
|
|
11
|
+
COLOR: GOOGLE_QUERY.COLOR.BLACK_AND_WHITE,
|
|
12
|
+
SIZE: GOOGLE_QUERY.SIZE.LARGE,
|
|
13
|
+
LICENCE: GOOGLE_QUERY.LICENCE.COMMERCIAL_AND_OTHER,
|
|
14
|
+
EXTENSION: GOOGLE_QUERY.EXTENSION.JPG
|
|
15
|
+
},
|
|
16
|
+
domains: [],
|
|
17
|
+
custom: "name=content&name2=content2",
|
|
18
|
+
safeSearch: false,
|
|
19
|
+
excludeDomains: []
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
console.log(test, test.result.length);
|
|
23
|
+
})();
|