因此,我正在尝试制作一个Twitter机器人,用当前天气回复一条消息。我有以下代码:
from requests import get
import json
import time
import os
import tweepy
import schedule
api = os.environ\['api'\]
weather_api = 'https://pro.openweathermap.org/data/2.5/weather?lat=29.973330&lon=-95.687332&appid='+api+'&units=imperial'
one_call_api = 'https://pro.openweathermap.org/data/2.5/onecall?lat=29.973330&lon=-95.687332&appid='+api+'&units=imperial'
twitter_api = os.environ\['twitter_api'\]
twitter_api_secret = os.environ\['twitter_api_secret'\]
twitter_bearer_token = os.environ\['twitter_bearer_token'\]
twitter_access_token = os.environ\['twitter_access_token'\]
twitter_access_token_secret = os.environ\['twitter_access_token_secret'\]
# Authenticate to Twitter
auth = tweepy.OAuthHandler(twitter_api, twitter_api_secret)
auth.set_access_token(twitter_access_token, twitter_access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True)
try:
print("Authenticating to Twitter")
api.verify_credentials()
print("Authentication OK")
except:
print("Error during Twitter authentication")
def degrees_to_cardinal(d):
'''
note: this is highly approximate...
'''
dirs = \[
"N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW",
"WSW", "W", "WNW", "NW", "NNW"
\]
ix = int((d + 11.25) / 22.5)
return dirs\[ix % 16\]
def job():
result = get(one_call_api).json();
degrees = result.get('current').get('wind_deg');
wind_gust = result.get('current').get('wind_gust')
weather_temp = result\['current'\]\["temp"\]
wind_speed = result\["current"\]\["wind_speed"\]
pressure = result\['current'\]\['pressure'\]
feels_like = result\['current'\]\["feels_like"\]
uvi = result.get('current')
uvi = uvi.get('uvi')
current_data = result.get('current')
current_data = current_data.get('weather')
conditions_data = json.dumps(current_data)
conditions_data = conditions_data.replace('"description": ', "")
conditions_data = conditions_data.replace('"', "")
conditions = conditions_data.split(", ")
atmo_pressure = result.get('current').get('pressure')
humidity = result.get('current').get('humidity')
dew_pt = result.get('current').get('dew_point')
wind_direction = degrees_to_cardinal(degrees)
any_alerts = result.get('alerts')
any_alert = json.dumps(any_alerts)
any_alert = any_alert.replace('"event": ', "")
any_alert = any_alert.replace('"', "")
if any_alert != "null":
any_alert = any_alert.split(", ")
else:
any_alert = \[ 'NONE','NONE','NONE','NONE','NONE',
'NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE'\]
message = "Current Conditions: " + str(conditions\[2\].swapcase() + "\\nTemp: " + str(weather_temp) + "°F \\n" + "Feels Like: " + str(feels_like) + "°F \\n" + "\\n Wind Speed: " + str(wind_speed) + " Miles per Hour " +str(wind_direction)+"\\n"+ "Wind Gust: " + str(wind_gust) + " Miles per Hour \\n\\n" + "UV Index: " + str(uvi) + '\\nDew Point: ' + str(dew_pt) + '\\N{DEGREE SIGN}F' + '\\nAtmosperic Pressure: ' + str(atmo_pressure) + " hPa" + '\\nHumidity: ' + str(humidity) + "%" + "\\n\\n Data from openweathermap.org")
api.update_status(message)
stream = tweepy.Stream(twitter_api, twitter_api_secret,twitter_access_token, twitter_access_token_secret)
stream.filter(track=\["@TheBotWeather"\], languages=\["en"\])
但我不知道如何从stream中获得提及。filter(),然后回复提及的内容。有没有办法做到这一点,或者我只是做错了。
我试图找出如何从流中获取数据,但我不知道如何做到这一点。