Skip to content

Commit

Permalink
feat: cli scraping
Browse files Browse the repository at this point in the history
  • Loading branch information
dimfu committed Jun 4, 2023
1 parent 980b1a8 commit 3279954
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Scrap food recipe data from various websites that support JSON+LD and Microdata
## Install

```bash
> npm install --save @dimfu/recipe-scraper
npm install --save @dimfu/recipe-scraper
```

## Usage
Expand Down Expand Up @@ -148,6 +148,30 @@ getRecipeData(options).then((recipe) => {

You can specify the URL by either passing it as the first parameter, or by setting it in the options object.

## Command line usage

### Using without installation

```bash
npx @dimfu/recipe-scraper https://example/recipes/creamy-courgette-potato-bake
```

> Note: Use this method only if you plan to use for one time, installing globally (see-below) is recommended for multiple time usages.
### Installation

```bash
npm install @dimfu/recipe-scraper -g
```

> Note for Linux & MacOS users: DO NOT use sudo to install global packages! The correct way is to tell npm where to install its global packages: npm config set prefix ~/.local. Make sure ~/.local/bin is added to PATH.
### Usage after installation

```
@dimfu/recipe-scraper https://example/recipes/creamy-courgette-potato-bake
```

## License

Distributed under the MIT License. See [LICENSE](LICENSE) for more information.
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dimfu/recipe-scraper",
"version": "0.2.0",
"version": "0.3.0",
"description": "Extract recipe data from the web effortlessly",
"publishConfig": {
"access": "public"
Expand Down Expand Up @@ -30,16 +30,18 @@
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"bin": "./dist/cli.js",
"files": [
"dist"
],
"scripts": {
"build-fast": "tsup src/index.ts --format cjs,esm",
"build-fast": "tsup src/index.ts --format cjs,esm && tsup src/cli.ts",
"build": "pnpm run build-fast --dts-resolve",
"prepublishOnly": "pnpm run build"
},
"devDependencies": {
"@antfu/eslint-config": "^0.39.4",
"@types/node": "^20.2.5",
"eslint": "^8.41.0",
"lint-staged": "^13.2.2",
"simple-git-hooks": "^2.8.1",
Expand All @@ -55,6 +57,7 @@
"dependencies": {
"axios": "^1.4.0",
"cheerio": "1.0.0-rc.12",
"commander": "^10.0.1",
"iso8601-duration": "^2.1.1",
"jsonschema": "^1.4.1",
"microdata-node": "^2.0.0",
Expand Down
9 changes: 8 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env node

import { program } from 'commander'
import pkg from '../package.json'
import getRecipeData from '.'

const { name, description, version } = pkg

program.name(`npx ${name}`).version(version).allowExcessArguments(false).arguments('<url>').description(description, { url: 'food recipe url' }).action(async (url) => {
try {
console.log(await (getRecipeData(url)))
}
catch (err: unknown) {
console.error((err as { message: string }).message)
process.exit(1)
}
})

program.parseAsync().catch((e: unknown) => {
console.error((e as { message: string }).message)
process.exit(1)
})

0 comments on commit 3279954

Please sign in to comment.