Skip to content

Commit

Permalink
变更
Browse files Browse the repository at this point in the history
  • Loading branch information
Lelter committed Apr 28, 2023
1 parent bfba4a3 commit b5228a5
Show file tree
Hide file tree
Showing 36 changed files with 777 additions and 89 deletions.
156 changes: 85 additions & 71 deletions code/Parse/parseData.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,82 +8,96 @@
# 连接数据库
db = pymysql.connect(host="localhost", user="root", password="ye007068", database="musicdata")

domain = "http://localhost:3000"


def getSingleList(interface="/user/record", uid=88090080, type=0):
url = domain + interface + "?uid=" + str(uid) + "&type=" + str(type)
r = requests.get(url)

# 只提取歌曲和歌手和播放次数和分数和歌曲ID
data = json.loads(r.text)
if data['code'] == -2:
print("无权限访问", uid)
return
if data['code'] == 406:
print("操作频繁", uid)
time.sleep(10)
return
data = data['allData']
# for i in data:
# print(i['song']['name'], i['song']['ar'][0]['name'], i['playCount'], i['score'], i['song']['id'])
# 存储记录 userId,songName,singer,songId,playCount,score
sql = "INSERT INTO songinfo(userId,songName,singer,songId,playCount,score) VALUES (%s, %s, %s, %s, %s, %s)"
for i in data:
DBUtil().exeDML(sql,
uid, i['song']['name'], i['song']['ar'][0]['name'], i['song']['id'], i['playCount'], i['score'])


def parseAllUser(interface="/user/followeds", uid=9003, limit=100, offset=0):
userList = []
# 将所有用户的ID存入userList
for count in range(0, 100):
print("offset: ", offset)

url = domain + interface + "?uid=" + str(uid) + "&limit=" + str(limit) + "&offset=" + str(offset)
try:
r = requests.get(url)
time.sleep(1)
data = json.loads(r.text)
# print(data)

data = data['followeds']
for i in data:
userList.append([i['userId'], i['nickname']])
offset += 100
except Exception as e:
print(e)
class parseData:
def __init__(self):
self.domain = "http://www.codeman.ink:3000"

def getSingleList(self, interface="/user/record", uid=88090080, type=0):
url = self.domain + interface + "?uid=" + str(uid) + "&type=" + str(type)
r = requests.get(url)

# 只提取歌曲和歌手和播放次数和分数和歌曲ID
data = json.loads(r.text)
if data['code'] == -2:
print("无权限访问", uid)
return
if data['code'] == 406:
print("操作频繁", uid)
time.sleep(10)
continue
# print(userList)
# 将所有用户的歌单存入数据库
for user in userList:
# print(user[0], user[1])
sql = "INSERT INTO user(userId, nickname) VALUES (%s, %s)"
DBUtil().exeDML(sql, (user[0], user[1]))


def getMusicList():
# 获取用户ID
userList = []
sql = "SELECT userId FROM user"

results = DBUtil().query_all(sql)
for row in results:
uid = row[0]
# print(uid)
userList.append(uid)
# 获取用户歌单

# print(userList)
for user in userList:
getSingleList(uid=user, type=0) # 获取单个用户播放记录

pass
return
data = data['allData']
# for i in data:
# print(i['song']['name'], i['song']['ar'][0]['name'], i['playCount'], i['score'], i['song']['id'])
# 存储记录 userId,songName,singer,songId,playCount,score
sql = "INSERT INTO songinfo(userId,songName,singer,songId,playCount,score) VALUES (%s, %s, %s, %s, %s, %s)"
for i in data:
DBUtil().exeDML(sql,
uid, i['song']['name'], i['song']['ar'][0]['name'], i['song']['id'], i['playCount'],
i['score'])

def parseAllUser(self, interface="/user/followeds", uid=9003, limit=100, offset=0):
userList = []
# 将所有用户的ID存入userList
for count in range(0, 100):
print("offset: ", offset)

url = self.domain + interface + "?uid=" + str(uid) + "&limit=" + str(limit) + "&offset=" + str(offset)
try:
r = requests.get(url)
time.sleep(1)
data = json.loads(r.text)
# print(data)

data = data['followeds']
for i in data:
userList.append([i['userId'], i['nickname']])
offset += 100
except Exception as e:
print(e)
time.sleep(10)
continue
# print(userList)
# 将所有用户的歌单存入数据库
for user in userList:
# print(user[0], user[1])
sql = "INSERT INTO user(userId, nickname) VALUES (%s, %s)"
DBUtil().exeDML(sql, (user[0], user[1]))

def getMusicList(self, ):
# 获取用户ID
userList = []
sql = "SELECT userId FROM user"

results = DBUtil().query_all(sql)
for row in results:
uid = row[0]
# print(uid)
userList.append(uid)
# 获取用户歌单

# print(userList)
for user in userList:
self.getSingleList(uid=user, type=0) # 获取单个用户播放记录

pass

def netGetUserSongs(self, songIds, interface="/song/detail", ):
print(songIds)
# 用逗号隔开
if songIds.__class__ == list:
songIds = ','.join(songIds)
else:
songIds = str(songIds)
url = self.domain + interface + "?ids=" + str(songIds)
r = requests.get(url)
data = json.loads(r.text)
data = data['songs']
return data


# test() #测试
# parseAllUser() # 爬取一部分用户的UID
# getMusicList() # 爬取一部分用户的歌单
# 308567448
getSingleList(uid=88090080, type=0)
# getSingleList(uid=88090080, type=0)parseData().netGetUserSongs()
6 changes: 6 additions & 0 deletions code/api/apimodel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from pydantic import BaseModel


class User(BaseModel):
username: str
password: str
Loading

0 comments on commit b5228a5

Please sign in to comment.