diff --git a/.gitignore b/.gitignore index 25c8fdb..e842f3c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ node_modules -package-lock.json \ No newline at end of file +package-lock.json +.yalc +yalc.lock diff --git a/README.md b/README.md index d4f69d1..5b940d9 100644 --- a/README.md +++ b/README.md @@ -2,27 +2,29 @@ [![NPM License](https://img.shields.io/npm/l/all-contributors.svg?style=flat)](https://github.com/exoticchild-alt/pastebin-scraper/blob/master/LICENSE) [![NPM Downloads](https://img.shields.io/npm/dt/pastebin-scraper.svg?style=flat)]() -[![NPM](https://nodei.co/npm/pastebin-scraper.png?downloads=true)](https://www.npmjs.com/pastebin-scraper) +[![NPM](https://nodei.co/npm/pastebin-scraper.png?downloads=true)](https://www.npmjs.com/pastebin-scraper)
-[![GitHub Release](https://img.shields.io/github/release/exoticchild-alt/pastebin-scraper.svg?style=flat)]() +[![GitHub Release](https://img.shields.io/github/release/exoticchild-alt/pastebin-scraper.svg?style=flat)]() [![Issues](https://img.shields.io/github/issues-raw/exoticchild-alt/pastebin-scraper.svg?maxAge=25000)](https://github.com/exoticchild-alt/pastebin-scraper/issues) [![run on repl.it](https://repl.it/badge/github/exoticchild-alt/pastebin-scraper)](https://repl.it/github/exoticchild-alt/pastebin-scraper) [![Open Source](https://badges.frapsoft.com/os/v1/open-source.svg?v=103)](https://opensource.org/) [![Awesome](https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg)](https://github.com/sindresorhus/awesome) ### *Usage* ### -First, to be able to use the library, install it first using the following command +First, to be able to use the library, install it first using the following command: +```bash +npm install pastebin-scraper --save ``` -$ npm install pastebin-scraper --save -``` -This library is a promise-based library, so you first must require it in your code and then use it somewhat like this. + +This library is a promise-based library, so you first must require it in your code and then import the function `Scrape`: ```javascript -const scrape = require('pastebin-scraper') -scrape('https://pastebin.com/4Z489JeC') +const {Scrape} = require('pastebin-scraper'); +Scrape('https://pastebin.com/4Z489JeC') .then(res => console.log(res)) -//response looks something like this +``` -/* +Output will be an object, which can be accessed using properties. +```bash { title: 'Pastebin Scraper', views: '8', @@ -32,8 +34,8 @@ scrape('https://pastebin.com/4Z489JeC') dateCreated: 'Aug 15th, 2021', language: 'text' } -*/ ``` -You can run this test this library in the tryit.js folder if you are, for example, running this library in Github or Replit. + +You can run this test this library in the tryit.js folder if you are, for example, running this library in GitHub or Replit.
-*v1.0.1* \ No newline at end of file +*v2.0.0* diff --git a/dist/index.d.ts b/dist/index.d.ts new file mode 100644 index 0000000..83658a4 --- /dev/null +++ b/dist/index.d.ts @@ -0,0 +1,9 @@ +export declare function Scrape(url: string): Promise<{ + title: string; + views: string; + rawData: string; + user: string; + expiration: string; + dateCreated: string; + language: string; +}>; diff --git a/dist/index.js b/dist/index.js new file mode 100644 index 0000000..42a5a7f --- /dev/null +++ b/dist/index.js @@ -0,0 +1,30 @@ +"use strict"; +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 }); +exports.Scrape = void 0; +const cheerio = require("cheerio"); +const axios = require('axios').default; +function Scrape(url) { + return __awaiter(this, void 0, void 0, function* () { + const response = yield axios.get(url); + const $ = cheerio.load(response.data); + return { + title: $("body > div.wrap > div.container > div.content > div.post-view > div.details > div.info-bar > div.info-top > h1").text(), + views: $("body > div.wrap > div.container > div.content > div.post-view > div.details > div.info-bar > div.info-bottom > div.visits").text().replace("\n", "").trim(), + rawData: $("body > div.wrap > div.container > div.content > div.post-view > textarea").text(), + user: $("body > div.wrap > div.container > div.content > div.post-view > div.details > div.info-bar > div.info-bottom > div.username > a").text(), + expiration: $("body > div.wrap > div.container > div.content > div.post-view > div.details > div.info-bar > div.info-bottom > div.expire").text().replace("\n", "").trim(), + dateCreated: $("body > div.wrap > div.container > div.content > div.post-view > div.details > div.info-bar > div.info-bottom > div.date > span").text(), + language: $("body > div.wrap > div.container > div.content > div.post-view > div.highlighted-code > div.top-buttons > div.left > a").text() + }; + }); +} +exports.Scrape = Scrape; diff --git a/index.js b/lib/index.ts similarity index 86% rename from index.js rename to lib/index.ts index 04ffc43..20401a3 100644 --- a/index.js +++ b/lib/index.ts @@ -1,15 +1,15 @@ -const cheerio = require('cheerio'); -const axios = require('axios'); -module.exports = async function(url) { +import cheerio = require('cheerio'); +const axios = require('axios').default; +export async function Scrape(url: string) { const response = await axios.get(url); const $ = cheerio.load(response.data); return { title: $("body > div.wrap > div.container > div.content > div.post-view > div.details > div.info-bar > div.info-top > h1").text(), - views: $("body > div.wrap > div.container > div.content > div.post-view > div.details > div.info-bar > div.info-bottom > div.visits").text().replace("\n", "").trim(), + views: $("body > div.wrap > div.container > div.content > div.post-view > div.details > div.info-bar > div.info-bottom > div.visits").text().replace("\n", "").trim(), rawData: $("body > div.wrap > div.container > div.content > div.post-view > textarea").text(), user: $("body > div.wrap > div.container > div.content > div.post-view > div.details > div.info-bar > div.info-bottom > div.username > a").text(), expiration: $("body > div.wrap > div.container > div.content > div.post-view > div.details > div.info-bar > div.info-bottom > div.expire").text().replace("\n", "").trim(), dateCreated: $("body > div.wrap > div.container > div.content > div.post-view > div.details > div.info-bar > div.info-bottom > div.date > span").text(), language: $("body > div.wrap > div.container > div.content > div.post-view > div.highlighted-code > div.top-buttons > div.left > a").text() } -} \ No newline at end of file +} diff --git a/package.json b/package.json index 3a45588..c258827 100644 --- a/package.json +++ b/package.json @@ -1,24 +1,34 @@ { "name": "pastebin-scraper", - "version": "1.0.1", + "version": "2.0.0", "description": "A simple library that scrapes data about pastes from Pastebin", - "main": "index.js", + "main": "dist/index.js", + "types": "dist/index.d.ts", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "prepublish": "npm run build", + "pretest": "yalc publish && yalc add pastebin-scraper", + "test": "node tryit.js", + "tsc-init": " cd node_modules/.bin/ && tsc --init", + "build": "tsc" }, "repository": { "type": "git", "url": "git+https://github.com/exoticchild-alt/pastebin-scraper.git" }, "keywords": ["pastebin", "scraper", "web", "cheerio", "axios"], - "author": "", + "author": "https://github.com/exoticchild", "license": "MIT", "bugs": { "url": "https://github.com/exoticchild-alt/pastebin-scraper/issues" }, "homepage": "https://github.com/exoticchild-alt/pastebin-scraper#readme", "dependencies": { - "axios": "^0.21.1", + "axios": "^0.24.0", "cheerio": "^1.0.0-rc.10" + }, + "devDependencies": { + "@types/node": "^16.11.7", + "typescript": "^4.4.4", + "yalc": "^1.0.0-pre.53" } } diff --git a/tryit.js b/tryit.js index 0fd71f6..c397c60 100644 --- a/tryit.js +++ b/tryit.js @@ -1,4 +1,7 @@ //here you can try this library -//const pastebin = require('pastebin-scraper') -const scrape = require('pastebin-scraper') -scrape('https://pastebin.com/4Z489JeC').then(res => console.log(res)) \ No newline at end of file +//const {scrape} = require('pastebin-scraper') +const {Scrape} = require('pastebin-scraper'); +const res = Scrape('https://pastebin.com/4Z489JeC') +.then(res => console.log(res)); +//console.log(res); +console.log(typeof res); diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..e02f780 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "target": "es6", + "module": "commonjs", + "declaration": true, + "outDir": "./dist", + "strict": true, + "moduleResolution": "node" + } +}