Skip to content

Instantly share code, notes, and snippets.

@Mariven
Created November 25, 2024 17:00
Show Gist options
  • Select an option

  • Save Mariven/fbaa9be87ea7d0e2ca9de7f471697685 to your computer and use it in GitHub Desktop.

Select an option

Save Mariven/fbaa9be87ea7d0e2ca9de7f471697685 to your computer and use it in GitHub Desktop.
const apiHost = "twitter-api45.p.rapidapi.com";
const apiKey = "your_rapidapi_api_key";
const searchTypes = ["Top", "Latest", "Media", "People", "Lists"];
async function get_results(user, keywords, search_type=null, cursor=null, max_pages = 5) {
if (max_pages === 0) {
return;
}
const type = search_type || "Latest";
const query = encodeURI(`from:${user} ${keywords}`);
const with_cursor = cursor ? `&cursor=${cursor}` : "";
const with_type = searchTypes.includes(type) ? `&search_type=${type}` : "";
let url = `https://${apiHost}/search.php?query=${query}` + with_cursor + with_type;
const options = {
method: 'GET',
headers: {
'x-rapidapi-key': apiKey,
'x-rapidapi-host': apiHost
} };
const res = await fetch(url, options).then(x => x.json());
if (!(res?.timeline) || res.timeline.length === 0) {
return;
}
const lines = res?.timeline.map(t => {
const time_parts = t.created_at.split(" ");
return `<p>(<a href="https://x.com/${t.screen_name}/status/${t.tweet_id}">@${t.screen_name}, ${time_parts[1]} ${time_parts[2]} ${time_parts[3]}</a>, ${t.favorites}L/${t.retweets}R) ${t.text}</p>`;
});
lines.forEach(line => console.log(line));
if (res?.next_cursor) {
await get_results(user, keywords, type, res.next_cursor, max_pages - 1);
}
}
const search = async (user, keywords) => {await get_results(user, keywords, "Latest", null, 5);}
await search("psychiel", "elephant");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment