Skip to content

Commit

Permalink
Feat: Add detection to Chinese holiday and detailed error.
Browse files Browse the repository at this point in the history
  • Loading branch information
remiliacn committed Feb 7, 2022
1 parent eb29b63 commit 801a1ac
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
47 changes: 40 additions & 7 deletions Services/simulate_stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ async def get_all_stonk_log_by_user(self, uid: Union[int, str], ctx=None):
time_now = datetime.now()

if time_now.hour >= 15 or (time_now.hour == 9 and time_now.minute < 15 or time_now.hour < 9) \
or time_now.weekday() >= 5:
or time_now.weekday() >= 5 or await self._get_if_is_chinese_holiday(time_now):
time_awareness = '【SZ·休市】\n'
elif time_now.hour == 12:
time_awareness = '【SZ·午间休市】\n'
Expand All @@ -104,7 +104,8 @@ async def get_all_stonk_log_by_user(self, uid: Union[int, str], ctx=None):
else:
time_awareness = '【SZ·交易中】\n'

if time_now.weekday() >= 5 or (time_now.hour == 9 and time_now.minute < 30) or time_now.hour < 9:
if time_now.weekday() >= 5 or (time_now.hour == 9 and time_now.minute < 30) \
or time_now.hour < 9 or await self._get_if_is_chinese_holiday(time_now):
time_awareness += '【港股·休市】\n'
elif time_now.hour >= 16:
time_awareness += "【港股·休市】\n"
Expand Down Expand Up @@ -140,12 +141,13 @@ async def get_all_user_info(self, valid_time=None):
user_data_info = []
for uid in data:
data = await self.get_user_overall_stat(uid, valid_time)
user_data_info.append(data)
if data:
user_data_info.append(data)

if len(user_data_info) > 3:
if len(user_data_info) > 5:
sorted_list_reverse = sorted(user_data_info, key=lambda d: d["ratio"], reverse=True)
sorted_list = sorted_list_reverse[::-1][:3]
sorted_list_reverse = sorted_list_reverse[:3]
sorted_list = sorted_list_reverse[::-1][:5]
sorted_list_reverse = sorted_list_reverse[:5]
else:
sorted_list_reverse = sorted(user_data_info, key=lambda d: d["ratio"], reverse=True)
sorted_list = sorted_list_reverse[::-1]
Expand Down Expand Up @@ -312,6 +314,9 @@ async def get_user_overall_stat(self, uid: Union[int, str], valid_time) -> dict:
current_stock_money += price_now * data[uid][stock]['purchaseCount']

total_money += current_stock_money
if round(total_money, 2) == 5000000.00:
return {}

ratio = ((total_money - (10 ** 6 * 5)) / (10 ** 6 * 5)) * 100
return {
"total": total_money,
Expand Down Expand Up @@ -478,12 +483,40 @@ async def _get_western_stock_data(

return True, self.stock_price_cache[stock_code]

@staticmethod
async def _get_if_is_chinese_holiday(day_now):
if day_now.month == 1 and day_now.day <= 3:
return True

if (day_now.month == 1 and day_now.day >= 29) or (day_now.month == 2 and day_now.day <= 6):
return True

if day_now.month == 4 and 3 <= day_now.day <= 5:
return True

if day_now.month == 5 and day_now.day <= 4:
return True

if day_now.month == 6 and 3 <= day_now.day <= 5:
return True

if day_now.month == 9 and 10 <= day_now.day <= 12:
return True

if day_now.month == 10 and day_now.day <= 7:
return True

return False

async def _get_chinese_stock_data(
self, last_updated_exact, stock_code, day_now, day_now_timestamp, last_updated
) -> (bool, dict):
if datetime.today().weekday() >= 5:
if day_now.weekday() >= 5:
return last_updated_exact.weekday() >= 4, self.stock_price_cache[stock_code]

if await self._get_if_is_chinese_holiday(day_now):
return True, self.stock_price_cache[stock_code]

# 周1-5逻辑
# AB股闭市时间
if self.stock_price_cache[stock_code]['stockType'] in (0, 1) \
Expand Down
11 changes: 9 additions & 2 deletions awesome/plugins/stonk/stonk.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
import traceback
from asyncio.log import logger
from datetime import datetime
from random import choice
Expand Down Expand Up @@ -173,10 +174,16 @@ async def my_stonks(session: nonebot.CommandSession):
@nonebot.on_command('战绩', aliases={'炒股战绩', '龙虎榜'}, only_to_me=False)
async def stonk_stat_send(session: nonebot.CommandSession):
try:
await session.send('少女祈祷中……')
leaderboard = await virtual_market.get_all_user_info(valid_time=60 * 60)
except ClientConnectionError:
await session.finish('查询过于频繁,请重试。')
return
except Exception as err:
logger.error(f'战绩功能出错{err}')
await session.finish('查询出错, 请重试。')
bot = nonebot.get_bot()
logger.error(f'龙虎榜功能未知错误:{err}')
await bot.send_private_msg(user_id=SUPER_USER, message=f'战绩功能出错:{err}\n{traceback.format_exc()}')
await session.finish('查询出现未知错误')
return

await session.finish(
Expand Down

0 comments on commit 801a1ac

Please sign in to comment.