|
| 1 | +import tweepy |
| 2 | +import requests |
| 3 | + |
| 4 | +# Function to get tweets from Twitter API |
| 5 | +def get_tweets(api_key, api_secret_key, access_token, access_token_secret, username): |
| 6 | + auth = tweepy.OAuthHandler(api_key, api_secret_key) |
| 7 | + auth.set_access_token(access_token, access_token_secret) |
| 8 | + api = tweepy.API(auth) |
| 9 | + |
| 10 | + tweets = [] |
| 11 | + try: |
| 12 | + for tweet in tweepy.Cursor(api.user_timeline, screen_name=username, tweet_mode="extended").items(10): |
| 13 | + tweets.append(tweet.full_text) |
| 14 | + except tweepy.TweepError as e: |
| 15 | + print(f"Error: {e}") |
| 16 | + |
| 17 | + return tweets |
| 18 | + |
| 19 | +# Function to get weather data from OpenWeatherMap API |
| 20 | +def get_weather(api_key, city): |
| 21 | + url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}" |
| 22 | + response = requests.get(url) |
| 23 | + data = response.json() |
| 24 | + |
| 25 | + if response.status_code == 200: |
| 26 | + return data |
| 27 | + else: |
| 28 | + print(f"Error: {data['message']}") |
| 29 | + return None |
| 30 | + |
| 31 | +# Function to get stock data from Alpha Vantage API |
| 32 | +def get_stock_data(api_key, symbol): |
| 33 | + url = f"https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol={symbol}&apikey={api_key}" |
| 34 | + response = requests.get(url) |
| 35 | + data = response.json() |
| 36 | + |
| 37 | + if "Time Series (Daily)" in data: |
| 38 | + return data["Time Series (Daily)"] |
| 39 | + else: |
| 40 | + print(f"Error: {data['Note']}") |
| 41 | + return None |
| 42 | + |
| 43 | +if __name__ == "__main__": |
| 44 | + # Replace with your Twitter API credentials |
| 45 | + twitter_api_key = "YOUR_TWITTER_API_KEY" |
| 46 | + twitter_api_secret_key = "YOUR_TWITTER_API_SECRET_KEY" |
| 47 | + twitter_access_token = "YOUR_TWITTER_ACCESS_TOKEN" |
| 48 | + twitter_access_token_secret = "YOUR_TWITTER_ACCESS_TOKEN_SECRET" |
| 49 | + twitter_username = "target_username" |
| 50 | + |
| 51 | + # Replace with your OpenWeatherMap API key |
| 52 | + weather_api_key = "YOUR_OPENWEATHERMAP_API_KEY" |
| 53 | + city_name = "New York" |
| 54 | + |
| 55 | + # Replace with your Alpha Vantage API key |
| 56 | + alpha_vantage_api_key = "YOUR_ALPHA_VANTAGE_API_KEY" |
| 57 | + stock_symbol = "AAPL" |
| 58 | + |
| 59 | + # Retrieve data from APIs |
| 60 | + tweets = get_tweets(twitter_api_key, twitter_api_secret_key, twitter_access_token, twitter_access_token_secret, twitter_username) |
| 61 | + weather_data = get_weather(weather_api_key, city_name) |
| 62 | + stock_data = get_stock_data(alpha_vantage_api_key, stock_symbol) |
| 63 | + |
| 64 | + # Print the results |
| 65 | + print("Twitter Tweets:") |
| 66 | + for idx, tweet in enumerate(tweets, start=1): |
| 67 | + print(f"{idx}. {tweet}") |
| 68 | + |
| 69 | + print("\nWeather Data:") |
| 70 | + print(weather_data) |
| 71 | + |
| 72 | + print("\nStock Data:") |
| 73 | + print(stock_data) |
0 commit comments