Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add a real cli #4

Merged
merged 2 commits into from
Oct 12, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: add a cli
Signed-off-by: Logan McAnsh <logan@mcan.sh>
  • Loading branch information
mcansh committed Oct 3, 2020
commit 9625c893c85bbdf17376f3bda789a691cb9715bd
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.ds_store
node_modules
dist
output.csv
*.csv
*.env
.eslintcache
38 changes: 33 additions & 5 deletions package-lock.json

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

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -5,20 +5,23 @@
"license": "MIT",
"author": "Logan McAnsh <logan@mcan.sh> (https://mcan.sh)",
"main": "dist/index.js",
"bin": "dist/cli.js",
"scripts": {
"build": "tsc",
"lint": "eslint --cache --ext .js,.ts --fix"
},
"dependencies": {
"csv-writer": "1.6.0",
"dotenv": "8.2.0",
"he": "1.2.0",
"node-fetch": "2.6.1"
"kleur": "4.1.3",
"node-fetch": "2.6.1",
"sade": "1.7.4"
},
"devDependencies": {
"@types/he": "1.1.1",
"@types/node": "14.11.2",
"@types/node-fetch": "2.5.7",
"@types/sade": "1.7.2",
"@typescript-eslint/eslint-plugin": "4.3.0",
"@typescript-eslint/parser": "4.3.0",
"eslint": "7.10.0",
11 changes: 0 additions & 11 deletions src/@types/global.d.ts

This file was deleted.

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

import sade from 'sade';
import kleur from 'kleur';
import { fetchShopListings, Options } from './';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const pkg = require('../package.json');

const prog = sade(pkg.name).version(pkg.version);

prog
.command('generate')
.option('--shop', 'the name of your shop')
.option('--slug', 'the slug of your shop, found after https://etsy.com/')
.option('--domain', 'the custom domain you have pointed to your shop')
.option('--apiKey', 'the api key you got from etsy')
.option('--fallbackTaxonomy', 'a fallback taxonomy path')
.option('--noDigital', 'whether or not to filter out digital listings', true)
.action(async (args: Options) => {
if (!args.shop || !args.slug || !args.domain || !args.apiKey) {
console.log(
kleur.red(
`You must supply all of the following arguments: 'shop', 'slug', 'domain', and 'apiKey'`
)
);
process.exit(1);
}

try {
await fetchShopListings(args, 1, []);
console.log(kleur.green(`saved ${args.slug}.csv!`));
process.exit(0);
} catch (error) {
console.error(kleur.red('something went wrong'));
process.exit(1);
}
});

prog.parse(process.argv);
74 changes: 41 additions & 33 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,11 @@
import path from 'path';
import { format } from 'url';

import fetch, { RequestInfo, RequestInit } from 'node-fetch';
import { createObjectCsvWriter } from 'csv-writer';
import he from 'he';
import * as dotenv from 'dotenv';

import { ApiResponse, Result } from './@types/shop';

dotenv.config({ path: path.join(process.cwd(), '.env') });

const {
ETSY_SHOP_NAME,
ETSY_API_KEY,
ETSY_SHOP_SLUG,
ETSY_DOMAIN,
ETSY_CATEGORY,
} = process.env;

if (!ETSY_SHOP_NAME || !ETSY_API_KEY || !ETSY_SHOP_SLUG || !ETSY_DOMAIN) {
throw new Error(
`Missing one of the following environment variables 'ETSY_SHOP_NAME', 'ETSY_API_KEY', 'ETSY_SHOP_SLUG', 'ETSY_DOMAIN'`
);
}

const args = process.argv.slice(2);

const typedFetch = async <T>(
input: RequestInfo,
init: RequestInit = {}
@@ -38,18 +18,47 @@ const typedFetch = async <T>(
return promise.json();
};

export interface Options {
/**
* @description the name of your shop
*/
shop: string;
/**
* @description the slug of your shop, found after https://etsy.com/
*/
slug: string;
/**
* @description the custom domain you have pointed to your shop
*/
domain: string;
/**
* @description the api key you got from etsy.
*/
apiKey: string;
/**
* @description a fallback taxonomy path
* @see https://www.google.com/basepages/producttype/taxonomy.en-US.txt
* @example Arts & Entertainment > Party & Celebration > Gift Giving > Greeting & Note Cards
*/
fallbackTaxonomy?: string;
/**
* @description whether or not to filter out digital listings
* @default true
*/
noDigital: boolean;
}

async function fetchShopListings(
options: Options,
page: number,
currentItems: Result[] = []
): Promise<void> {
const filterDigitalItems = args[0] === '--filter-digital';

const url = format({
protocol: 'https',
host: 'openapi.etsy.com',
pathname: `/v2/shops/${ETSY_SHOP_SLUG}/listings/active`,
pathname: `/v2/shops/${options.slug}/listings/active`,
query: {
api_key: ETSY_API_KEY,
api_key: options.apiKey,
limit: 100,
page,
includes: 'MainImage',
@@ -58,18 +67,18 @@ async function fetchShopListings(

const data = await typedFetch<ApiResponse>(url);

const items = filterDigitalItems
const items = options.noDigital
? data.results.filter((item) => !item.is_digital)
: data.results;

const nextPage = data.pagination.next_page;

const merged = [...currentItems, ...items];

if (nextPage) return fetchShopListings(nextPage, merged);
if (nextPage) return fetchShopListings(options, nextPage, merged);

const csvWriter = createObjectCsvWriter({
path: './output.csv',
path: `./${options.slug}.csv`,
header: [
{ id: 'id', title: 'id' },
{ id: 'title', title: 'title' },
@@ -92,16 +101,15 @@ async function fetchShopListings(
availability: 'in stock',
condition: 'new',
price: `${item.price} ${item.currency_code}`,
link: item.url.replace('https://www.etsy.com', ETSY_DOMAIN),
link: item.url.replace('https://www.etsy.com', options.domain),
image_link: item.MainImage.url_fullxfull,
brand: ETSY_SHOP_NAME,
google_product_category: ETSY_CATEGORY
? ETSY_CATEGORY
: item.taxonomy_path.join(' > ') || '',
brand: options.shop,
google_product_category:
item.taxonomy_path?.join(' > ') ?? options.fallbackTaxonomy ?? '',
};
});

return csvWriter.writeRecords(records);
}

fetchShopListings(1);
export { fetchShopListings };
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@
"sourceMap": true,
"outDir": "dist",
"baseUrl": ".",
"resolveJsonModule": true,
"paths": {
"*": ["node_modules/*", "src/types/*"]
}