设置计时器以每天执行一次API请求

时间:2019-02-06 18:36:25

标签: python python-3.x python-requests

我为伊斯兰祈祷时期编造了我的第一个家庭自动化脚本。所以我要做的是每天显示祷告时间。问题是它不会每天重置。就像每天0:00一样

所以我想知道是否可以在特定时间设置我的请求?还是我应该添加一个重启脚本,例如:

import os, sys

def force_restart_script():
    python = sys.executable
    os.execl(python, python, * sys.argv)

这是我当前的脚本

import json
import requests

# basics
street = "wallstreet"      # fill in your streetname
housenumber = "5050"          # fill in your housenumber
city = "New York"             # fill in your city
country = "Netherlands"     # fill in your country
angle = "15.0,null,15.0"    # fill in angle

#dont touch
location = street +" "+ housenumber + "," + city +","+ country
url = "https://api.aladhan.com/timingsByAddress?address=" + location + "&method=99&methodSettings=" + angle + ".json"
headers = {'content-type': 'application/json'}

r= requests.get(url, headers)
response    = r.content.decode("utf-8")

jsonstring  = json.loads(response)
inner       = jsonstring["data"]

prayer      = inner["timings"]

fajr    = prayer["Fajr"]
dhuhr   = prayer["Dhuhr"]
asr     = prayer["Asr"]
maghrib = prayer["Maghrib"]
isha    = prayer["Isha"]

print(fajr)
print(dhuhr)
print(asr)
print(maghrib)
print(isha)

DOMAIN = 'salat_assistant'

def setup(hass, config):
    hass.states.set('salat.fajr', fajr)
    hass.states.set('salat.dhuhr', dhuhr)
    hass.states.set('salat.asr', asr)
    hass.states.set('salat.maghrib', maghrib)
    hass.states.set('salat.isha', isha)

    return True

1 个答案:

答案 0 :(得分:0)

如果您使用的是Windows,则应查看“任务计划程序”程序。只需单击左下角的Windows图标,然后键入“任务计划程序”以启动它,因为Windows默认提供它。您可以将其设置为在各种时间运行,例如每天或在晚上9:00或登录PC时运行。