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

@hk-artificial-intelligence-association/weibo-api

v1.0.4

Published

元狲科技封装的新浪微博API

Downloads

3

Readme

WeiboAPI

基于 m.weibo.cn 相关接口封装的访问新浪微博的API。主要用于微博相关的数据获取。

0. 安装

目前该 package 存放在 github 的 npm registry 下,因此在安装时,您需要在您的项目目录里添加 .npmrc 文件,并且添加以下行:

@hk-artificial-intelligence-association:registry=https://npm.pkg.github.com/

而后运行:

npm install @hk-artificial-intelligence-association/weibo-api@latest

1. 使用示例

目前,对于通常的需求而言,仅提供了获取指定微博用户前十条最新微博的 API 函数 fetchUserLatestWeiboByUID :

import {
  fetchUserLatestWeiboByUID,
  fetchUserLatestWeiboByUIDWithoutParsed,
  analyazeWeiboInfoToOneSentenceSummary,
  parseCardInfosToWeiboInfos,
  type WeiboInfo,
  type Card
} from '@hk-artificial-intelligence-association/weibo-api'

// 使用示例
// 获取指定uid的用户最新微博
async function getLatestWeibo(uid: string) {
  // fetchUserLatestWeiboByUID 会自动对结果进行解析,它会将结果转换为我们自己定义的类型 WeiboInfo ,提取出一些关键信息
  const weiboInfo = await fetchUserLatestWeiboByUID(uid)
  console.log('最新微博信息:', weiboInfo)
}

// 获取未解析的用户最新微博
async function getLatestWeiboWithoutParsing(uid: string) {
  // fetchUserLatestWeiboByUIDWithoutParsed 会获取 Card[] 信息,它对应于接口  https://m.weibo.cn/api/container/getIndex 返回体里 data.cards 的数据
  const rawWeiboData = await fetchUserLatestWeiboByUIDWithoutParsed(uid)
  console.log('未解析的微博数据:', rawWeiboData)
}


async function analyzeWeibo(uid: string) {
  const weiboInfo = await fetchUserLatestWeiboByUID(uid)
  // 分析 WeiboInfo 并生成一句话摘要
  // 如果想要将微博原数据转化为 "XXX 在 2024-01-01 23:59 发布了一条微博,微博内容为:" 这样的语句,可以使用该分析函数
  const summary = await analyazeWeiboInfoToOneSentenceSummary(weiboInfo[0])
  console.log('微博摘要:', summary)
}


function parseCards(cards: Card[]) {
  // 将卡片信息解析为微博信息
  const weiboInfos: WeiboInfo[] = parseCardInfosToWeiboInfos(cards)
  console.log('解析后的微博信息:', weiboInfos)
}

2. UID 的获取

  1. 通过搜索引擎等,在网页端找到你想要找的博主,进入ta的主页:

找到你想要获取的数据的博主

  1. 在对应主页的顶端 https://weibo.com/u/7797535872/u/ 后跟的数字便是 UID。

2-UID

  1. 需要注意的是,部分用户可能是不能被我们使用的接口访问到的。因此,你需要访问 https://m.weibo.cn/u/{你获取的UID} 这个页面,看看能不能这个页面能否正常访问。如果可以,那么便可以使用本包的封装函数访问。

3. 关键的类型定义

WeiboInfo 是本包自定义的关键的类型:

/**
 * 目前看起来只有发原创推文和转发推文的情况
 */
export type WeiboUserActionType = 'tweet' | 'retweet'

export type ParsedWeiboUserInfo = {
  id: number,
  name: string,
  avatar: string,
  description: string
}

export type BaseWeiboInfo = {
  action: WeiboUserActionType
  user: ParsedWeiboUserInfo
  text: string
  rawText: string
  /**
   * 点赞数
   */
  attitudesCount: number
  /**
   * 评论数
   */
  commentsCount: number
  /**
   * 转发数
   */
  retweetCount: number
  createdAt: string
  /**
   * 博文 Id
   */
  weiboId: string
  /**
   * 发微博的Ip定位
   */
  region?: string
}

/**
 * 原创推文
 */
export type OrginalWeiboInfo = BaseWeiboInfo & {
  action: 'tweet'
}

/**
 * 转发的推文
 */
export type RetweetWeiboInfo = BaseWeiboInfo & {
  action: 'retweet'
  retweetedWeibo: OrginalWeiboInfo
}

export type WeiboInfo = OrginalWeiboInfo | RetweetWeiboInfo

WeiboInfo 的示例数据:

{
  user: {
    id: 7797535872,
    name: '李問道Erika',
    avatar: 'https://wx3.sinaimg.cn/orj480/008vHFhCly8h7gdpq3zepj30u00u0jru.jpg',
    description: '痛苦。已卸载app端微博。'
  },
  text: '轉發微博',
  rawText: '轉發微博',
  attitudesCount: 0,
  commentsCount: 0,
  retweetCount: 0,
  createdAt: 'Wed Sep 18 12:11:03 +0800 2024',
  weiboId: '5079927940186629',
  region: '发布于 浙江',
  action: 'retweet',
  retweetedWeibo: {
    action: 'tweet',
    user: {
      id: 1706699904,
      name: 'i陆三金',
      avatar: 'https://ww2.sinaimg.cn/orj480/65ba2c80jw1e8qgp5bmzyj2050050aa8.jpg',
      description: '机器人、自动驾驶、AI 视频及相关'
    },
    text: '再次读到了 OpenAI 创立之始定下的技术目标,穿透时间,仍具有很强的指引作用。稍作删减:<br /><br />《OpenAI 技术目标》,2016 年 6 月 20 日,by Ilya Sutskever、Greg Brockman、Sam Altman、Elon Musk。<br /><br />「OpenAI 的使命是建立安全的人工智能,并确保人工智能的收益尽可能广泛而均衡地分配。<br /><br />为智能下定 ...<a href="/status/5036474810502028"> 全文</a>',
    rawText: undefined,
    attitudesCount: 0,
    commentsCount: 3,
    retweetCount: 54,
    createdAt: 'Tue May 21 14:23:51 +0800 2024',
    weiboId: '5036474810502028',
    region: '发布于 北京'
  }
}

CONTRIBUTION

欢迎提交PR或ISSUE完善本包的功能。

CONTRIBUTORS