Skip to content

Commit

Permalink
refactor: 代码重构
Browse files Browse the repository at this point in the history
  • Loading branch information
gakkiyomi committed Dec 22, 2024
1 parent 5b6f600 commit ba97f7c
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions src/core/chatroom.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,23 +189,30 @@ def renderWeather(username: str, lines: list[str]) -> str:
if username != 'xiaoIce':
return '\n'.join(lines)
for index in range(len(lines)):
data = json.loads(lines[index])
data['date'] = data['date'].split(',')
data['weatherCode'] = data['weatherCode'].split(',')
data['max'] = [i + '°C' for i in data['max'].split(',')]
data['min'] = [i + '°C' for i in data['min'].split(',')]
data.pop('msgType')
data.pop('type')
table = PrettyTable()
table.title = data.pop('t') + ' ' + data.pop('st')
table.field_names = list(data.keys())
for i in range(len(data['date'])):
row_data = [data[key][i] for key in data.keys()]
table.add_row(row_data)
lines[index] = table.get_string()
try:
lines[index] = _renderWeather(lines[index])
except json.JSONDecodeError:
pass
return '\n'.join(lines)


def _renderWeather(json_str: str) -> str:
data = json.loads(json_str)
data['date'] = data['date'].split(',')
data['weatherCode'] = data['weatherCode'].split(',')
data['max'] = [i + '°C' for i in data['max'].split(',')]
data['min'] = [i + '°C' for i in data['min'].split(',')]
data.pop('msgType')
data.pop('type')
table = PrettyTable()
table.title = data.pop('t') + ' ' + data.pop('st')
table.field_names = list(data.keys())
for i in range(len(data['date'])):
row_data = [data[key][i] for key in data.keys()]
table.add_row(row_data)
return table.get_string()


def at_notification(api: FishPi, message: dict) -> None:
if message["userName"] != api.current_user and message["md"].__contains__(f'@{api.current_user}'):
sender(Event(type=NTYPE.FROM_CHATROOM, sender=message["userName"],
Expand Down

0 comments on commit ba97f7c

Please sign in to comment.