-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
165 lines (126 loc) · 5.59 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from shuaUI import Ui_shuaUI
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QWidget, QFileDialog, QApplication
import sys
from requestium import Session
import time
import json
from PyQt5.QtCore import QThread, pyqtSignal, QTimer
import random
class Win(QWidget):
currentDisplay = None
def __init__(self, parent=None):
super().__init__(parent)
self.ui = Ui_shuaUI()
self.ui.setupUi(self)
self.initUI()
def initUI(self):
self.ui.loadCookieBtn.clicked.connect(self.loadCookie)
self.ui.loginBtn.clicked.connect(self.logIn)
self.ui.playlistBtn.clicked.connect(self.playlistID)
def initDriver(self):
self.s = Session(webdriver_path=r'C:\Software\Cent\CentBrowser\Application\chromedriver.exe',
browser='chrome',
default_timeout=15,
webdriver_options={
'arguments': [
# '-headless',
'-mute-audio',
'-window-size=1920,1080',
'-start-maximized',
'-no-sandbox']}
)
self.s.driver.get('http://music.163.com/#/login')
time.sleep(1)
self.s.driver.switch_to.default_content()
source_text = self.s.driver.page_source
autoId_st = source_text.find("visibility:") + 26
autoId_end = autoId_st + 24
autoId = source_text[autoId_st:autoId_end]
path = str("//*[@id='" + autoId + "']/div[1]/div[1]/a")
time.sleep(0.5)
lockBtn = self.s.driver.find_element_by_xpath(path)
lockBtn.click()
def loadCookie(self):
self.fileName, self.fileType = QFileDialog.getOpenFileName(self, "选取文件", "./", "Text Files (*.txt)")
self.ui.cookieLable.setText(self.fileName)
self.ui.cookiePic.setPixmap(QPixmap('2.png'))
def logIn(self):
self.initDriver()
f = open(self.fileName)
cookie = f.read()
cookie = json.loads(cookie)
for c in cookie:
self.s.driver.add_cookie(c)
self.s.driver.refresh()
self.s.driver.get("https://music.163.com/#/user/update")
self.s.driver.switch_to.frame(self.s.driver.find_element_by_id("g_iframe"))
time.sleep(1)
nickName = self.s.driver.find_element_by_xpath('//*[@id="nickname"]').get_attribute('value')
self.ui.introLable.setText('你好呀(✿◠‿◠): ' + nickName)
self.ui.loginPic.setPixmap(QPixmap('2.png'))
self.s.driver.get("https://music.163.com")
def playlistID(self):
listID = self.ui.playlistEdit.text()
self.s.driver.get("https://music.163.com/#/playlist?id=" + listID)
self.track_handle = self.s.driver.current_window_handle
self.ui.playlistPic.setPixmap(QPixmap('2.png'))
self.s.driver.switch_to.frame(self.s.driver.find_element_by_id("g_iframe"))
trackCount = self.s.driver.find_element_by_xpath('//*[@id="m-playlist"]/div[1]/div/div/div[2]/div[1]/span').text
self.ui.totalPlayLcd.display(trackCount[:-2])
time.sleep(2)
self.reqSongs()
def reqSongs(self):
js = 'window.open("https://music.163.com/#/user/level");'
self.s.driver.execute_script(js)
# 获取当前窗口句柄集合(列表类型)
handles = self.s.driver.window_handles
# 获取 num 窗口
self.reqsongs_handle = None
for handle in handles:
if handle != self.track_handle:
self.reqsongs_handle = handle
self.s.driver.switch_to.window(self.reqsongs_handle)
time.sleep(2)
self.s.driver.switch_to.frame(self.s.driver.find_element_by_id("g_iframe"))
reqnextLv1 = self.s.driver.find_element_by_xpath('//*[@id="m-level"]/div/div[3]/div[2]').text
reqnextLv = (reqnextLv1.split("\n", 1))[1]
reqnextLv = reqnextLv[4:-1]
currentLv1 = self.s.driver.find_element_by_xpath('//*[@id="m-level"]/div/div[1]/div[1]').text
currentLv = currentLv1.split("\n", 2)[1]
self.ui.numLcd.display(reqnextLv)
self.ui.lvLcd.display(currentLv)
self.s.driver.switch_to.window(self.track_handle)
self.startPlay()
def startPlay(self):
# # 播 放
self.s.driver.switch_to.frame(self.s.driver.find_element_by_id("g_iframe"))
element = self.s.driver.find_element_by_xpath("//*[@id='content-operation']/a[1]")
element.click()
self.timeNeeded()
def timeNeeded(self):
global currentDisplay
self.timeDelay = random.randint(85, 135)
self.ui.totalSongTimeLcd.display(self.timeDelay)
currentDisplay = self.timeDelay
self.timer = QTimer(self)
self.timer.timeout.connect(self.timeDisplay)
self.timer.start(1000)
def timeDisplay(self):
global currentDisplay
currentDisplay -= 1
if currentDisplay == 0:
self.timer.stop()
self.ui.currentSongTimeLcd.display(currentDisplay)
def nextSong(self):
self.s.driver.switch_to.frame(self.s.driver.find_element_by_id("g_iframe"))
ele = self.s.driver.find_element_by_xpath("//*[@id='g_player']/div[1]/a[3]")
ele.click()
self.timeNeeded()
def main():
app = QApplication(sys.argv)
win = Win()
win.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()