From c443b9664f221e2b2567627712f96070a19b33e9 Mon Sep 17 00:00:00 2001 From: IliesT <47112446+ilies-t@users.noreply.github.com> Date: Wed, 22 Jul 2020 19:40:22 +0200 Subject: [PATCH] v0.1.0 --- .gitignore | 3 + LICENSE | 2 +- README.md | 145 +++++++++--------- changelog.md | 32 ++++ examples/project.js | 31 ---- examples/user.js | 31 ---- index.js | 13 +- lib/Behance.js | 331 +++++++++++++++++++++-------------------- lib/Behance.js.map | 1 + lib/Behance.ts | 210 ++++++++++++++++++++++++++ lib/MainMethods.js | 92 ++++++++++++ lib/MainMethods.js.map | 1 + lib/MainMethods.ts | 83 +++++++++++ lib/_functions.js | 67 --------- lib/interfaces.js | 10 ++ lib/interfaces.js.map | 1 + lib/interfaces.ts | 31 ++++ package-lock.json | 43 +++--- package.json | 7 +- test/test.js | 100 ++++++++++++- tsconfig.json | 17 +++ 21 files changed, 852 insertions(+), 399 deletions(-) create mode 100644 changelog.md delete mode 100644 examples/project.js delete mode 100644 examples/user.js create mode 100644 lib/Behance.js.map create mode 100644 lib/Behance.ts create mode 100644 lib/MainMethods.js create mode 100644 lib/MainMethods.js.map create mode 100644 lib/MainMethods.ts delete mode 100644 lib/_functions.js create mode 100644 lib/interfaces.js create mode 100644 lib/interfaces.js.map create mode 100644 lib/interfaces.ts create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore index 4d29575..98066ec 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,6 @@ npm-debug.log* yarn-debug.log* yarn-error.log* + +# IDE +.idea \ No newline at end of file diff --git a/LICENSE b/LICENSE index 3e09f05..c15e919 100644 --- a/LICENSE +++ b/LICENSE @@ -186,7 +186,7 @@ Apache License same "printed page" as the copyright notice for easier identification within third-party archives. - Copyright 2019 Ilies T + Copyright 2020 Ilies T Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/README.md b/README.md index 1dfb584..c3526d1 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,9 @@ # Behance-node +[![NPM Version](https://img.shields.io/npm/v/behance-node.svg)](https://npmjs.org/package/behance-node) +[![Strict TypeScript Checked](https://badgen.net/badge/Built%20With/TypeScript)](https://www.typescriptlang.org) +[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) -An asynchronous library to scrape Behance. +An asynchronous module for scrapping Behance using JavaScript. ## Install ```shell @@ -13,90 +16,80 @@ const Behance = require('behance-node'); ``` # Documentation -* [User informations](#user-informations) -* [User detailed informations](#user-detailed-informations) -* [Save user detailed informations](#save-user-detailed-informations) -* [Project informations](#project-informations) -* [Save project informations](#save-project-informations) +* [User data](#user-data) +* [Project data](#project-data) * [Random project](#random-project) -## User informations -Return result about user -```js -Behance.user(`nicholaslosacco`) - .then((result) => { - console.log(result) - }) - .catch((error) => { - console.log(error) - }); -``` - -## User detailed informations -Return detailed result about user -```js -Behance.detailledUser(`nicholaslosacco`) - .then((detailledResult) => { - console.log(detailledResult) - }) - .catch((error) => { - console.log(error) - }); -``` +## User data +Return data about user. +* Method: + * `user(username: string, detailed?: boolean)` +* Parameters: -## Save user detailed informations -Save user detailed result in JSON file -```js -let user = `nicholaslosacco`; -let directory = `datas-folder`; + | Name | Type | Info | + | ---------- | ------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------ | + | `username` | string | Desired username | + | `detailed` | boolean | Return more detail about user or not | -Behance.saveDetailledUser(user, directory) - .then(() => { - console.log(`${user}.json saved in ${directory}`) - }) - .catch((error) => { - console.log(error) - }); -``` +* Example: + * Get detailed data about this user `https://www.behance.net/moesalah`. + ```js + Behance.user('moesalah', true) + .then( result => console.log(result) ) + .catch( error => console.log(error) ); + ``` -## Project informations -Return detailed result about project -```js -Behance.project(`75651921/Cirka-Free-Typeface`) - .then((result) => { - console.log(result); - }) - .catch((error) => { - console.log(error) - }) -``` +## Project data +Return data about project. +* Method: + * `project(path: string)` +* Parameter: -## Save project informations -Save project result in JSON file -```js -let project = `75651921/Cirka-Free-Typeface`; -let directory = `datas-folder`; + | Name | Type | Info | + | ---------- | ------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------ | + | `path` | string | Desired project path, must contain project id and name | -Behance.saveProject(project, directory) - .then(() => { - console.log(`šŸ‘Œ saved`); - }) - .catch((error) => { - console.log(error) - }) -``` +* Example: + * Get data about this project `https://www.behance.net/gallery/77156181/Amazon-Rebranding-UI-Concept`. + ```js + Behance.project('77156181/Amazon-Rebranding-UI-Concept') + .then( result => console.log(result) ) + .catch( error => console.log(error) ); + ``` ## Random project -Return random project from trending -```js -Behance.randomProject() - .then((result) => { - console.log(result) - }) - .catch((error) => { - console.log(error) - }) -``` +Get a random project data using sort. +* Method: + * ` + randomProject( + sort?: 'recommended' | 'curated' | 'most_appreciated' | 'most_viewed' | 'most_commented' | 'most_recent', + time?: 'today' | 'this_week' | 'this_month' | 'all_time' + ) + ` +* Parameters: + + | Name | Type | Info | + | ---------- | ------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------ | + | `sort` | `recommended`, `curated`, `most_appreciated`, `most_viewed`, `most_commented`, `most_recent` | Only `most_appreciated`, `most_viewed`, `most_commented` can be used with `time` parameter | + | `time` | `today`, `this_week`, `this_month`, `all_time` | Sort result by date | + +* Examples: + * Get a random project from most viewed this week. + ```js + Behance.randomProject('most_viewed', 'this_week') + .then( result => console.log(result) ) + .catch( error => console.log(error) ); + ``` + * Get a random curated project. + ```js + Behance.randomProject('curated') + .then( result => console.log(result) ) + .catch( error => console.log(error) ); + ``` + +# Links +* [NPM Package](https://www.npmjs.com/package/behance-node) +* [Moe Salah - artist from documentation examples](https://www.behance.net/moesalah) # License [Apache License 2.0](https://choosealicense.com/licenses/apache-2.0/) \ No newline at end of file diff --git a/changelog.md b/changelog.md new file mode 100644 index 0000000..479b2f9 --- /dev/null +++ b/changelog.md @@ -0,0 +1,32 @@ +`Date format: YYYY-DD-MM`
+`Language: English` + + +0.0.1 / 2019-22-09 +================== +* First release. + + +0.0.2 / 2019-22-09 +================== +* Update NPM publication. + + +0.0.3 / 2019-08-10 +================== +* Update comments and NPM publication. + + +0.1.0 / 2020-22-07 +================== +* Move to typescript <3 . + +* Improve the module and main methods code readability/safety by using TypeScript class. + +* Delete all file-related functions. + +* Better tests. + +* Add filters for `randomProject()`. + +* Change documentation skeleton. \ No newline at end of file diff --git a/examples/project.js b/examples/project.js deleted file mode 100644 index 7577613..0000000 --- a/examples/project.js +++ /dev/null @@ -1,31 +0,0 @@ -const Behance = require('../index'); - -// get project -Behance.project(`75651921/Cirka-Free-Typeface`) - .then((result) => { - console.log(result); - }) - .catch((error) => { - console.log(error) - }) - -// save project -let project = `75651921/Cirka-Free-Typeface`; -let directory = `datas-folder`; - -Behance.saveProject(project, directory) - .then(() => { - console.log(`šŸ‘Œ saved`); - }) - .catch((error) => { - console.log(error) - }) - -// random project -Behance.randomProject() - .then((result) => { - console.log(result) - }) - .catch((error) => { - console.log(error) - }) \ No newline at end of file diff --git a/examples/user.js b/examples/user.js deleted file mode 100644 index e971540..0000000 --- a/examples/user.js +++ /dev/null @@ -1,31 +0,0 @@ -const Behance = require('../index'); - -// get user -Behance.user(`nicholaslosacco`) - .then((result) => { - console.log(result) - }) - .catch((error) => { - console.log(error) - }); - -// get detailled user -Behance.detailledUser(`nicholaslosacco`) - .then((detailledResult) => { - console.log(detailledResult) - }) - .catch((error) => { - console.log(error) - }); - -// save detailled user -let user = `nicholaslosacco`; -let directory = `datas-folder`; - -Behance.saveDetailledUser(user, directory) - .then(() => { - console.log(`${user}.json saved in ${directory}`) - }) - .catch((error) => { - console.log(error) - }); \ No newline at end of file diff --git a/index.js b/index.js index 893075f..e1e84c4 100644 --- a/index.js +++ b/index.js @@ -1,8 +1,9 @@ -/* - Behance-node v0.0.3 - Project: https://github.com/ilies-t/behance-node - Author: ilies t - License: Apache License 2.0 -*/ +/** + * Behance-node v0.1.0 + * + * @link GitHub repository + * @author ilies t + * @licence Apache Licence 2.0 + */ module.exports = require('./lib/Behance'); \ No newline at end of file diff --git a/lib/Behance.js b/lib/Behance.js index 83857fd..03deafe 100644 --- a/lib/Behance.js +++ b/lib/Behance.js @@ -1,160 +1,175 @@ -/* - Behance-node v0.0.3 - Project: https://github.com/ilies-t/behance-node - Author: ilies t - License: Apache License 2.0 -*/ - -// import the required packages -const rp = require('request-promise'); - -// import some functions -const fun = require('./_functions'); - -// package ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€” -var Behance = { - /* - * user datas ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€” - */ - user: user = async (_user) => { - const link = `https://www.behance.net/${_user}`; - rp(link) - .then(jsonDatas = async () => { - // get JSON datas with _jsonDOM() - const jsonResp = await fun._jsonDOM(link); - - // make the response - const result = { - id: jsonResp.profile.owner.id, - first_name: jsonResp.profile.owner.first_name, - last_name: jsonResp.profile.owner.last_name, - username: jsonResp.profile.owner.username, - city: jsonResp.profile.owner.city, - state: jsonResp.profile.owner.state, - country: jsonResp.profile.owner.country, - location: jsonResp.profile.owner.location, - company: jsonResp.profile.owner.company, - occupation: jsonResp.profile.owner.occupation, - created_on: jsonResp.profile.owner.created_on, - url: jsonResp.profile.owner.url, - profile_picture: jsonResp.profile.owner.images['276'], - banner_image_url: jsonResp.profile.owner.banner_image_url, - followers: jsonResp.profile.owner.stats.followers, - following: jsonResp.profile.owner.stats.following, - appreciations: jsonResp.profile.owner.stats.appreciations, - views: jsonResp.profile.owner.stats.views, - comments: jsonResp.profile.owner.stats.comments, - }; - - return result; - }) - .catch((_err)=>{console.log(`šŸ”“ļø ${_err}\nšŸ”“ļø ERROR when get the profile data, Maybe the profile don't exist`)}); - return jsonDatas(); - }, - - /* - * detailled user datas ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€” - */ - detailledUser: detailledUser = async (_user) => { - const link = `https://www.behance.net/${_user}`; - rp(link) - .then(jsonDatas = async () => { - // get JSON datas with _jsonDOM() - const result = await _jsonDOM(link); - - // delete insignificant datas - fun._deleteUser(result); - - return result; - }) - .catch((_err)=>{console.log(`šŸ”“ļø ${_err}\nšŸ”“ļø ERROR when get the profile data, Maybe the profile don't exist`)}); - return jsonDatas(); - }, - - /* - * save user detailled datas in JSON file ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€” - */ - saveDetailledUser: saveDetailledUser = async (_user, _directory) => { - const link = `https://www.behance.net/${_user}`; - rp(link) - .then(jsonDatas = async () => { - // get JSON datas with _jsonDOM() - const result = await _jsonDOM(link); - - // delete insignificant datas - fun._deleteUser(result); - - // save with _saver() - fun._saver(result, _user, _directory); - }) - .catch((_err)=>{console.log(`šŸ”“ļø ${_err}\nšŸ”“ļø ERROR when get the profile data, Maybe the profile don't exist`)}); - }, - - /* - * detailled datas about project ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€” - */ - project: project = async (_project) => { - const link = `https://www.behance.net/gallery/${_project}`; - rp(link) - .then(jsonDatas = async () => { - // get JSON datas with _jsonDOM() - const result = await _jsonDOM(link); - - // delete not important datas - fun._deleteProject(result); - - return result; - }) - .catch((_err)=>{console.log(`šŸ”“ļø ${_err}\nšŸ”“ļø ERROR when get the project data, Maybe the project don't exist`)}); - return jsonDatas(); - }, - - /* - * return detailled datas ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€” - */ - saveProject: saveProject = async (_project, _directory) => { - const link = `https://www.behance.net/gallery/${_project}`; - rp(link) - .then(jsonDatas = async () => { - // get JSON datas with _jsonDOM() - const result = await _jsonDOM(link); - - // delete not important datas - fun._deleteProject(result); - - // write the datas in json file - fun._saver(result, _project, _directory); - }) - .catch((_err)=>{console.log(`šŸ”“ļø ${_err}\nšŸ”“ļø ERROR when get the project data, Maybe the project don't exist`)}); - return jsonDatas(); - }, - - /* - * return detailled datas ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€” - */ - randomProject: randomProject = async () => { - const link = `https://www.behance.net/search?content=projects&sort=featured_date&time=today`; - rp(link) - .then(jsonDatas = async () => { - // get JSON datas with _jsonDOM() - const jsonResp = await _jsonDOM(link); - - // get number of project in JSON returned - var objectInJSON = jsonResp.search.content.projects; - var count = Object.keys(objectInJSON).length; - - // choose random a project in JSON +"use strict"; +/** + * Behance-node v0.1.0 + * + * @link GitHub repository + * @author ilies t + * @licence Apache Licence 2.0 + */ +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +// import main methods +const MainMethods_1 = require("./MainMethods"); +/** + * Main package class + */ +class Behance { + // methods + /** + * Get user information by username. + * + * @param username Desired username. + * @param detailed Return more detail about user or not. + */ + static user(username, detailed) { + return __awaiter(this, void 0, void 0, function* () { + // get JSON data with jsonDOM() + return MainMethods_1.MainMethods.getJSONFromUrl(`https://www.behance.net/${username}`) + .then((jsonResponse) => { + // then return more data + if (detailed) { + return MainMethods_1.MainMethods.deleteInsignificantUserData(jsonResponse) + .then(() => { + return jsonResponse; + }); + // then return basic data + } + else { + // make the response + return { + id: jsonResponse.profile.owner.id, + first_name: jsonResponse.profile.owner.first_name, + last_name: jsonResponse.profile.owner.last_name, + username: jsonResponse.profile.owner.username, + city: jsonResponse.profile.owner.city, + state: jsonResponse.profile.owner.state, + country: jsonResponse.profile.owner.country, + location: jsonResponse.profile.owner.location, + company: jsonResponse.profile.owner.company, + occupation: jsonResponse.profile.owner.occupation, + created_on: jsonResponse.profile.owner.created_on, + url: jsonResponse.profile.owner.url, + profile_picture: jsonResponse.profile.owner.images['276'], + banner_image_url: jsonResponse.profile.owner.banner_image_url, + followers: jsonResponse.profile.owner.stats.followers, + following: jsonResponse.profile.owner.stats.following, + appreciations: jsonResponse.profile.owner.stats.appreciations, + views: jsonResponse.profile.owner.stats.views, + comments: jsonResponse.profile.owner.stats.comments, + }; + } + }); + }); + } + /** + * Get project data by project path. + * + * @param path Desired project path, must contain project id and name. + */ + static project(path) { + return __awaiter(this, void 0, void 0, function* () { + // get JSON data with jsonDOM() + return MainMethods_1.MainMethods.getJSONFromUrl(`https://www.behance.net/gallery/${path}`) + .then((jsonResponse) => { + return MainMethods_1.MainMethods.deleteInsignificantProjectData(jsonResponse) + .then(() => { + return jsonResponse; + }); + }); + }); + } + /** + * Get a random project data using sort. + * + * @param sort Type of sort. + * @param time Date parameter for sort. + */ + static randomProject(sort, time) { + return __awaiter(this, void 0, void 0, function* () { + // unknown parameters + if (![undefined, null, 'recommended', 'curated', 'most_appreciated', 'most_viewed', 'most_commented', 'most_recent'] + .includes(sort) && + ![undefined, null, 'today', 'this_week', 'this_month', 'all_time'].includes(time)) { + throw new Error(`'${sort}' is not a valid sort parameter`); + } + // create url + let URL = `https://www.behance.net/search`; + let sortURL = ``; + let timeURL = ``; + let isTimeSortAllowed; + // these cannot be sorted by date + if (sort === 'recommended' || sort === 'curated' || sort === 'most_recent') { + switch (sort) { + case 'curated': + sortURL = 'featured_date'; + break; + case 'most_recent': + sortURL = 'published_date'; + break; + default: + sortURL = 'recommended'; + break; + } + isTimeSortAllowed = false; + URL += `?sort=${sortURL}`; + } + // these can be sorted by date + else if (sort === 'most_appreciated' || sort === 'most_viewed' || sort === 'most_commented') { + switch (sort) { + case 'most_appreciated': + timeURL = 'appreciations'; + break; + case 'most_viewed': + timeURL = 'views'; + break; + case 'most_commented': + timeURL = 'comments'; + break; + } + isTimeSortAllowed = true; + URL += `?sort=${timeURL}`; + } + // append time + if (isTimeSortAllowed) { + switch (time) { + case 'today': + timeURL = 'today'; + break; + case 'this_week': + timeURL = 'week'; + break; + case 'this_month': + timeURL = 'month'; + break; + case 'all_time': + timeURL = 'all'; + break; + default: + timeURL = 'week'; + break; + } + URL += `&time=${timeURL}`; + } + // get JSON data with jsonDOM() + return MainMethods_1.MainMethods.getJSONFromUrl(URL) + .then((jsonResponse) => { + // get length of all projects + const count = Object.keys(jsonResponse.search.content.projects).length; + // random chose between all projects const theChosenOne = Math.floor(Math.random() * count); - const result = objectInJSON[theChosenOne]; - - return result; - }) - .catch((_err)=>{console.log(`šŸ”“ļø ${_err}\nšŸ”“ļø ERROR when get random project`)}); - return jsonDatas() - }, - // just a function to test if the library is succesfully loaded - safe:safe=(_err)=>{if(!_err){console.log(`āœ… Library is succesfully loaded!`)}else{console.log(`šŸ”“ļø Library ERROR\nšŸ”“ļø\t${_err}`)}} + return jsonResponse.search.content.projects[theChosenOne]; + }); + }); + } } - -exports['default'] = Behance; -module.exports = exports['default']; \ No newline at end of file +exports.Behance = Behance; +module.exports = Behance; +//# sourceMappingURL=Behance.js.map \ No newline at end of file diff --git a/lib/Behance.js.map b/lib/Behance.js.map new file mode 100644 index 0000000..7a8231e --- /dev/null +++ b/lib/Behance.js.map @@ -0,0 +1 @@ +{"version":3,"file":"Behance.js","sourceRoot":"","sources":["Behance.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;;;;;;;;;AAEH,sBAAsB;AACtB,+CAA4C;AAE5C;;GAEG;AACH,MAAa,OAAO;IAEhB,UAAU;IAEV;;;;;OAKG;IACI,MAAM,CAAO,IAAI,CAAC,QAAgB,EAAE,QAAkB;;YAEzD,+BAA+B;YAC/B,OAAO,yBAAW,CAAC,cAAc,CAAC,2BAA2B,QAAQ,EAAE,CAAC;iBAEnE,IAAI,CAAC,CAAC,YAAiB,EAAE,EAAE;gBAExB,wBAAwB;gBACxB,IAAI,QAAQ,EAAE;oBAEV,OAAO,yBAAW,CAAC,2BAA2B,CAAC,YAAY,CAAC;yBACvD,IAAI,CAAC,GAAG,EAAE;wBACP,OAAO,YAAY,CAAC;oBACxB,CAAC,CAAC,CAAC;oBAEP,yBA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,MAAM,CAAC,IAAI,CAC7B,YAAY,CAAC,MAAM,CAAC,OAAO,CAAC,QAAQ,CACvC,CAAC,MAAM,CAAC;gBAET,oCAAoC;gBACpC,MAAM,YAAY,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,KAAK,CAAC,CAAC;gBAEvD,OAAO,YAAY,CAAC,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAC,CAAC;YAE9D,CAAC,CAAC,CAAC;QAEX,CAAC;KAAA;CACJ;AAjMD,0BAiMC;AAED,MAAM,CAAC,OAAO,GAAG,OAAO,CAAC"} \ No newline at end of file diff --git a/lib/Behance.ts b/lib/Behance.ts new file mode 100644 index 0000000..28a90a0 --- /dev/null +++ b/lib/Behance.ts @@ -0,0 +1,210 @@ +/** + * Behance-node v0.1.0 + * + * @link GitHub repository + * @author ilies t + * @licence Apache Licence 2.0 + */ + +// import main methods +import { MainMethods } from './MainMethods'; + +/** + * Main package class + */ +export class Behance { + + // methods + + /** + * Get user information by username. + * + * @param username Desired username. + * @param detailed Return more detail about user or not. + */ + public static async user(username: string, detailed?: boolean): Promise { + + // get JSON data with jsonDOM() + return MainMethods.getJSONFromUrl(`https://www.behance.net/${username}`) + + .then((jsonResponse: any) => { + + // then return more data + if (detailed) { + + return MainMethods.deleteInsignificantUserData(jsonResponse) + .then(() => { + return jsonResponse; + }); + + // then return basic data + } else { + + // make the response + return { + id: jsonResponse.profile.owner.id, + first_name: jsonResponse.profile.owner.first_name, + last_name: jsonResponse.profile.owner.last_name, + username: jsonResponse.profile.owner.username, + city: jsonResponse.profile.owner.city, + state: jsonResponse.profile.owner.state, + country: jsonResponse.profile.owner.country, + location: jsonResponse.profile.owner.location, + company: jsonResponse.profile.owner.company, + occupation: jsonResponse.profile.owner.occupation, + created_on: jsonResponse.profile.owner.created_on, + url: jsonResponse.profile.owner.url, + profile_picture: jsonResponse.profile.owner.images['276'], + banner_image_url: jsonResponse.profile.owner.banner_image_url, + followers: jsonResponse.profile.owner.stats.followers, + following: jsonResponse.profile.owner.stats.following, + appreciations: jsonResponse.profile.owner.stats.appreciations, + views: jsonResponse.profile.owner.stats.views, + comments: jsonResponse.profile.owner.stats.comments, + }; + } + }); + } + + /** + * Get project data by project path. + * + * @param path Desired project path, must contain project id and name. + */ + public static async project(path: string): Promise { + + // get JSON data with jsonDOM() + return MainMethods.getJSONFromUrl(`https://www.behance.net/gallery/${path}`) + + .then((jsonResponse: any) => { + + return MainMethods.deleteInsignificantProjectData(jsonResponse) + .then(() => { + return jsonResponse; + }); + + }); + } + + /** + * Get a random project data using sort. + * + * @param sort Type of sort. + * @param time Date parameter for sort. + */ + public static async randomProject( + sort?: 'recommended' | 'curated' | 'most_appreciated' | 'most_viewed' | 'most_commented' | 'most_recent', + time?: 'today' | 'this_week' | 'this_month' | 'all_time' + ): Promise { + + // unknown parameters + if( + ![undefined, null, 'recommended', 'curated', 'most_appreciated', 'most_viewed', 'most_commented', 'most_recent'] + .includes(sort) && + ![undefined, null, 'today', 'this_week', 'this_month', 'all_time'].includes(time) + ) { + throw new Error(`'${sort}' is not a valid sort parameter`); + } + + // create url + let URL: string = `https://www.behance.net/search`; + let sortURL: string = ``; + let timeURL: string = ``; + let isTimeSortAllowed: boolean; + + // these cannot be sorted by date + if(sort === 'recommended' || sort === 'curated' || sort === 'most_recent') { + + switch (sort) { + + case 'curated': + sortURL = 'featured_date'; + break; + + case 'most_recent': + sortURL = 'published_date' + break; + + default: + sortURL = 'recommended'; + break; + } + + isTimeSortAllowed = false; + URL += `?sort=${sortURL}`; + + } + + // these can be sorted by date + else if(sort === 'most_appreciated' || sort === 'most_viewed' || sort === 'most_commented') { + + switch (sort) { + + case 'most_appreciated': + timeURL = 'appreciations'; + break; + + case 'most_viewed': + timeURL = 'views'; + break; + + case 'most_commented': + timeURL = 'comments'; + break; + } + + isTimeSortAllowed = true; + URL += `?sort=${timeURL}`; + } + + // append time + if(isTimeSortAllowed) { + switch (time) { + + case 'today': + timeURL = 'today'; + break; + + case 'this_week': + timeURL = 'week'; + break; + + case 'this_month': + timeURL = 'month'; + break; + + case 'all_time': + timeURL = 'all'; + break; + + default: + timeURL = 'week'; + break; + + } + + URL += `&time=${timeURL}`; + + } + + // get JSON data with jsonDOM() + return MainMethods.getJSONFromUrl(URL) + + .then((jsonResponse: any) => { + + // get length of all projects + const count: number = Object.keys( + jsonResponse.search.content.projects + ).length; + + // random chose between all projects + const theChosenOne = Math.floor(Math.random() * count); + + return jsonResponse.search.content.projects[theChosenOne]; + + }); + + } +} + +module.exports = Behance; \ No newline at end of file diff --git a/lib/MainMethods.js b/lib/MainMethods.js new file mode 100644 index 0000000..110bf5f --- /dev/null +++ b/lib/MainMethods.js @@ -0,0 +1,92 @@ +"use strict"; +/** + * Behance-node v0.1.0 + * + * @link GitHub repository + * @author ilies t + * @licence Apache Licence 2.0 + */ +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +// import the required packages +const rp = require('request-promise'); +const cheerio = require('cheerio'); +// import CSS value to get the JSON elements in HTML +const settings = require('./settings.json'); +class MainMethods { + /** + * Get JSON object from DOM. + * + * @param url URL to scrape. + */ + static getJSONFromUrl(url) { + return __awaiter(this, void 0, void 0, function* () { + // search CSS value with cheerio + const html = yield rp(url); + const $ = cheerio.load(html); + // filter to get only innerHTML + const result = $(settings.jsonResponseCss, html)[0].children[0].data; + // jsonify and return the result + return JSON.parse(result); + }); + } + /** + * Delete insignificant data about user. + * + * @param user User containing data to delete. + */ + static deleteInsignificantUserData(user) { + return __awaiter(this, void 0, void 0, function* () { + delete user.config; + delete user.gates; + delete user.user; + delete user.env; + delete user.profile.adminPermissions; + delete user.profile.admin; + delete user.profile.checklist; + delete user.profile.isReportModalVisible; + delete user.profile.activeSection; + delete user.profile.wip.uploader.postStoryStatusCode; + delete user.profile.wip.uploader.sendSMSStatusCode; + delete user.profile.wip.uploader.sessionId; + delete user.profile.wip.uploader.isUploadModalVisible; + delete user.profile.wip.uploader.shouldRedirectToLogin; + }); + } + /** + * Delete insignificant data about project. + * + * @param project Project containing data to delete. + */ + static deleteInsignificantProjectData(project) { + return __awaiter(this, void 0, void 0, function* () { + delete project.project.credits; + delete project.project.cssPaths; + delete project.project.admin; + delete project.project.shareConfig; + delete project.project.commentForm; + delete project.project.urls; + delete project.careers; + delete project.config; + delete project.footer; + delete project.footerAnalytics; + delete project.env; + delete project.user; + delete project.gates; + delete project.projectAnalytics; + delete project.onboardingAnalytics; + delete project.downloadAppModal; + delete project.downloadAppModalAnalytics; + }); + } +} +exports.MainMethods = MainMethods; +//# sourceMappingURL=MainMethods.js.map \ No newline at end of file diff --git a/lib/MainMethods.js.map b/lib/MainMethods.js.map new file mode 100644 index 0000000..34d38f6 --- /dev/null +++ b/lib/MainMethods.js.map @@ -0,0 +1 @@ +{"version":3,"file":"MainMethods.js","sourceRoot":"","sources":["MainMethods.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;;;;;;;;;AAEH,+BAA+B;AAC/B,MAAM,EAAE,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAC;AACtC,MAAM,OAAO,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;AAEnC,oDAAoD;AACpD,MAAM,QAAQ,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAC;AAE5C,MAAa,WAAW;IAEpB;;;;OAIG;IACI,MAAM,CAAO,cAAc,CAAC,GAAW;;YAE1C,gCAAgC;YAChC,MAAM,IAAI,GAAG,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC;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,OAAO,CAAC,OAAO,CAAC;YAC/B,OAAO,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC;YAChC,OAAO,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC;YAC7B,OAAO,OAAO,CAAC,OAAO,CAAC,WAAW,CAAC;YACnC,OAAO,OAAO,CAAC,OAAO,CAAC,WAAW,CAAC;YACnC,OAAO,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC;YAC5B,OAAO,OAAO,CAAC,OAAO,CAAC;YACvB,OAAO,OAAO,CAAC,MAAM,CAAC;YACtB,OAAO,OAAO,CAAC,MAAM,CAAC;YACtB,OAAO,OAAO,CAAC,eAAe,CAAC;YAC/B,OAAO,OAAO,CAAC,GAAG,CAAC;YACnB,OAAO,OAAO,CAAC,IAAI,CAAC;YACpB,OAAO,OAAO,CAAC,KAAK,CAAC;YACrB,OAAO,OAAO,CAAC,gBAAgB,CAAC;YAChC,OAAO,OAAO,CAAC,mBAAmB,CAAC;YACnC,OAAO,OAAO,CAAC,gBAAgB,CAAC;YAChC,OAAO,OAAO,CAAC,yBAAyB,CAAC;QAC7C,CAAC;KAAA;CAEJ;AAnED,kCAmEC"} \ No newline at end of file diff --git a/lib/MainMethods.ts b/lib/MainMethods.ts new file mode 100644 index 0000000..d3cc5ca --- /dev/null +++ b/lib/MainMethods.ts @@ -0,0 +1,83 @@ +/** + * Behance-node v0.1.0 + * + * @link GitHub repository + * @author ilies t + * @licence Apache Licence 2.0 + */ + +// import the required packages +const rp = require('request-promise'); +const cheerio = require('cheerio'); + +// import CSS value to get the JSON elements in HTML +const settings = require('./settings.json'); + +export class MainMethods { + + /** + * Get JSON object from DOM. + * + * @param url URL to scrape. + */ + public static async getJSONFromUrl(url: string): Promise { + + // search CSS value with cheerio + const html = await rp(url); + const $ = cheerio.load(html); + + // filter to get only innerHTML + const result = $(settings.jsonResponseCss, html)[0].children[0].data; + + // jsonify and return the result + return JSON.parse(result); + } + + /** + * Delete insignificant data about user. + * + * @param user User containing data to delete. + */ + public static async deleteInsignificantUserData(user: any) { + delete user.config; + delete user.gates; + delete user.user; + delete user.env; + delete user.profile.adminPermissions; + delete user.profile.admin; + delete user.profile.checklist; + delete user.profile.isReportModalVisible; + delete user.profile.activeSection; + delete user.profile.wip.uploader.postStoryStatusCode; + delete user.profile.wip.uploader.sendSMSStatusCode; + delete user.profile.wip.uploader.sessionId; + delete user.profile.wip.uploader.isUploadModalVisible; + delete user.profile.wip.uploader.shouldRedirectToLogin; + } + + /** + * Delete insignificant data about project. + * + * @param project Project containing data to delete. + */ + public static async deleteInsignificantProjectData(project: any) { + delete project.project.credits; + delete project.project.cssPaths; + delete project.project.admin; + delete project.project.shareConfig; + delete project.project.commentForm; + delete project.project.urls; + delete project.careers; + delete project.config; + delete project.footer; + delete project.footerAnalytics; + delete project.env; + delete project.user; + delete project.gates; + delete project.projectAnalytics; + delete project.onboardingAnalytics; + delete project.downloadAppModal; + delete project.downloadAppModalAnalytics; + } + +} \ No newline at end of file diff --git a/lib/_functions.js b/lib/_functions.js deleted file mode 100644 index 4ca2cb0..0000000 --- a/lib/_functions.js +++ /dev/null @@ -1,67 +0,0 @@ -/* - Behance-node v0.0.3 - Project: https://github.com/ilies-t/behance-node - Author: ilies t - License: Apache License 2.0 -*/ - -// import the required packages -const rp = require('request-promise'); -const fs = require('fs'); -const cheerio = require('cheerio'); - -// import CSS value to get the JSON elements in HTML -const settings = require('./settings.json'); - -var fun = { - // delete insgnifiant user datas - _deleteUser : _deleteUser = (__result) => { - delete __result.config; delete __result.gates; delete __result.user; delete __result.env; delete __result.profile.adminPermissions; delete __result.profile.admin; delete __result.profile.checklist; delete __result.profile.isReportModalVisible; delete __result.profile.activeSection; delete __result.profile.wip.uploader.postStoryStatusCode; delete __result.profile.wip.uploader.sendSMSStatusCode; delete __result.profile.wip.uploader.sessionId; delete __result.profile.wip.uploader.isUploadModalVisible; delete __result.profile.wip.uploader.shouldRedirectToLogin; - }, - - // delete insgnifiant project datas - _deleteProject : _deleteProject = (__result) => { - delete __result.project.credits; delete __result.project.cssPaths; delete __result.project.otherProjects; delete __result.project.admin; delete __result.project.shareConfig; delete __result.project.commentForm; delete __result.project.urls; delete __result.config; delete __result.footer; delete __result.env; delete __result.user; delete __result.gates; - }, - - // JSON tag finder in DOM - _jsonDOM : _jsonDOM = async (__link) => { - - // search CSS value with cheerio - const html = await rp(__link); - var $ = cheerio.load(html); - - // filter to get only the datas innerHTML - var result = $(settings.jsonResponseCss, html)[0].children[0].data; - - // jsonify and return the result - return JSON.parse(result); - }, - - // save JSON file - _saver : _saver = (__result, __datasType, __directory) => { - - // if directory don't exists, create it - if (!fs.existsSync(__directory)){ - fs.mkdirSync(__directory); - }; - - // write the datas in json file - const jsonContent = JSON.stringify(__result, null, 4); - - // replace to clarify the name - __datasType = __datasType.replace('-', '_'); - __datasType = __datasType.replace('/', '-'); - - // write it - fs.writeFile(`${__directory}/${__datasType}.json`, jsonContent, 'utf-8', (_err) => { - if(_err) { - console.log(`šŸ”“ļø ERROR with JSON save\nšŸ”“ļø ${_err}`); - } - }); - } - -} - -exports['default'] = fun; -module.exports = exports['default']; \ No newline at end of file diff --git a/lib/interfaces.js b/lib/interfaces.js new file mode 100644 index 0000000..3405842 --- /dev/null +++ b/lib/interfaces.js @@ -0,0 +1,10 @@ +"use strict"; +/** + * Behance-node v0.1.0 + * + * @link GitHub repository + * @author ilies t + * @licence Apache Licence 2.0 + */ +Object.defineProperty(exports, "__esModule", { value: true }); +//# sourceMappingURL=interfaces.js.map \ No newline at end of file diff --git a/lib/interfaces.js.map b/lib/interfaces.js.map new file mode 100644 index 0000000..5050104 --- /dev/null +++ b/lib/interfaces.js.map @@ -0,0 +1 @@ +{"version":3,"file":"interfaces.js","sourceRoot":"","sources":["interfaces.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG"} \ No newline at end of file diff --git a/lib/interfaces.ts b/lib/interfaces.ts new file mode 100644 index 0000000..8dccfe7 --- /dev/null +++ b/lib/interfaces.ts @@ -0,0 +1,31 @@ +/** + * Behance-node v0.1.0 + * + * @link GitHub repository + * @author ilies t + * @licence Apache Licence 2.0 + */ + +// TODO: implements other interfaces + +export interface IUser { + id: number, + first_name: string, + last_name: string, + username: string, + city: string, + state: string, + country: string, + location: string, + company: string, + occupation: string, + created_on: number, + url: string, + profile_picture: string, + banner_image_url: string, + followers: number, + following: number, + appreciations: number, + views: number, + comments: number, +} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index c3e3803..c93c55a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { - "name": "node-unsplash", - "version": "0.0.1", + "name": "behance-node", + "version": "0.1.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -10,9 +10,9 @@ "integrity": "sha512-W0+n1Y+gK/8G2P/piTkBBN38Qc5Q1ZSO6B5H3QmPCUewaiXOo2GCAWZ4ElZCcNhjJuBSUSLGFUJnmlCn5+nxOQ==" }, "bluebird": { - "version": "3.5.5", - "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.5.5.tgz", - "integrity": "sha512-5am6HnnfN+urzt4yfg7IgTbotDjIT/u8AJpEt0sIU9FtXfVeezXAPKswrG+xKUCOYAINpSdgZVDU6QFh+cuH3w==" + "version": "3.7.2", + "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.7.2.tgz", + "integrity": "sha512-XpNj6GDQzdfW+r2Wnn7xiSAd7TM3jzkxGXBGTtWKuSXv1xUV+azxAm8jdWZN06QTQk+2N2XB9jRDkvbmQmcRtg==" }, "boolbase": { "version": "1.0.0", @@ -84,11 +84,6 @@ "resolved": "https://registry.npmjs.org/entities/-/entities-1.1.2.tgz", "integrity": "sha512-f2LZMYl1Fzu7YSBKg+RoROelpOaNrcGmE9AZubeDfrCEia483oW4MI4VyFd5VNHIgQ/7qm1I0wUHK1eJnn2y2w==" }, - "fs": { - "version": "0.0.1-security", - "resolved": "https://registry.npmjs.org/fs/-/fs-0.0.1-security.tgz", - "integrity": "sha1-invTcYa23d84E/I4WLV+yq9eQdQ=" - }, "htmlparser2": { "version": "3.10.1", "resolved": "https://registry.npmjs.org/htmlparser2/-/htmlparser2-3.10.1.tgz", @@ -108,9 +103,9 @@ "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" }, "lodash": { - "version": "4.17.15", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz", - "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==" + "version": "4.17.19", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.19.tgz", + "integrity": "sha512-JNvd8XER9GQX0v2qJgsaN/mzFCNA5BRe/j8JN9d+tWyGLSodKQHKFicdwNYzWwI3wjRnaKPsGj1XkBjx/F96DQ==" }, "nth-check": { "version": "1.0.2", @@ -129,9 +124,9 @@ } }, "psl": { - "version": "1.3.1", - "resolved": "https://registry.npmjs.org/psl/-/psl-1.3.1.tgz", - "integrity": "sha512-2KLd5fKOdAfShtY2d/8XDWVRnmp3zp40Qt6ge2zBPFARLXOGUf2fHD5eg+TV/5oxBtQKVhjUaKFsAaE4HnwfSA==" + "version": "1.8.0", + "resolved": "https://registry.npmjs.org/psl/-/psl-1.8.0.tgz", + "integrity": "sha512-RIdOzyoavK+hA18OGGWDqUTsCLhtA7IcZ/6NCs4fFJaHBDab+pDDmDIByWFRQJq2Cd7r1OoQxBGKOaztq+hjIQ==" }, "punycode": { "version": "2.1.1", @@ -149,22 +144,22 @@ } }, "request-promise": { - "version": "4.2.4", - "resolved": "https://registry.npmjs.org/request-promise/-/request-promise-4.2.4.tgz", - "integrity": "sha512-8wgMrvE546PzbR5WbYxUQogUnUDfM0S7QIFZMID+J73vdFARkFy+HElj4T+MWYhpXwlLp0EQ8Zoj8xUA0he4Vg==", + "version": "4.2.5", + "resolved": "https://registry.npmjs.org/request-promise/-/request-promise-4.2.5.tgz", + "integrity": "sha512-ZgnepCykFdmpq86fKGwqntyTiUrHycALuGggpyCZwMvGaZWgxW6yagT0FHkgo5LzYvOaCNvxYwWYIjevSH1EDg==", "requires": { "bluebird": "^3.5.0", - "request-promise-core": "1.1.2", + "request-promise-core": "1.1.3", "stealthy-require": "^1.1.1", "tough-cookie": "^2.3.3" } }, "request-promise-core": { - "version": "1.1.2", - "resolved": "https://registry.npmjs.org/request-promise-core/-/request-promise-core-1.1.2.tgz", - "integrity": "sha512-UHYyq1MO8GsefGEt7EprS8UrXsm1TxEvFUX1IMTuSLU2Rh7fTIdFtl8xD7JiEYiWU2dl+NYAjCTksTehQUxPag==", + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/request-promise-core/-/request-promise-core-1.1.3.tgz", + "integrity": "sha512-QIs2+ArIGQVp5ZYbWD5ZLCY29D5CfWizP8eWnm8FoGD1TX61veauETVQbrV60662V0oFBkrDOuaBI8XgtuyYAQ==", "requires": { - "lodash": "^4.17.11" + "lodash": "^4.17.15" } }, "safe-buffer": { diff --git a/package.json b/package.json index e20b036..6411a81 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "behance-node", - "version": "0.0.3", - "description": "An asynchronous library to scrape Behance.", + "version": "0.1.0", + "description": "An asynchronous module for scrapping Behance using JavaScript.", "main": "index.js", "scripts": { "test": "node test/test.js", @@ -19,7 +19,6 @@ "license": "Apache-2.0", "dependencies": { "cheerio": "^1.0.0-rc.3", - "fs": "0.0.1-security", - "request-promise": "^4.2.4" + "request-promise": "^4.2.5" } } diff --git a/test/test.js b/test/test.js index f97d2fc..f8fc6a7 100644 --- a/test/test.js +++ b/test/test.js @@ -1,5 +1,103 @@ +/** + * Behance-node v0.1.0 + * + * @link GitHub repository + * @author ilies t + * @licence Apache Licence 2.0 + */ + 'use strict'; const Behance = require('../index'); -Behance.safe(); \ No newline at end of file +/** + * Get user and project data to test. + */ +Behance.randomProject() + .then((result) => { + + // define user data test + let projectTest = result['url'].replace('https://www.behance.net/gallery/', ''); + + // define project data test + let userTest = result['owners'][0]['username']; + + // data tests are ready + + /** + * Test `user()` method + */ + Behance.user(userTest) + .then((result) => { + console.log(userTest); + console.assert( + result !== undefined && result !== null, + 'it should return desired user data' + ); + }); + + Behance.user(userTest, true) + .then((result) => { + console.assert( + result !== undefined && result !== null, + 'it should return detailed desired user data' + ); + }); + + /** + * Test `project()` method + */ + Behance.project(projectTest) + .then((result) => { + console.assert( + result !== undefined && result !== null, + 'it should return desired project data' + ); + }); + + /** + * Test `randomProject()` method + */ + Behance.randomProject() + .then((result) => { + console.assert( + result !== undefined && result !== null, + 'it should return a random project sort by recommended' + ); + }); + + Behance.randomProject('recommended') + .then((result) => { + console.assert( + result !== undefined && result !== null, + 'it should return a random project sort by recommended (specified sort)' + ); + }); + + Behance.randomProject('curated', 'all_time') + .then((result) => { + console.assert( + result !== undefined && result !== null, + 'it should ignore time argument because sort' + ); + }); + + Behance.randomProject('most_appreciated', 'this_week') + .then((result) => { + console.assert( + result !== undefined && result !== null, + 'it should return a random project sort by most appreciated and this week' + ); + }); + + Behance.randomProject('most_commented', 'all_time') + .then((result) => { + console.assert( + result !== undefined && result !== null, + 'it should return a random project sort by most appreciated and this week' + ); + }); + + + + }); \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..d0afb93 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,17 @@ +{ + "compileOnSave": true, + "compilerOptions": { + "allowSyntheticDefaultImports": true, + "declaration": false, + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "lib": ["es6", "dom"], + "module": "CommonJS", + "moduleResolution": "node", + "sourceMap": true, + "target": "ES2016" + }, + "include": [ + "lib/*.ts" + ] +} \ No newline at end of file