Spaces:
Sleeping
Sleeping
import requests | |
import gradio as gr | |
import os | |
import json | |
import tweepy | |
bearer_token=os.environ["BEARER_KEY"] | |
consumer_key = os.environ["CONSUMER_KEY"] | |
consumer_secret = os.environ["CONSUMER_SECRET"] | |
access_token = os.environ["ACCESS_TOKEN"] | |
access_token_secret = os.environ["ACCESS_TOKEN_SECRET"] | |
client = tweepy.Client( | |
consumer_key=consumer_key, consumer_secret=consumer_secret, | |
access_token=access_token, access_token_secret=access_token_secret | |
) | |
def get_twitter_user_followers(user_id): | |
twitter_api_url = f"https://api.twitter.com/2/users/{user_id}" | |
user_ids = [2244994945, 6253282] | |
print(user_ids) | |
response = client.get_users(ids=user_ids, user_fields=["profile_image_url"]) | |
print(response) | |
print(response.status_code) | |
print(response.text) | |
print(response.json()) | |
for user in response.data: | |
print(user.username, user.profile_image_url) | |
if response.status_code == 200: | |
data = response.json() | |
if "data" in data: | |
return data["data"]["public_metrics"]["following_count"] | |
else: | |
raise Exception("Unable to retrieve user data") | |
else: | |
raise Exception("Failed to fetch user data") | |
def get_followers_counts(user_ids): | |
followers_counts = {} | |
for user_id in user_ids: | |
try: | |
followers_count = get_twitter_user_followers(user_id) | |
followers_counts[user_id] = followers_count | |
except Exception as e: | |
followers_counts[user_id] = str(e) | |
return followers_counts | |
def predict(user_ids_json): | |
user_ids = json.loads(user_ids_json) | |
followers_counts = get_followers_counts(user_ids) | |
return followers_counts | |
iface = gr.Interface(fn=predict, inputs="text", outputs="text") | |
iface.launch() |