Skip to content

Instantly share code, notes, and snippets.

@brickgao
Last active October 21, 2016 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brickgao/58dba5e2174b5afd7a1fea182bc644d6 to your computer and use it in GitHub Desktop.
Save brickgao/58dba5e2174b5afd7a1fea182bc644d6 to your computer and use it in GitHub Desktop.
Simple script to remind me to pay the electricity bill
#!/usr/bin/env python3
# The MIT License (MIT)
# Copyright (c) <2016> <brickgao>
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import json
import requests
# Your domain in the settings of mailgun
DOMAIN = ""
# Your API key in the settings of mailgun
API_KEY = ""
ELECTRICITY_LIMIT = 50
# Send to who
MAIL_TO_LIST = []
# Some room description
MY_ROOM = ["文荟", "2号楼", "3层", "1339"]
class FetchDataException(Exception):
pass
def valid_data(response):
if "系统异常" in response.text:
raise FetchDataException("Couldn't use API to fetch data")
def get_electricity():
session = requests.session()
request_meta_list = [
("https://ws.arraypay.cn/tulando-api/web/elec/getCampus", None),
("https://ws.arraypay.cn/tulando-api/web/elec/getLoudong", "campusCode"),
("https://ws.arraypay.cn/tulando-api/web/elec/getLouceng", "buildingCode"),
("https://ws.arraypay.cn/tulando-api/web/elec/getFangjian", "storeyCode"),
]
pre_result = None
for room_description, (request_url, payload_key) in zip(MY_ROOM, request_meta_list):
payload = {}
if payload_key:
payload[payload_key] = pre_result
response = session.post(request_url, payload)
valid_data(response)
data = json.loads(response.json()["roomInfolist"])
pre_result = None
for _data in data:
if room_description in _data["text"]:
pre_result = _data["value"]
break
if not pre_result:
raise FetchDataException("Couldn't use API to fetch data")
electricity_reqeust_url = "https://ws.arraypay.cn/tulando-api/web/elec/getDump"
payload = {"roomCode": pre_result}
response = session.post(electricity_reqeust_url, data=payload)
valid_data(response)
data = response.json()
return data["num"]
def send_email(is_api_broken, electricity=0):
base_api_url = "https://api.mailgun.net/v3/{0}/messages".format(DOMAIN)
mail_from = "Electricity Bot <electricity_bot@{0}>".format(DOMAIN)
subject = "电费提醒"
text = "电费已经小于设定值 {0} 度,还剩 {1} 度,请尽快缴费。".format(
ELECTRICITY_LIMIT, electricity
)
if is_api_broken:
subject = "API 故障提醒"
text = "API 服务器跪了,请排查异常。"
for mail_to in MAIL_TO_LIST:
requests.post(
base_api_url,
auth=("api", API_KEY),
data={
"from": mail_from,
"to": [mail_to],
"subject": subject,
"text": text
}
)
def main_cron_job():
try:
data = get_electricity()
if data < ELECTRICITY_LIMIT:
send_email(False, electricity=data)
except FetchDataException:
send_email(True)
if __name__ == "__main__":
main_cron_job()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment