@hkai-ai/twitter-data-fetch
v1.0.19
Published
推特接口API
Readme
Twitter-Data-Fetch
该仓库为获取推特数据的接口封装代码。
请注意,现在的它是一个封装的函数库。你应该调用封装好的函数进行调用。
NOTE: 该 package 预计在很长一段时间里都不会进行维护了。推荐使用 https://github.com/fa0311/twitter-openapi-typescript 这个库。
0. 目前支持的接口 Support
- 列表的最新推文数据,对应推特接口为
/ListLatestTweetsTimeline - 某用户的最新 12 条推文数据,对应推特接口为
/UserTweets
1. 安装 Installation
请注意,现在的它是一个封装的函数库。
如果你想通过 npm packages 安装,直接运行:
npm install @hk-artificial-intelligence-association/twitter-data-fetch@latest即可。
如果你想通过 Github packages 的镜像安装,你应该首先在引入的工程根目录下添加 .npmrc 文件引入 @hk-artificial-intelligence-association 的正确 registry:
@hk-artificial-intelligence-association:registry=https://npm.pkg.github.com/
//npm.pkg.github.com/:_authToken= the token而后运行命令安装:
npm install @hk-artificial-intelligence-association/twitter-data-fetch@latest如果你没有安装 playwright ,你还需要运行这些命令进行安装:
npm install playwright
npx playwright install核心的获取功能函数都是依赖于 playwright 的,因此,如果你需要获取 twitter 的数据,应当安装 playwright 。
2. 示例使用 Sample/Usage
对于封装的函数调用:
import { loginToGetCookies } from "@hk-artificial-intelligence-association/twitter-data-fetch";
import { getTwitterListNewlyTweetsWithCookies } from "@hk-artificial-intelligence-association/twitter-data-fetch";
import { getTwitterUserNewlyTweetsWithCookies } from "@hk-artificial-intelligence-association/twitter-data-fetch";
import { simplyParseTweetEntity } from "@hk-artificial-intelligence-association/twitter-data-fetch";
import { analyzeTypeOFTweetSimpleParsedResult } from "@hk-artificial-intelligence-association/twitter-data-fetch";
import { analyzeTwitterSimpleParsedResult } from "@hk-artificial-intelligence-association/twitter-data-fetch";
import dotenv from 'dotenv'
async function main() {
dotenv.config()
const cookies = await loginToGetCookies(process.env.TWITTER_ID || 'UserLoginName',process.env.TWITTER_PASSWORD || 'UserPassword',10000)
const listId = '1795638097327358182'
const userId = 'LeeWendao'
const listTweets = await getTwitterListNewlyTweetsWithCookies(listId,cookies)
/**
* 只有12条哦~
*/
const userTweets = await getTwitterUserNewlyTweetsWithCookies(userId,cookies)
/**
* 原始数据比较杂,可以使用解析函数对结果进行简单的解析
* 该解析会保留推文的原始信息数据
*/
const parsedListTweets = listTweets.map(t=>simplyParseTweetEntity(t))
/**
* 可以使用分析函数判断推文的类型:原创,转发 etc.
*/
const analyzedListTweets = parsedListTweets.map(t=>analyzeTypeOFTweetSimpleParsedResult(t))
/**
* 可以使用分析函数提取推文的内容,提取的内容会是形如这样的字符串:
* "@xx 于 某年某月某日 转发了 @yy 于 某年某月某日发布 的推文 \n----\n @yy 的推文内容:略"
*/
const analyzedTweetsContent = parsedListTweets.map(t=>analyzeTwitterSimpleParsedResult(t))
}
main()