私は、日の出と日没の時刻を知らせる Facebook メッセージを送信するスクリプトを書いています。時刻は午前 10 時に受信者のリストに送信されます。タイミングを把握する方法は次のとおりです。スクリプトを実行すると時間を取得し、Web ページから日の出/日の入りの時間を取得し、最も早い時間 (日の出としましょう) までの時間を計算し、その時間だけ睡眠をとります。メッセージを送信し、日没までスリープして別のメッセージを送信するなどです。スクリプトは最初のメッセージでは機能しますが、2 番目のメッセージでは機能しません。最初のメッセージを送信した後、2 番目のメッセージまでスリープする時間を表示し、常に正しい時間ですが、そのスリープから復帰していないようです。ここで何が起こっているかについてのアイデアはありますか/タスクを達成するためのより良い方法はありますか?
from fbchat import Client
import sunset2
import time
email, password = [********, ********]
client = Client(email, password)
receivers = [receiver1 , receiver2, receiver3, receiver4] # Fb friends names
while True:
# Check out the get_msg() function below
# Get the appropriate message and send it to all receivers
msg = sunset2.get_msg(url)
for receiver in receivers:
client.send(Message(text = msg), thread_id = receiver,
thread_type=ThreadType.USER)
time.sleep(60) # sleep for a minute so that you don't send more than one message to the users
def get_msg(url):
# Calculate the time to the sunrise and sunset, pick the smallest and return a message accordingly
# get sunrise and sunset times from another function
sunrise, sunset = get_sunrise_sunset(url)
now = datetime.datetime.now()
t_now = now.strftime("%I:%M %p")
until_sunset = diff(t_now, sunset) # get difference in seconds
until_10 = diff(t_now, '12:00 PM') # get difference in seconds
wait_time = min(until_sunset, until_10)
# for debug
print('{}:{} left!'.format(wait_time //3600, wait_time //60%60))
time.sleep(rem - 20) # 20 seconds earlier because sending the messages takes about 20 seconds
if until_sunset < until_10:
return 'It's sunset!!'
else:
return f'Sunset today: {sunset}\nSunrise today: {sunrise}'