Twitter API-v2 라이브러리에서 Twitter 검색
개시하다
Twitter API-v2 라이브러리에서
인스턴스 생성
TwitterApi의 인스턴스 생성
이번에는 v2 스타일로 만들어봤습니다.
(겸사겸사 트위터에 앞으로 v2가 주류가 될 것이라고 발표했다)
const client = new TwitterApi('<YOUR-TWITTER-BEARER-TOKEN>');
// Read+Write level
const rwClient = client.readWrite;
// Read-only level
const roClient = client.readOnly;
트위터 가져오기
example
// Get and delete old rules if needed
const rules = await client.v2.streamRules();
if (rules.data?.length) {
await client.v2.updateStreamRules({
delete: { ids: rules.data.map(rule => rule.id) },
});
}
// Add our rules
await client.v2.updateStreamRules({
add: [{ value: 'JavaScript' }, { value: 'NodeJS' }],
});
const stream = await client.v2.searchStream({
'tweet.fields': ['referenced_tweets', 'author_id'],
expansions: ['referenced_tweets.id'],
});
// Enable auto reconnect
stream.autoReconnect = true;
stream.on(ETwitterStreamEvent.Data, async tweet => {
// Ignore RTs or self-sent tweets
const isARt = tweet.data.referenced_tweets?.some(tweet => tweet.type === 'retweeted') ?? false;
if (isARt || tweet.data.author_id === meAsUser.id_str) {
return;
}
// Reply to tweet
await client.v1.reply('Did you talk about JavaScript? love it!', tweet.data.id);
});
각 요소를 살펴보겠습니다.가져올 규칙 설정
지난번 규칙이 남았으면 삭제하는 처리입니다.
// Get and delete old rules if needed
const rules = await client.v2.streamRules();
if (rules.data?.length) {
await client.v2.updateStreamRules({
delete: { ids: rules.data.map(rule => rule.id) },
});
}
연결
/**
* Return a list of rules currently active on the streaming endpoint, either as a list or individually.
* https://developer.twitter.com/en/docs/twitter-api/tweets/filtered-stream/api-reference/get-tweets-search-stream-rules
*/
streamRules(options?: Partial<StreamingV2GetRulesParams>): Promise<StreamingV2GetRulesResult>;
/**
* Add or delete rules to your stream.
* To create one or more rules, submit an add JSON body with an array of rules and operators.
* Similarly, to delete one or more rules, submit a delete JSON body with an array of list of existing rule IDs.
* https://developer.twitter.com/en/docs/twitter-api/tweets/filtered-stream/api-reference/post-tweets-search-stream-rules
*/
updateStreamRules(options: StreamingV2AddRulesParams, query?: Partial<StreamingV2UpdateRulesQuery>): Promise<StreamingV2UpdateRulesAddResult>;
{
"data": [
{
"id": "1165037377523306497",
"value": "dog has:images",
"tag": "dog pictures"
},
{
"id": "1165037377523306498",
"value": "cat has:images -grumpy"
}
],
"meta": {
"sent": "2019-08-29T01:12:10.729Z"
}
}
연결
검색하려는 Word 추가 처리
// Add our rules
await client.v2.updateStreamRules({
add: [{ value: 'JavaScript' }, { value: 'NodeJS' }],
});
JavaScript
또는 NodeJS
의 트위터설정된 Word로 읽어들이기
공식 example
JavaScript
또는 NodeJS
트윗const stream = await client.v2.searchStream({
'tweet.fields': ['referenced_tweets', 'author_id'],
expansions: ['referenced_tweets.id'],
});
// Enable auto reconnect
stream.autoReconnect = true;
stream.on(ETwitterStreamEvent.Data, async tweet => {
// Ignore RTs or self-sent tweets
const isARt = tweet.data.referenced_tweets?.some(tweet => tweet.type === 'retweeted') ?? false;
if (isARt || tweet.data.author_id === meAsUser.id_str) {
return;
}
검색 스트림 검색 방법
연결
트위터에 회신한 처리는 이번 취지와 맞지 않아 생략했다
실행 결과
> {
> data: {
> author_id: '84208574',
> id: '1517746807451037696',
> text: 'Hello guys,\n' +
> 'Does anyone know where it is a company that has whatsapp api services?\n' +
> 'I only need to integrate with my backend (node + express) and use it as an outgoing message.\n' +
> 'I preferred pay as go (per message)\n' +
> 'Have a green check wa account\n' +
> '\n' +
> '#Nodejs #whatsappAPI #WebDevelopment'
> },
> matching_rules: [ { id: '1517746830268067840', tag: '' } ]
> }
> {
> data: {
> author_id: '731117954188775424',
> id: '1517746817114554368',
> text: 'Siempre habra un color para cada momento del dia, ahorita mismo estoy muy #5df497\n' +
> '\n' +
> ' #bot #nodejs #random #color #face #emoji #programacion #meme #api https://t.co/Zioq4MbP3B'
> },
> matching_rules: [ { id: '1517746830268067840', tag: '' } ]
> }
> {
> data: {
> author_id: '1499252873103695873',
> id: '1517746858780626945',
> text: 'うーいんだよ~\n' +
> '\n' +
> 'これはオススメだね~\n' +
> 'だってプログラムの書き方や考え方が、 うーいんの中の人と同じ思想だからね~\n' +
> '\n' +
> 'スラスラ読める JavaScript ふりがなプ ログラミング (ふりがなプログラミングシリーズ) https://t.co/qc0ljSyx80\n' +
> '\n' +
> '#プログラミング勉強中'
> },
> matching_rules: [ { id: '1517746830268067841', tag: '' } ]
> }
> {
> data: {
> author_id: '69941978',
> id: '1517746856687980544',
> referenced_tweets: [ [Object] ],
> text: "@_gulam_anas_ Thanks Anas for the feedback. Yeah I have considered people's feedbacks from the similar post I made last month. Based on those, I have added/rectified\n" +
> '\n' +
> '> Other Skills\n' +
> '> Practice\n' +
> '> Frequently Asked Questions\n' +
> '\n' +
> 'I also added links to HTML/CSS and JavaScript individual roadmaps.'
> },
> includes: { tweets: [ [Object] ] },
> matching_rules: [ { id: '1517746830268067841', tag: '' } ]
> }
> {
> data: {
> author_id: '1401391924754325512',
> id: '1517746862173925377',
> text: 'UDP is better in the COVID era since it avoids unnecessary handshakes.\n' +
> '\n' +
> '#programming #programmingjoke #programminghumor #Python #javascript #Java'
> },
> matching_rules: [ { id: '1517746830268067841', tag: '' } ]
> }
> {
> data: {
> author_id: '1078992647933448192',
> id: '1517746892800544769',
> text: 'Change file name from backend to server https://t.co/NK0LynUzuM #github #JavaScript #SCSS #HTML #CSS'
> },
> matching_rules: [ { id: '1517746830268067841', tag: '' } ]
> }
> {
> data: {
> author_id: '950931957860286464',
> id: '1517746905190649856',
> text: '【人気!】\n' +
> 'カテゴリー: プログラミング\n' +
> '「ゲームを作りながら楽しく学べるHTML5+CSS+JavaScriptプログラミング[改訂版] (Future Coders(NextPublishing))」\n' +
> '44%OFF / 821円\n' +
> 'https://t.co/ACHcL4ZSGa'
> },
> matching_rules: [ { id: '1517746830268067841', tag: '' } ]
> }
> {
> data: {
> author_id: '1163939968559058944',
> id: '1517746926191525888',
> referenced_tweets: [ [Object] ],
> text: '@sourav_code Well explained 👌'
> },
> includes: { tweets: [ [Object] ] },
> matching_rules: [ { id: '1517746830268067841', tag: '' } ]
> }
> {
> data: {
> author_id: '13821792',
> id: '1517746998015033344',
> referenced_tweets: [ [Object] ],
> text: 'Javaが出てきた時はOSを代替すると 言われて期待されたけど、アップレットとかまとも に動かなくてサーバーサイドにピボットした。\n' +
> 'ネスケのJavascriptは最初バグだらけで 使い物にならなくて、10年くらいたってやっとまと もに使えるようになった。\n' +
> '技術が成熟するには時間がかかるね。 https://t.co/AY92UoXZqd'
> },
> includes: { tweets: [ [Object] ] },
> matching_rules: [ { id: '1517746830268067841', tag: '' } ]
> }
! functions: Your function timed out after ~60s. To configure this timeout, see
https://firebase.google.com/docs/functions/manage-functions#set_timeout_and_memory_allocation.
Cloud Function for Firebase에서 실험한 결과, 얻었지만 중간에 시간이 초과되었습니다.
문자의 대문자와 소문자를 검색해도 개의치 않는 모습
끝말
참고 자료
Reference
이 문제에 관하여(Twitter API-v2 라이브러리에서 Twitter 검색), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://zenn.dev/temple_c_tech/articles/search-tweet-on-nodejs텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)