Skip to content

Commit

Permalink
image add new label
Browse files Browse the repository at this point in the history
  • Loading branch information
ARC-MX committed Jan 4, 2025
1 parent 8dcc9a4 commit 2dbd250
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 38 deletions.
2 changes: 1 addition & 1 deletion example.env
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ DATA_RETENTION_DAYS=7

## 余额提醒
# 是否缴费提醒
RECHARGE_NOTIFY=Flase
RECHARGE_NOTIFY=False
# 余额
BALANCE=5.0
# pushplus token 如果有多个就用","分隔,","之间不要有空格
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ Pillow==10.1.0
undetected_chromedriver==3.5.4
onnxruntime==1.18.1
numpy==1.26.2
python-dotenv
python-dateutil
# python-dotenv
# python-dateutil
34 changes: 12 additions & 22 deletions scripts/data_fetcher.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from ast import Try
import logging
import os
import re
Expand All @@ -7,9 +6,6 @@

import random
import base64
import json
import requests
import dotenv
import sqlite3
import undetected_chromedriver as uc
from datetime import datetime
Expand Down Expand Up @@ -86,7 +82,8 @@ def get_transparency_location(image):
class DataFetcher:

def __init__(self, username: str, password: str):
dotenv.load_dotenv()
if 'PYTHON_IN_DOCKER' not in os.environ:
dotenv.load_dotenv(verbose=True)
self._username = username
self._password = password
self.onnx = ONNX("./captcha.onnx")
Expand Down Expand Up @@ -142,16 +139,6 @@ def _sliding_track(self, driver, distance):# 机器模拟人工滑动轨迹
# time.sleep(0.2)
ActionChains(driver).release().perform()

def base64_api(self, b64, typeid=33):
data = {"username": self._tujian_uname, "password": self._tujian_passwd, "typeid": typeid, "image": b64}
result = json.loads(requests.post("http://api.ttshitu.com/predict", json=data).text)
if result['success']:
return result["data"]["result"]
else:
#!!!!!!!注意:返回 人工不足等 错误情况 请加逻辑处理防止脚本卡死 继续重新 识别
return result["message"]
return ""

def connect_user_db(self, user_id):
"""创建数据库集合,db_name = electricity_daily_usage_{user_id}
:param user_id: 用户ID"""
Expand Down Expand Up @@ -269,7 +256,7 @@ def _login(self, driver, phone_code = False):
self._click_button(driver, By.XPATH, '//*[@id="login_box"]/div[1]/div[1]/div[2]/span')
#get canvas image
background_JS = 'return document.getElementById("slideVerify").childNodes[0].toDataURL("image/png");'
targe_JS = 'return document.getElementsByClassName("slide-verify-block")[0].toDataURL("image/png");'
# targe_JS = 'return document.getElementsByClassName("slide-verify-block")[0].toDataURL("image/png");'
# get base64 image data
im_info = driver.execute_script(background_JS)
background = im_info.split(',')[1]
Expand Down Expand Up @@ -317,11 +304,13 @@ def fetch(self):
logging.info("login successed !")
else:
logging.info("login unsuccessed !")
raise Exception("login unsuccessed")
else:
if self._login(driver):
logging.info("login successed !")
else:
logging.info("login unsuccessed !")
raise Exception("login unsuccessed")
except Exception as e:
logging.error(
f"Webdriver quit abnormly, reason: {e}. {self.RETRY_TIMES_LIMIT} retry times left.")
Expand Down Expand Up @@ -399,19 +388,22 @@ def _get_all_data(self, driver, user_id, userid_index):
logging.info(
f"Get year power charge for {user_id} successfully, yealrly charge is {yearly_charge} CNY")

# 按月获取数据
month, month_usage, month_charge = self._get_month_usage(driver)

# get yesterday usage
last_daily_date, last_daily_usage = self._get_yesterday_usage(driver)

if month is None:
logging.error(f"Get month power usage for {user_id} failed, pass")

# 新增储存用电量
if self.enable_database_storage:
# 将数据存储到数据库
logging.info("enable_database_storage is true, we will store the data to the database.")
# 按天获取数据 7天/30天
date, usages = self._get_daily_usage_data(driver)
# 按月获取数据
month, month_usage, month_charge = self._get_month_usage(driver)
if month is None:
logging.error(f"Get month power usage for {user_id} failed, pass")
self._save_user_data(user_id, balance, last_daily_date, last_daily_usage, date, usages, month, month_usage, month_charge, yearly_charge, yearly_usage)
else:
logging.info("enable_database_storage is false, we will not store the data to the database.")

Expand All @@ -420,8 +412,6 @@ def _get_all_data(self, driver, user_id, userid_index):
else:
logging.info(
f"Get daily power consumption for {user_id} successfully, , {last_daily_date} usage is {last_daily_usage} kwh.")

self._save_user_data(user_id, balance, last_daily_date, last_daily_usage, date, usages, month, month_usage, month_charge, yearly_charge, yearly_usage)

if month_charge:
month_charge = month_charge[-1]
Expand Down
11 changes: 4 additions & 7 deletions scripts/main.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import logging
import logging.config
import requests
import os
import sys
import time
from datetime import datetime,timedelta

import dotenv
import schedule

from datetime import datetime,timedelta
from const import *
from data_fetcher import DataFetcher


def main():
# 读取 .env 文件
dotenv.load_dotenv(verbose=True)
if 'PYTHON_IN_DOCKER' not in os.environ:
# 读取 .env 文件
dotenv.load_dotenv(verbose=True)
global RETRY_TIMES_LIMIT
try:
PHONE_NUMBER = os.getenv("PHONE_NUMBER")
Expand Down
3 changes: 0 additions & 3 deletions scripts/onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
from PIL import ImageDraw,Image,ImageOps
import numpy as np
import onnxruntime
import time
import random
import sys

anchors = [[(116,90),(156,198),(373,326)],[(30,61),(62,45),(59,119)],[(10,13),(16,30),(33,23)]]
anchors_yolo_tiny = [[(81, 82), (135, 169), (344, 319)], [(10, 14), (23, 27), (37, 58)]]
Expand Down
5 changes: 2 additions & 3 deletions scripts/sensor_updator.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import logging
import os
from datetime import datetime
from dateutil.relativedelta import relativedelta

import requests
from sympy import true
Expand Down Expand Up @@ -77,8 +76,8 @@ def update_month_data(self, postfix: str, sensorState: float, usage=False):
if usage
else MONTH_CHARGE_SENSOR_NAME + postfix
)
last_updated = datetime.now() - relativedelta(months=1)
last_reset = last_updated.strftime("%Y-%m")
last_updated = datetime.now().month - 1
last_reset = datetime.now().replace(month=last_updated).strftime("%Y-%m")
request_body = {
"state": sensorState,
"unique_id": sensorName,
Expand Down

0 comments on commit 2dbd250

Please sign in to comment.