-
Notifications
You must be signed in to change notification settings - Fork 8
/
WangYiYunCiYun.py
101 lines (83 loc) · 2.99 KB
/
WangYiYunCiYun.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
# -*- coding: utf-8 -*-
import sys
import re
import requests
cookies = dict(appver='2.0.2')
headers = dict(referer="http://music.163.com")
global failtime
failtime = 0
global total_song_nums
total_song_nums = 0
def get_singerid_from_singer(singer):
url = 'http://music.163.com/api/search/get/'
data = {
's': singer,
'type': 100,
'limit': 20,
'offset': 0,
}
r = requests.post(url, headers=headers, cookies=cookies, data=data)
data = r.json()['result']['artists'][0]
return {
'singerid': data['id'],
'album_nums': data['albumSize']
}
def get_albumlist_from_singerid(singer_data):
url = 'http://music.163.com/api/artist/albums/{0}?offset=0&limit={1}'.format(
singer_data['singerid'], singer_data['album_nums'])
r = requests.get(url, headers=headers, cookies=cookies)
album_nums = r.json()
return [{'albumid': i['id'], 'song_nums': i['size']} for i in r.json()['hotAlbums']]
def get_songid_from_albumid(albumid):
url = 'http://music.163.com/api/album/{}/'.format(albumid)
r = requests.get(url, headers=headers, cookies=cookies)
return [i['id'] for i in r.json()['album']['songs']]
def get_lyric_from_songid(songid):
url = 'http://music.163.com/api/song/lyric?os=pc&id={}&lv=-1&kv=-1&tv=-1'.format(songid)
r = requests.get(url, headers=headers, cookies=cookies)
global failtime
global total_song_nums
total_song_nums += 1
try:
text = r.json()['lrc']['lyric'].encode('utf-8')
textlist = text.split('\n')
text = '\n'.join([re.sub(r'\[.*?\]', '', i) for i in textlist])
return text
except KeyError:
failtime += 1
return ''
def main(singer):
lyrics = []
singer_data = get_singerid_from_singer(singer)
albumlist = get_albumlist_from_singerid(singer_data)
with open(singer+'lyric.txt', 'w') as lyricfile:
for albumid in albumlist:
songids = get_songid_from_albumid(albumid['albumid'])
for songid in songids:
lyric = get_lyric_from_songid(songid)
lyricfile.write(lyric)
print "write once."
print "total ", total_song_nums, 'songs.'
print "fail ", failtime, 'times.'
print "done"
def generate_ciyun_pic():
import matplotlib.pyplot as plt
from wordcloud import WordCloud
import jieba
from cv2 import imread
text_from_file_with_apath = open('./{}lyric.txt'.format(singer), 'r').read().replace('作词', '').replace('作曲', '')
wordlist_after_jieba = jieba.cut(text_from_file_with_apath, cut_all = True)
wl_space_split = " ".join(wordlist_after_jieba)
mask_img = imread('./mask.jpg')# , flatten=True)
my_wordcloud = WordCloud(
font_path='msyh.ttc',
background_color='white',
mask=mask_img
).generate(wl_space_split)
plt.imshow(my_wordcloud)
plt.axis("off")
plt.show()
if __name__ == '__main__':
singer = sys.argv[1]
main(singer)
# generate_ciyun_pic()