You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.8 KiB

#!/usr/bin/env python
# encoding: utf-8
"""
@author: tx
@file: tool.py
@time: 2023/7/4 19:37
@desc:
"""
import os
import socket
import time
from datetime import datetime
import pygame
def parse_datetime(time_str, trans_str='%Y-%m-%d %H:%M:%S'):
"""
时间日期格式转化
:param time_str:
:param trans_str: 转义输出格式
:return:
"""
possible_formats = [
"%Y-%m-%d %H:%M:%S",
"%Y/%m/%d %H:%M:%S",
"%Y-%m-%d",
"%Y/%m/%d",
"%d-%m-%Y %H:%M:%S",
"%d/%m/%Y %H:%M:%S",
"%d-%m-%Y",
"%d/%m/%Y",
"%Y年%m月%d",
"%Y年%m月%d%H时%M分",
"%Y年%m月%d%H时%M分%S秒",
"%Y-%m-%dT%H:%M:%S.%f",
"%Y-%m-%dT%H:%M:%S.%f%z",
"%Y-%m-%dT%H:%M:%S%z",
"%Y-%m-%d %H:%M:%S.%f",
"%Y-%m-%d %H:%M:%S.%f%z",
]
datetime_list = []
for format_str in possible_formats:
try:
dt = datetime.strptime(time_str, format_str)
datetime_list.append(dt)
except ValueError:
pass
if datetime_list:
result = datetime_list[0].strftime(trans_str)
return result
else:
return time_str
def get_ip_address():
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('8.8.8.8', 1))
ip = s.getsockname()[0]
finally:
s.close()
return ip
def play_sound(sound_name):
"""
播放音频文件
:param sound_name:
:return:
"""
pygame.mixer.init(frequency=15500, size=-16, channels=4)
if os.path.exists(os.path.join("static", "sounds", sound_name)):
track2 = pygame.mixer.Sound(os.path.join(os.getcwd(), "static", "sounds", sound_name))
track2.play()
time.sleep(2.5)