-
Notifications
You must be signed in to change notification settings - Fork 76
Examples
Tayyab Kharl edited this page Feb 12, 2025
·
2 revisions
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())
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())
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())
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())
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())
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())
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())
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
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())
Twitter has advance search feature using which you can filter out the search results
# assuming `client` is authenticated instance of `Twitter`
search_results = await client.search("github until:2023-05-01 since:2022-05-01")
search_results = await client.search("github min_replies:280 min_faves:280 min_retweets:280")
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