-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtweet_rephraser.py
72 lines (51 loc) · 1.95 KB
/
tweet_rephraser.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import re
import tweepy
import openai
import time
from config import TWITTER_API_KEY, TWITTER_API_SECRET, TWITTER_ACCESS_TOKEN, TWITTER_ACCESS_SECRET, OPENAI_API_KEY, LIST_ID, TARGET_ACCOUNT, REPHRASE_OPEN_AI_ENGINE, TRANSLATE_OPEN_AI_ENGINE
# Set up Tweepy and OpenAI API
auth = tweepy.OAuthHandler(TWITTER_API_KEY, TWITTER_API_SECRET)
auth.set_access_token(TWITTER_ACCESS_TOKEN, TWITTER_ACCESS_SECRET)
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
openai.api_key = OPENAI_API_KEY
# Function to rephrase a tweet
def rephrase_tweet(tweet):
prompt = f"Rephrase the following tweet:\n\n{tweet}\n\nRephrased:"
response = openai.Completion.create(
engine=REPHRASE_OPEN_AI_ENGINE,
prompt=prompt,
max_tokens=100,
n=1,
stop=None,
temperature=0.7,
)
return response.choices[0].text.strip()
# Function to translate a tweet to Arabic
def translate_tweet_to_arabic(tweet):
prompt = f"Translate the following English tweet into Arabic without translating URLs and emojis:\n\n{tweet}\n\nTranslation:"
response = openai.Completion.create(
engine=TRANSLATE_OPEN_AI_ENGINE,
prompt=prompt,
max_tokens=100,
n=1,
stop=None,
temperature=0.7,
)
return response.choices[0].text.strip()
# Function to fetch tweets from a Twitter list
def get_tweets_from_list(LIST_ID, count):
return api.list_timeline(LIST_ID, count=count)
# Function to post a tweet
def post_tweet(tweet):
api.update_status(tweet)
# Main function
def main():
tweets = get_tweets_from_list(LIST_ID, 10) # Fetch 10 tweets from the list
for tweet in tweets:
rephrased_tweet = rephrase_tweet(tweet.text)
arabic_tweet = translate_tweet_to_arabic(rephrased_tweet)
post_tweet(f"@{TARGET_ACCOUNT} {arabic_tweet}")
if __name__ == "__main__":
while True:
main()
time.sleep(3600) # Wait for an hour before restarting