npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pinduoduo

v1.0.3

Published

Non-official Promise Node library for pinduoduo

Downloads

5

Readme

Pinduoduo

拼多多开放平台非官方 Promise Node 库 Non-official Promise Node library for pinduoduo.com

安装

npm i pinduoduo --save

快速上手

const client_id = '8aa57095eacf449f94fa61e6c761a202'; //这里设置你自己的 client_id
const client_secret = ''; // 这里设置成自己的 client_secret
const Pinduoduo = require('pinduoduo');
const pdd = new Pinduoduo({
    client_id,
}, client_secret);
const goods = await pdd.ddk.goods.recommend.get({
    offset: 0
});

设计理念

使用 Proxy 对 API 进行了映射,使得以简单的元编程(meta programming)方式支持了全部 API(暂时不覆盖授权部分)。

Pinduoduo 构造函数

class Pinduoduo {
    constructor(commonArgs[, client_secret, options]) { }
}

commonArgs - 公共参数

必填项:client_id 对于不需要授权的 API,本类库会自动生成公众请求参数并签名; 需要授权的 API 需要手动传入 access_token

client_secret

请在 open.pinduoduo.com 后台获取 第二个参数也可以是 options,client_secret 作为它的一个键传入。

options 参数

options = {
    url: 'https://gw-api.pinduoduo.com/api/router',
    getNestedResponse: true
}

类库的使用选项

  • url - 请求网关地址,默认为正式环境 https://gw-api.pinduoduo.com/api/router。注意官方提供的 url 是 http 而非 https 的。如果希望以官方形式,或者希望传入沙箱 url 可修改此配置。
  • getNestedResponse - 是否自动解析嵌套的 response
  • getBuiltUrl - 是否直接返回构建好的 URL

返回数据的自动解析:getNestedResponse

目的是使拼多多返回数据更简洁易读,仅仅解析第一级 JSON 数据。更深层次的内容不会解析。

  • 当响应的 JSON 格式仅只有一个 key 时,自动返回 key 内的值;
  • 当响应的 JSON 格式 key 以 _response 结尾时,自动返回此 key 内的值;
  • 其他情况会抛出 Error,提示 Can't get nested response.

直接返回构建好的 URL:getBuiltUrl

如果希望在客户端直接调用,或者希望使用自己的 http request lib,可以通过此功能直接获取构建好的 url,然后自己通过 post 到此 url 即可调用。 开启此选项时,不要使用 await 调用。返回的不是 Promise 对象,而直接是字符串。

此外需要注意拼多多开放平台的 IP 白名单功能。

API 调用

使用 new 生成变量名为 pdd 的实例后,即可以官方文档中的风格调用,请求参数以 Object 对象传入函数的第一个参数。

示例:pdd.goods.cats.get(商品标准类目接口)

const cats = await pdd.goods.cats.get({
    parent_cat_id: 0
})

错误处理

当拼多多返回出错信息时,将会抛出 Error 对象。 Error.name - 错误码 Error.message - 错误描述

try {
    const goods = await pdd.ddk.goods.recommend.get({
        offset: 0
    });
    console.log(goods);
} catch (err) {
    console.log('#', err.name, err.message);
}

Contributors

Dai Jie [email protected]