mirror of
https://github.com/mahrtayyab/tweety.git
synced 2026-04-26 10:55:54 +03:00
2
Examples
Tayyab Kharl edited this page 2025-02-12 21:29:36 +05:00
Table of Contents
Login
Interactive Login
import asyncio
from tweety import TwitterAsync
async def main():
client = TwitterAsync("session_name")
await client.start()
# this function will ask for inputs including username , password or any any information required
asyncio.run(main())
Non-Interactive Login
import asyncio
from tweety import TwitterAsync
from tweety.exceptions import ActionRequired
async def main():
client = TwitterAsync("session_name")
try:
await client.sign_in(username, password)
except ActionRequired as action_required:
required_info = input(f"{action_required} : ")
await client.sign_in(username, password, required_info)
asyncio.run(main())
Getting all pages (available pages)
All classes with BaseGeneratorClass as parent will follow this example
import asyncio
from tweety import TwitterAsync
from tweety.filters import SearchFilters
from tweety.exceptions import ActionRequired
async def main():
client = TwitterAsync("session_name")
await client.connect()
async for search_object, tweets in client.iter_search("Bitcoin", pages=-1, filter_=SearchFilters.Latest):
for tweet in tweets:
print(tweet)
pritn(search_object.cursor)
asyncio.run(main())
Inbox functions
Getting All conversations
import asyncio
from tweety import TwitterAsync
async def main():
client = TwitterAsync("session_name")
await client.connect()
for inbox_object, conversations in twitter.iter_inbox(pages=-1):
for conversation in conversations:
print(conversation)
print(inbox_object.cursor)
asyncio.run(main())
Getting Conversation with specific User
import asyncio
from tweety import TwitterAsync
async def main():
client = TwitterAsync("session_name")
await client.connect()
conversation = await twitter.get_conversation("elonmusk")
print(conversation)
asyncio.run(main())
Getting all messages of a conversation
import asyncio
from tweety import TwitterAsync
async def main():
client = TwitterAsync("session_name")
await client.connect()
conversation = await twitter.get_conversation("elonmusk")
for conversation_object, messages in conversation.iter_all_messages():
for message in messages:
print(message)
print(conversation_object.cursor)
asyncio.run(main())
Getting new message
import asyncio
from tweety import TwitterAsync
async def main():
client = TwitterAsync("session_name")
await client.connect()
inbox = await self.client.get_inbox(pages=1)
while True:
new_chats = await inbox.get_new_messages()
for converation in new_chats:
print(converation.messages)
asyncio.run(main())
OR
import asyncio
from tweety import TwitterAsync
from tweety import events
async def main():
client = TwitterAsync("session_name")
await client.connect()
@client.on(events.NewMessageUpdate)
async def newMessage(event):
await event.respond("OKAY")
client.run_until_disconnected()
asyncio.run(main())
Creating Tweet Thread
In order to create a thread you need to reply to previous tweet with batch_compose argument set to True
# client is authenticated instance of `TwitterAsync` class
threads = ["Thread 1", "2 Thread", "Three Thread"]
reply_to = None
for thread in threads:
tweet = await client.create_tweet(thread, batch_compose=True, reply_to=reply_to)
reply_to = tweet.id
Memory Session
If you don't want to create a session on disk, you can create it in memory only too.
from tweety.session import MemorySession
from tweety import TwitterAsync
import asyncio
async def main():
client = TwitterAsync(MemorySession()) # Session will be stored in Memory instead of Disk
await client.load_auth_token(...)
print(client.me)
asyncio.run(main())
Advance Search
Twitter has advance search feature using which you can filter out the search results
Filtering Date
# assuming `client` is authenticated instance of `Twitter`
search_results = await client.search("github until:2023-05-01 since:2022-05-01")
Interactions Filtering
search_results = await client.search("github min_replies:280 min_faves:280 min_retweets:280")
Filtering Users
search_results = await client.search("github (from:kharltayyab OR from:elonmusk) (to:reply_to OR to:reply_to2) (@mention OR @mention3 OR @23)")
More about advance search