I created a python twitter bot using tweepy, that tweet only text (relevant part of the script below). currently i’m using tweepy.client because from what i understood only twitter API v2 can post text tweets (free API access, i have no intention paying to twitter).i would like to attach media to tweets from this bot (spesific media to spesific event), and i wanted to know if somehow i can also use tweepy API (i.e API v1.1), so i would be able to do this (from what i understood, the free v1.1 tier allow posting media but doesn’t allow powting text)
this is the relevant part of my script (as the full script is unfourtently really long and doesn’t have a good readability):
client = tweepy.Client(
consumer_key="xxxxxx",
consumer_secret="xxxxxx",
access_token="xxxxxx",
access_token_secret="xxxxxx",
wait_on_rate_limit=True
)
def calculate_event_hash(event):
composite_key = f"{event['type']}-{event['player_name']}-{event['rival_team_name']}"
return hashlib.sha256(composite_key.encode()).hexdigest()
def has_event_been_tweeted(event_hash):
try:
with open(File_name, "r") as file:
return event_hash in file.read()
except FileNotFoundError:
return False
def mark_event_as_tweeted(event_hash):
with open(File_name, "a") as file:
file.write(event_hash + "n")
def tweet_event(event, text):
try:
event_hash = calculate_event_hash(event)
if not has_event_been_tweeted(event_hash):
if not debug_mode:
client.create_tweet(text=text)
logger.tweet("Tweeted: %s",text)
mark_event_as_tweeted(event_hash)
else:
logger.tweet("Tweet not sent (debug mode): %s",text)
else:
logger.tweet("Event has already been tweeted: %s",text)
except Exception as e:
logging.error('Error tweeting %s: %s', text, e, exc_info=True)
def tweet_starting_player(player_name, team_name, rival_team_name, short_name,competition, match_id):
event = {'type': 'starting_player', 'player_name': player_name, 'team_name': team_name, 'rival_team_name': rival_team_name, 'short_name': short_name, 'competition':competition, 'id': match_id}
text = f"{player_name} פותח בהרכב {team_name} למשחק {competition} נגד {rival_team_name}nיאללה {short_name}, תעשה אותנו גאים!"
tweet_event(event,text)
def tweet_not_starting(player_name, team_name, rival_team_name,competition,match_id):
event = {'type': 'not_starting', 'player_name': player_name, 'team_name': team_name, 'rival_team_name': rival_team_name,'competition':competition,'id': match_id}
text = f"לצערנו {player_name} על הספסל של {team_name} למשחק {competition} נגד {rival_team_name}"
tweet_event(event,text)
def tweet_player_event(match_status, event_type, event_time, player_name, team_name, rival_team_name, leader, short_name, current_score, match_id):
event_texts = {
'own_goal': f'סעמק!n{player_name} שם שער עצמי בדקה ה-{event_time} נגד {rival_team_name}',
'goal': f"יופי {short_name}!n{player_name} כובש בדקה ה-{event_time}",
'assist': f"יופי {short_name}!n{player_name} מבשל בדקה ה-{event_time}",
'var': f"סעמק!n{player_name} כובש בדקה ה-{event_time}, אבל ה-VAR פסל את השער",
'red_card': f"מאנייק!nהשופט הרשע מרחיק את {player_name} בדקה ה-{event_time}, והוא יאלץ לראות את שאר המשחק מהיציע",
'sub_in': f"קדימה {short_name}!n{player_name} נכנס למגרש בדקה ה-{event_time} במדי {team_name} נגד {rival_team_name}",
'sub_out': f"חבל שהמאמן אנטישמיn{player_name} מוחלף בדקה ה-{event_time} למשחק נגד {rival_team_name}"
}
text = event_texts.get(event_type, "")
if leader and match_status not in ['הסתיים לאחרונה', 'טרם החל']:
if leader == rival_team_name:
if event_type in ['goal', 'assist','own_goal']:
text += f", {team_name} כעת בפיגור {current_score} נגד {rival_team_name}"
if event_type == 'var':
text += f", התוצאה נשארת {current_score} לטובת {rival_team_name}"
elif leader == team_name:
if event_type in ['goal', 'assist','own_goal']:
text += f", {team_name} כעת מובילה {current_score} על {rival_team_name}!"
if event_type == 'var':
text += f", התוצאה נשארת {current_score} לטובת {team_name}"
elif leader == None and match_status not in ['הסתיים לאחרונה', 'טרם החל']:
if event_type in ['goal', 'assist','own_goal']:
text += f", {team_name} משווה ל{current_score} נגד {rival_team_name}!"
if event_type == 'var':
text += f", התוצאה נשארת שוויון {current_score}"
event = {'type': event_type, 'player_name': player_name, 'event_time': event_time, 'team_name': team_name, 'rival_team_name': rival_team_name,'id': match_id}
tweet_event(event,text)
def tweet_player_stats(player_name, minutes_played, rival_team_name, stats, team_name,current_score, outcome,ranking,match_id):
event = {'type': 'player_stats', 'player_name': player_name, 'team_name': team_name, 'rival_team_name': rival_team_name,'id': match_id}
stats_text = ", ".join([f"{stat['name']} {stat['value']}" if stat['value'] == 'אחד' else f"{stat['value']} {stat['name']}" for stat in stats if stat['name'] != 'דקות'])
outcome_prepositions = {
'מנצחת': 'את',
'מפסידה': 'ל',
'עושה תיקו': 'עם'}
preposition = outcome_prepositions.get(outcome, 'נגד')
if preposition == 'ל':
text = f"{team_name} {outcome} {current_score} {preposition}{rival_team_name}n{player_name} שיחק {minutes_played} דקות"
else:
text = f"{team_name} {outcome} {current_score} {preposition} {rival_team_name}n{player_name} שיחק {minutes_played} דקות"
if stats and stats_text:
text += f" וסיים עם {stats_text}"
if ranking is not None and ranking > 0:
text += f", וציון {ranking} מ-365"
tweet_event(event,text)