Skip to content

Commit

Permalink
Multiple attempts when data fetching fails
Browse files Browse the repository at this point in the history
  • Loading branch information
ARC-MX committed Dec 19, 2024
1 parent f53b28a commit f0a6d5d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 36 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

**注意** 有很多新手都在提交验证码不能识别的相关issue,特在此统一说明:国网每天有登录限制,每天只能登录有限的几次,超过限制验证码识别成功也不会登录成功。因此,诸如[issue47](https://github.com/ARC-MX/sgcc_electricity_new/issues/47),[issue50](https://github.com/ARC-MX/sgcc_electricity_new/issues/50),[issue29](https://github.com/ARC-MX/sgcc_electricity_new/issues/29)这些都是这个问题,以后就不做回复了。

最近issue太多实在是回复不过来了,特此添加QQ交流群
[关于创建QQ付费群的说明](https://github.com/ARC-MX/sgcc_electricity_new/issues/78)

### 支付宝&微信 打赏码

<p align="center">
Expand Down Expand Up @@ -44,9 +47,7 @@

得到如下结果:

<p align="center">
<img src="assets/database.png" alt="mini-graph-card" width="400">
</p>
## 一、适用范围

1. 适用于除南方电网覆盖省份外的用户。即除广东、广西、云南、贵州、海南等省份的用户外,均可使用本应用获取电力、电费数据。
Expand Down Expand Up @@ -413,8 +414,7 @@ TO-DO
## 入群方式
通过为项目点star并微信打赏备注QQ名或QQ号等信息,入群会审核这些信息
<p align="center">
<img src="assets/QQ_group.jpg" width=200 style="margin-right: 70px";/>
</p>
## 再次说明,希望大家通过认真看文档和浏览历史issue解决问题,毕竟收费群不是开源项目的本意。
2 changes: 1 addition & 1 deletion scripts/data_fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def __init__(self, username: str, password: str):
logging.info("enable_database_storage is false, we will not store the data to the database.")

self.DRIVER_IMPLICITY_WAIT_TIME = int(os.getenv("DRIVER_IMPLICITY_WAIT_TIME", 60))
self.RETRY_TIMES_LIMIT = int(os.getenv("RETRY_TIMES_LIMIT", 20))
self.RETRY_TIMES_LIMIT = int(os.getenv("RETRY_TIMES_LIMIT", 5))
self.LOGIN_EXPECTED_TIME = int(os.getenv("LOGIN_EXPECTED_TIME", 10))
self.RETRY_WAIT_TIME_OFFSET_UNIT = int(os.getenv("RETRY_WAIT_TIME_OFFSET_UNIT", 10))
self.IGNORE_USER_ID = os.getenv("IGNORE_USER_ID", "xxxxx,xxxxx").split(",")
Expand Down
64 changes: 33 additions & 31 deletions scripts/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import os
import sys
import time
import traceback
from datetime import datetime,timedelta

import dotenv
Expand All @@ -23,6 +22,7 @@ def main():
global BALANCE
global PUSHPLUS_TOKEN
global RECHARGE_NOTIFY
global RETRY_TIMES_LIMIT
try:
PHONE_NUMBER = os.getenv("PHONE_NUMBER")
PASSWORD = os.getenv("PASSWORD")
Expand All @@ -34,6 +34,7 @@ def main():
BALANCE = float(os.getenv("BALANCE", 10.0))
PUSHPLUS_TOKEN = os.getenv("PUSHPLUS_TOKEN").split(",")
RECHARGE_NOTIFY = os.getenv("RECHARGE_NOTIFY", "false").lower() == "true"
RETRY_TIMES_LIMIT = int(os.getenv("RETRY_TIMES_LIMIT", 5))
except Exception as e:
logging.error(f"Failing to read the .env file, the program will exit with an error message: {e}.")
sys.exit()
Expand All @@ -57,36 +58,37 @@ def main():


def run_task(data_fetcher: DataFetcher, sensor_updator: SensorUpdator):
try:
user_id_list, balance_list, last_daily_date_list, last_daily_usage_list, yearly_charge_list, yearly_usage_list, month_list, month_usage_list, month_charge_list = data_fetcher.fetch()
# user_id_list, balance_list, last_daily_date_list, last_daily_usage_list, yearly_charge_list, yearly_usage_list, month_list, month_usage_list, month_charge_list = ['123456'],[58.1],['2024-05-12'],[3.0],['239.1'],['533'],['2024-04-01-2024-04-30'],['118'],['52.93']
for i in range(0, len(user_id_list)):
profix = f"_{user_id_list[i]}" if len(user_id_list) > 1 else ""
if balance_list[i] is not None:
sensor_updator.update(BALANCE_SENSOR_NAME + profix, None, balance_list[i], BALANCE_UNIT)
logging.info(f"Check the electricity bill balance. When the balance is less than {BALANCE} CNY, the notification will be sent = {RECHARGE_NOTIFY}")
if balance_list[i] < BALANCE and RECHARGE_NOTIFY:
for token in PUSHPLUS_TOKEN:
title= '电费余额不足提醒'
content =f'您用户号{user_id_list[i]}的当前电费余额为:{balance_list[i]}元,请及时充值。'
url = 'http://www.pushplus.plus/send?token='+token+'&title='+title+'&content='+content
requests.get(url)
logging.info(f'The current balance of user id {user_id_list[i]} is {balance_list[i]} CNY less than {BALANCE}CNY, notice has been sent, please pay attention to check and recharge.')
if last_daily_usage_list[i] is not None:
sensor_updator.update(DAILY_USAGE_SENSOR_NAME + profix, last_daily_date_list[i], last_daily_usage_list[i], month=False)
if yearly_usage_list[i] is not None:
sensor_updator.update(YEARLY_USAGE_SENSOR_NAME + profix, None, yearly_usage_list[i], month=False)
if yearly_charge_list[i] is not None:
sensor_updator.update(YEARLY_CHARGE_SENSOR_NAME + profix, None, yearly_charge_list[i], month=False)
if month_charge_list[i] is not None:
sensor_updator.update(MONTH_CHARGE_SENSOR_NAME + profix, month_list[i], month_charge_list[i], month=True)
if month_usage_list[i] is not None:
sensor_updator.update(MONTH_USAGE_SENSOR_NAME + profix, month_list[i], month_usage_list[i], month=True)
logging.info("state-refresh task run successfully!")
except Exception as e:
logging.error(f"state-refresh task failed, reason is {e}")
traceback.print_exc()

for retry_times in range(1, RETRY_TIMES_LIMIT + 1):
try:
user_id_list, balance_list, last_daily_date_list, last_daily_usage_list, yearly_charge_list, yearly_usage_list, month_list, month_usage_list, month_charge_list = data_fetcher.fetch()
# user_id_list, balance_list, last_daily_date_list, last_daily_usage_list, yearly_charge_list, yearly_usage_list, month_list, month_usage_list, month_charge_list = ['123456'],[58.1],['2024-05-12'],[3.0],['239.1'],['533'],['2024-04-01-2024-04-30'],['118'],['52.93']
for i in range(0, len(user_id_list)):
profix = f"_{user_id_list[i]}" if len(user_id_list) > 1 else ""
if balance_list[i] is not None:
sensor_updator.update(BALANCE_SENSOR_NAME + profix, None, balance_list[i], BALANCE_UNIT)
logging.info(f"Check the electricity bill balance. When the balance is less than {BALANCE} CNY, the notification will be sent = {RECHARGE_NOTIFY}")
if balance_list[i] < BALANCE and RECHARGE_NOTIFY:
for token in PUSHPLUS_TOKEN:
title= '电费余额不足提醒'
content =f'您用户号{user_id_list[i]}的当前电费余额为:{balance_list[i]}元,请及时充值。'
url = 'http://www.pushplus.plus/send?token='+token+'&title='+title+'&content='+content
requests.get(url)
logging.info(f'The current balance of user id {user_id_list[i]} is {balance_list[i]} CNY less than {BALANCE}CNY, notice has been sent, please pay attention to check and recharge.')
if last_daily_usage_list[i] is not None:
sensor_updator.update(DAILY_USAGE_SENSOR_NAME + profix, last_daily_date_list[i], last_daily_usage_list[i], month=False)
if yearly_usage_list[i] is not None:
sensor_updator.update(YEARLY_USAGE_SENSOR_NAME + profix, None, yearly_usage_list[i], month=False)
if yearly_charge_list[i] is not None:
sensor_updator.update(YEARLY_CHARGE_SENSOR_NAME + profix, None, yearly_charge_list[i], month=False)
if month_charge_list[i] is not None:
sensor_updator.update(MONTH_CHARGE_SENSOR_NAME + profix, month_list[i], month_charge_list[i], month=True)
if month_usage_list[i] is not None:
sensor_updator.update(MONTH_USAGE_SENSOR_NAME + profix, month_list[i], month_usage_list[i], month=True)
logging.info("state-refresh task run successfully!")
return
except Exception as e:
logging.error(f"state-refresh task failed, reason is [{e}], {RETRY_TIMES_LIMIT - retry_times} retry times left.")
continue

def logger_init(level: str):
logger = logging.getLogger()
Expand Down

0 comments on commit f0a6d5d

Please sign in to comment.