-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathtwiiter_x_streamlit_app.py
45 lines (31 loc) · 1.25 KB
/
twiiter_x_streamlit_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import streamlit as st
import tweepy
def main():
API_KEY = ""
API_KEY_SECRET = ""
ACCESS_TOKEN = ""
ACCESS_TOKEN_SECRET = ""
auth = tweepy.OAuthHandler(API_KEY, API_KEY_SECRET)
client = tweepy.Client(consumer_key=API_KEY, consumer_secret=API_KEY_SECRET,
access_token=ACCESS_TOKEN, access_token_secret=ACCESS_TOKEN_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)
with st.form('Twitter Bot'):
tweet_text = st.text_input('Put in tweet text')
tweet_media = st.file_uploader(
'Upload tweet media', type=['png', 'jpg', 'mp4'])
send_tweet_btn = st.form_submit_button('Send Tweet')
if send_tweet_btn:
if tweet_media:
with open(f'uploads/{tweet_media.name}', 'wb') as f:
f.write(tweet_media.getbuffer())
media = api.media_upload(f'uploads/{tweet_media.name}')
media_id = media.media_id
response = client.create_tweet(
text=tweet_text, media_ids=[media_id])
else:
response = client.create_tweet(text=tweet_text)
st.success("Tweet created successfully")
st.write(response)
if __name__ == '__main__':
main()