整理代码

This commit is contained in:
2019-02-04 16:16:41 +08:00
parent 0219d5d1aa
commit 362f5c51d3
11 changed files with 202 additions and 20 deletions

31
Struct/Chat.py Normal file
View File

@ -0,0 +1,31 @@
from .User import User
from .Lottery import Lottery
class Chat:
content: str =""
user: User=None
filterString:list = ["",]
isFiltered = False
def __init__(self, json=None, lottery:Lottery = None):
if json:
self.parse(json)
if lottery:
self.filterString.append(lottery.content)
def parse(self, json):
self.user = User(json)
if "extra" in json:
if "content" in json["extra"]:
self.content = json["extra"]['content']
if self.content in self.filterString:
self.isFiltered = True
def __str__(self):
return "{} : {}".format(self.user,self.content)
def __unicode__(self):
return self.__str__()

0
Struct/Digg.py Normal file
View File

54
Struct/Gift.py Normal file
View File

@ -0,0 +1,54 @@
import requests
from .User import User
class Gift:
ID:int = 0
count:int = 0
roomID:int = 0
giftList:dict = {}
amount:int = 0
user:User = None
def __init__(self, json=None):
if json:
self.parse(json)
def parse(self, json):
self.user = User(json)
if "common" in json and json["common"] is not None:
if Gift.roomID != int(json["common"]["room_id"]):
Gift.roomID = int(json["common"]["room_id"])
self.update()
if "extra" in json and json["extra"] is not None:
if "present_info" in json["extra"] and json["extra"]['present_info'] is not None:
self.ID = int(json["extra"]['present_info']['id'])
self.count = json["extra"]['present_info']['repeat_count']
elif "present_end_info" in json["extra"] and json["extra"]['present_end_info'] is not None:
self.ID = int(json["extra"]['present_end_info']['id'])
self.count = json["extra"]['present_end_info']['count']
if self.ID != 0 and self.ID in self.giftList:
self.amount = self.giftList[self.ID]["Price"] * self.count
else:
self.update()
def update(self):
p = requests.get("https://i.snssdk.com/videolive/gift/get_gift_list?room_id={roomID}"
"&version_code=730&device_platform=android".format(roomID = self.roomID))
d = p.json()
if "gift_info" not in d:
print("错误:礼物更新失败")
else:
for i in d["gift_info"]:
_id = int(i["id"])
Gift.giftList[_id] = {"Name": i["name"], "Price": i["diamond_count"]}
def __str__(self):
if self.ID in self.giftList:
giftN = self.giftList[self.ID]["Name"]
else:
giftN = "未知礼物[{}]".format(self.ID)
return "{user} 送出的 {count}{name}".format(user= self.user, count= self.count, name= giftN)
def __unicode__(self):
return self.__str__()

71
Struct/Lottery.py Normal file
View File

@ -0,0 +1,71 @@
import requests
import time
from .LuckyUser import LuckyUser
class Lottery:
ID: int = 0
isActive = False
content = ""
isFinished = False
luckyUsers = []
joinedUserCount = 0
prizeName = ""
finish:int = 0
def __init__(self, json=None):
if json:
self.parse(json)
def parse(self, json):
if "lottery_info" in json and json["lottery_info"] is not None:
self.isActive = int(json["lottery_info"]["status"]) > 0
self.ID = json["lottery_info"]["lottery_id"]
for i in json["lottery_info"]['conditions']:
if i['type'] != 3:
continue
self.content = i["content"]
self.joinedUserCount = int(json["lottery_info"]["candidate_num"])
self.prizeName = json["lottery_info"]["prize_info"]["name"]
_delta = int(json["lottery_info"]["draw_time"]) - int(json["lottery_info"]["current_time"])
self.finish = time.time()+_delta+1
elif "extra" in json and json["extra"] is not None:
if "lottery_info" in json["extra"] and json["extra"]["lottery_info"] is not None:
return self.parse(json["extra"])
def update(self):
if self.isActive:
if not self.isFinished and self.finish > time.time():
self.checkFinished()
return True
return False
def checkFinished(self):
p = requests.get("https://i.snssdk.com/videolive/lottery/check_user_right?lottery_id={}"
"&version_code=730&device_platform=android".format(
self.ID
))
d = p.json()
if d["base_resp"]["status_code"] != 0:
self.isActive = False
self.isFinished = False
return
self.isActive = int(d["lottery_info"]["status"]) > 0
self.isFinished = int(d["lottery_info"]["status"]) == 2
self.joinedUserCount = int(d["lottery_info"]["candidate_num"])
if self.isFinished:
for i in d["lottery_info"]["lucky_users"]:
self.luckyUsers.append(LuckyUser(i))
def __str__(self):
if self.isFinished:
ret = "恭喜以下中奖用户:\n"
for i in self.luckyUsers:
ret += "> {} {}\n".format(i,self.prizeName)
ret += "> 参与人数:{}".format(self.joinedUserCount)
return ret
elif self.isActive:
return "正在抽奖中。。。\n" \
"> 参与人数:{}".format(self.joinedUserCount)
else:
return "抽奖已失效"

19
Struct/LuckyUser.py Normal file
View File

@ -0,0 +1,19 @@
from .User import User
class LuckyUser:
user = None
count = 0
def __init__(self, json=None):
if json:
self.parse(json)
def parse(self, json):
self.user = User()
self.user.ID = json['user_id']
self.user.name = json['user_name']
self.count = int(json["grant_count"])
def __str__(self):
return "用户 {} 获得了 {}".format(self.user,self.count)

36
Struct/MemberMsg.py Normal file
View File

@ -0,0 +1,36 @@
from .User import User
class MemberMsg:
type:int = 0
content:str = ""
user:User = None
def __init__(self, json=None):
if json:
self.parse(json)
def parse(self, json):
self.user = User(json)
if "extra" in json:
if "action" in json["extra"]:
self.type = json["extra"]['action']
elif "content" in json["extra"]:
self.content = json["extra"]['content']
def __str__(self):
if self.type == 3:
return "{} 被禁言了".format(self.user)
elif self.type == 4:
return "{} 被取消禁言了".format(self.user)
elif self.type == 5:
return "{} 被任命为房管".format(self.user)
elif self.type == 1:
return "{} 进入了房间".format(self.user)
else:
if self.content == "":
return "未知消息{} 关于用户 {}".format(self.type, self.user)
return self.content.format(self.user)
def __unicode__(self):
return self.__str__()

54
Struct/User.py Normal file
View File

@ -0,0 +1,54 @@
class User:
ID: int = 0
name: str = ""
brand: str = ""
level: int = 0
type: int = 0
block: bool = False
mute: bool = False
def __init__(self, json=None):
if json:
self.parse(json)
def parse(self, json):
if "extra" in json:
if "user" in json["extra"] and json["extra"]["user"] is not None:
self.ID = json["extra"]['user']['user_id']
self.name = json["extra"]['user']['name']
if "im_discipulus_info" in json["extra"] and json["extra"]["im_discipulus_info"] is not None:
self.level = json["extra"]["im_discipulus_info"]["level"]
self.brand = json["extra"]["im_discipulus_info"]["discipulus_group_title"]
if "user_room_auth_status" in json["extra"] and json["extra"]["user_room_auth_status"] is not None:
self.type = json["extra"]["user_room_auth_status"]["user_type"]
self.block = json["extra"]["user_room_auth_status"]["is_block"]
self.mute = json["extra"]["user_room_auth_status"]["is_silence"]
elif "room" in json and json["room"] is not None:
if "user_info" in json["room"] and json["room"]["user_info"] is not None:
self.ID = json["room"]['user_info']['user_id']
self.name = json["room"]['user_info']['name']
elif "anchor" in json and json["anchor"] is not None:
if "user_info" in json["anchor"] and json["anchor"]['user_info'] is not None:
self.ID = json["anchor"]['user_info']['user_id']
self.name = json["anchor"]['user_info']['name']
if self.type is None:
self.type = 0
if isinstance(self.level, str):
self.level = int(self.level)
def __str__(self):
if self.level == 0:
if self.type == 1:
return "[房管]{}".format(self.name)
elif self.type == 3:
return "[主播]{}".format(self.name)
else:
return "{}".format(self.name)
else:
if self.type != 0:
return "[{}{}]{}".format(self.brand, self.level, self.name)
return "<{}{}>{}".format(self.brand,self.level,self.name)
def __unicode__(self):
return self.__str__()