Ich kann nicht überprüfen, ob das auth_token abgelaufen ist oder nicht.
Hier ist der Codeausschnitt als Referenz:
Code: Select all
from twikit import Client, TooManyRequests
import time
from datetime import datetime
import json
import csv
from configparser import ConfigParser
from random import randint
import asyncio
import tracemalloc
import os
import jwt
import tracemalloc
tracemalloc.start()
# Login credentials
config = ConfigParser()
config.read('config.ini')
username = config['X']['username']
password = config['X']['password']
email = config['X']['email']
# Create a client
client = Client(language='en-US')
# Define the file path
cookies_file_path = 'cookies.json'
async def login_client():
await client.login(auth_info_1=username, auth_info_2=email, password=password)
if not os.path.exists(cookies_file_path):
# Function to login and generate new token
asyncio.run(login_client())
client.save_cookies(cookies_file_path)
else:
# Check if the auth_token is valid or expired
token = jwt.decode(client.auth_token, verify=False)['exp']
current_time = int(datetime.now().timestamp())
if token < current_time:
# Generate a new token
asyncio.run(login_client())
client.save_cookies(cookies_file_path)
else:
# Load existing cookies
client.load_cookies(cookies_file_path)
# Get tweets
MINIMUM_TWEETS = 10
QUERY = 'Nvidia stock' #input("Enter the query: ")
tweet_data = [] # [tweet_count, tweet.user.name, tweet.text, tweet.created_at, tweet.updated_at, tweet.retweet_count, tweet.favorite_count]
global tweet_count
tweet_count = 0
async def get_tweets():
tweets = await client.search_tweet(query=QUERY, product='Top', count=MINIMUM_TWEETS)
global tweet_count
for tweet in tweets:
tweet_count += 1
tweet_data = [tweet_count, tweet.user.name, tweet.text, tweet.created_at, tweet.retweet_count, tweet.favorite_count]
print(tweet_data)
# with open('tweets.csv', 'a', newline='') as file:
# writer = csv.writer(file)
# writer.writerow(tweet_data)
asyncio.run(get_tweets())
print(f"Total tweets: {tweet_count}")
Ich habe versucht, die PyJwt-Python-Bibliothek zu verwenden, um das auth_token zu dekodieren und das Attribut „exp“ abrufen, es benötigt jedoch einen geheimen Schlüssel. Hier stecke ich gerade fest. Wie erhalte ich den Secret_Key? Ich kann nicht herausfinden, wo ich es bekommen kann. Ich meine, ich verwende Twikit, also speichere ich keinen geheimen Schlüssel aktiv und verwende keine Anfragen, um API-Aufrufe auszuführen.