From f056257e01d9d91d206fd0b16132d4e4aa37b502 Mon Sep 17 00:00:00 2001 From: Pratik <59784880+nexuspy@users.noreply.github.com> Date: Mon, 1 Jan 2024 20:08:49 +0530 Subject: [PATCH] Created TwitterWeatherBot.py Tweepy library for Twitter API interaction and OpenWeatherMap API for weather information --- TwitterWeatherBot.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 TwitterWeatherBot.py diff --git a/TwitterWeatherBot.py b/TwitterWeatherBot.py new file mode 100644 index 0000000..9e3090f --- /dev/null +++ b/TwitterWeatherBot.py @@ -0,0 +1,44 @@ +import tweepy +import requests +import json + +# Twitter API credentials +consumer_key = 'YourTwitterConsumerKey' +consumer_secret = 'YourTwitterConsumerSecret' +access_token = 'YourTwitterAccessToken' +access_token_secret = 'YourTwitterAccessTokenSecret' + +# OpenWeatherMap API key +openweathermap_api_key = 'YourOpenWeatherMapApiKey' + +# Twitter authentication +auth = tweepy.OAuthHandler(consumer_key, consumer_secret) +auth.set_access_token(access_token, access_token_secret) +api = tweepy.API(auth) + +# Function to get weather information +def get_weather(city): + url = f'https://api.openweathermap.org/data/2.5/weather?q={city}&appid={openweathermap_api_key}' + response = requests.get(url) + weather_data = json.loads(response.text) + + if response.status_code == 200: + return weather_data['weather'][0]['description'] + else: + return None + +# Function to tweet weather information +def tweet_weather(city): + weather = get_weather(city) + + if weather: + tweet = f'The weather in {city} is {weather}! ☀️🌧️❄️' + api.update_status(status=tweet) + print(f'Tweet posted: {tweet}') + else: + print('Failed to retrieve weather information.') + +if __name__ == "__main__": + # Replace 'CityName' with the desired city + city_name = 'CityName' + tweet_weather(city_name)