Вот мой сценарий:
Код: Выделить всё
# Twitter API credentials
API_KEY = ''
API_SECRET_KEY = ''
BEARER_TOKEN = ''
# Global variables to store access tokens
ACCESS_TOKEN = ''
ACCESS_TOKEN_SECRET = ''
# Create the Twitter API objects
def get_twitter_conn_v1(api_key, api_secret, access_token, access_token_secret) -> tweepy.API:
"""Get Twitter API v1.1 connection"""
auth = tweepy.OAuth1UserHandler(api_key, api_secret)
auth.set_access_token(access_token, access_token_secret)
return tweepy.API(auth)
def get_twitter_conn_v2(bearer_token) -> tweepy.Client:
"""Get Twitter API v2 connection"""
return tweepy.Client(bearer_token=bearer_token)
client_v1 = get_twitter_conn_v1(API_KEY, API_SECRET_KEY, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
client_v2 = get_twitter_conn_v2(BEARER_TOKEN)
# Path to the media file
media_path = 'big-gulps.mp4'
# Upload media using v1.1 API
try:
media = client_v1.media_upload(filename=media_path)
media_id = media.media_id_string
print(f"Media uploaded successfully. Media ID: {media_id}")
# Read the CSV file
csv_file_path = 'RandomItems1.csv' # Ensure the path is correct
with open(csv_file_path, newline='') as csvfile:
reader = csv.DictReader(csvfile)
header = reader.fieldnames # Print header for debugging
print(f"CSV Header: {header}")
random_things = [row[header[0]] for row in reader] # Dynamically read the first column
# Define the tweet template
tweet_template = "{} huh? Alllrightt. Welp, see ya later!"
# Function to post a text-only tweet using v2 API
def post_tweet(item, media_id):
tweet = tweet_template.format(item)
try:
# Post tweet using v2 endpoint
response = client_v2.create_tweet(text=tweet, media_ids=[media_id])
print(f"Tweeted: {tweet}")
except tweepy.TweepyException as e:
print(f"Error: {e}")
# Schedule tweets (every 2.5 hours)
for item in random_things:
post_tweet(item, media_id)
time.sleep(9000) # Sleep for 2.5 hours (2.5 * 3600 seconds)
except tweepy.TweepyException as e:
print(f"Error uploading media: {e}")
Код: Выделить всё
TypeError: Consumer key must be string or bytes, not NoneType
Подробнее здесь: https://stackoverflow.com/questions/787 ... hon-script