-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathWYtoQQ.py
116 lines (107 loc) · 4.99 KB
/
WYtoQQ.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
import urlparse
import re
from time import sleep
from base import BaseSpider
from settings import *
from urllib2 import quote
from api.wy import get_playlist_detail
from utils import _print, retry, RetryException
class WYtoQQ(BaseSpider):
@retry(retry_times=3, notice_message="login failed and retry")
def prepare(self):
self.browser.get("https://y.qq.com")
self.browser.set_window_size(1920, 1080)
self.wait.until(lambda browser: browser.find_element_by_xpath("/html/body/div[1]/div/div[2]/span/a[2]"))
self.browser.find_element_by_xpath("/html/body/div[1]/div/div[2]/span/a[2]").click()
self.wait.until(lambda browser: browser.find_element_by_id("frame_tips"))
self.browser.switch_to.frame("frame_tips")
self.wait.until(lambda browser: browser.find_element_by_id("switcher_plogin"))
sleep(0.5)
self.browser.find_element_by_id("switcher_plogin").click()
user_input = self.browser.find_element_by_id("u")
user_input.send_keys(self.config.account)
pwd_input = self.browser.find_element_by_id("p")
pwd_input.send_keys(self.config.password)
submit = self.browser.find_element_by_id("login_button")
submit.click()
sleep(1)
self.browser.switch_to.default_content()
self.browser.refresh()
self.wait.until(lambda browser: browser.find_element_by_class_name("popup_user"))
user_info = self.browser.find_element_by_class_name("popup_user")
user_info.find_element_by_css_selector("*")
print("login sucess")
def get_source_playlist(self):
pattern = re.compile(r'^.*id=(\d*)')
url = self.config.source_playlist_url
match = pattern.search(url)
if match:
playlist_id = match.groups()[0]
else:
raise Exception("can not find id, please check wy url!!!")
detail = get_playlist_detail(playlist_id)
song_list = detail['playlist']['tracks']
song_details = list()
for song in song_list:
ar_name = list()
song_name = song['name']
for ar in song['ar']:
ar_name.append(ar['name'])
album = ''
song_details.append((song_name, ' '.join(ar_name), album))
# response = requests.get(self.config.source_playlist_url.replace('#', 'm'), headers=headers)
# html = response.content
# soup = BeautifulSoup(html, "html.parser")
# details = soup.select("span[class='detail']")
# song_details = list()
# for detail in details:
# song_text = detail.text
# song_detail = song_text.strip('\n').split('\n\n')
#
# song = song_detail[0]
# singer = song_detail[1].split('- ', 1)[0]
# # don't use album yet
# album = ''
# song_details.append((song, singer.strip('\n'), album))
print("get 163 playlist success")
self.source_playlist = song_details
def get_target_playlist(self):
# self.browser.get(self.config.target_playlist_url)
# self.wait.until(lambda browser: browser.find_element_by_class_name("playlist__list"))
# playlist = self.browser.find_element_by_class_name("playlist__list")
# playlist_items = playlist.find_elements_by_class_name('playlist__item')
#
# for item in playlist_items:
# title = item.find_element_by_class_name('playlist__title').text
# item_id = item.get_attribute('data-dirid')
# if title == self.config.qq_playlist_name:
# self.target_playlist_tag = item_id
# return
# else:
# raise Exception("can not find qq playlist:{}, please check".format(self.config.qq_playlist_name))
self.target_playlist_tag = self.config.target_playlist_url.split('dirid=')[-1]
return
def sync_song(self):
for song_detail in self.source_playlist:
song = song_detail[0]
singer = song_detail[1]
search_word = u"{} {}".format(song, singer)
url_sw = quote(search_word.encode('utf8'))
self.browser.get(qq_search_url.format(url_sw))
self.wait.until(lambda browser: browser.find_element_by_class_name("songlist__list"))
sleep(0.5)
@retry(retry_times=3)
def _add(browser):
browser.execute_script("document.getElementsByClassName('songlist__list')[0].firstElementChild.getElementsByClassName('list_menu__add')[0].click()")
sleep(0.5)
browser.find_element_by_css_selector("a[data-dirid='{}']".format(self.target_playlist_tag)).click()
_print(u"song:{} success".format(song))
try:
_add(self.browser)
except RetryException:
_print(u"song:{}, sync error".format(song))
self.failed_list.append(search_word)
else:
self.success_list.append(search_word)
if __name__ == '__main__':
WYtoQQ().run()