首页 > 解决方案 > 如何使用本地机器调度程序每分钟运行一次 python 脚本?

问题描述

我有一个 python 脚本来执行一些 API 调用,并希望它每分钟或每小时运行一次。

我提供了一种解决方案,即 Windows 10 中的任务调度程序。

from urllib.parse import urlencode
import json
import requests

credentials = 'mart.polman@lamudi.co.id', 'lamudionfire'
session = requests.Session()
session.auth = credentials

params = {
    'query': 'type:ticket tags:"send_whatsapp" status<closed',
}

url_search = 'https://propertypro.zendesk.com/api/v2/search.json?' + \
    urlencode(params)
response = session.get(url_search)
if response.status_code != 200:
    print('Status:', response.status_code,
          'Problem with the request. Exiting.')
    exit()

# Print the subject of each ticket in the results
data = response.json()

id_merged = [result['id'] for result in data['results']]
print(type(id_merged))
print(id_merged)

# Join value of list by using comma separated
id_merged_joined = ','.join(map(str, id_merged))
print(id_merged_joined)

# Set the request parameters
body = 'Hai Pak/Bu'


# Package the data in a dictionary matching the expected JSON
data_comment = {"ticket": 
                    {
                        "comment": {"body": body},
                        "remove_tags":["send_whatsapp"]
                    }
                }



# Encode the data to create a JSON payload
payload = json.dumps(data_comment)

print("**Start**")

# Set the request parameters
url_put_comments = 'https://propertypro.zendesk.com/api/v2/tickets/update_many.json?' +\
    'ids=' + id_merged_joined
user = 'mart.polman@lamudi.co.id'
pwd = 'lamudionfire'
headers = {'content-type': 'application/json'}

# Do the HTTP put request
response_request = requests.put(url_put_comments, data=payload,
                                auth=(user, pwd), headers=headers)

# Check for HTTP codes other than 200
if response_request.status_code != 200:
    print('Status:', response.status_code,
          'Problem with the request. Exiting.')
    exit()

    # Report success
    print('Successfully added comment to ticket #{}'.format(id_merged))

我不希望它在我的本地机器上运行,因为它要求我的机器 24 小时待机,这并不理想。

有人可以帮我吗?

标签: pythoncron

解决方案


推荐阅读