class NetEaseService(object): def __init__(self): self.api = NetEase() self.storage = Storage() self.storage.load() self.collection = self.storage.database['collections'] self.autologin() @property def user(self): return self.storage.database['user'] @property def account(self): return self.user['username'] @property def md5pass(self): return self.user['password'] @property def userid(self): return self.user['user_id'] @property def username(self): return self.user['nickname'] def autologin(self, _account = None, _md5pass = None): if _account is not None and _md5pass is not None: account, md5pass = _account, _md5pass elif self.account and self.md5pass: account, md5pass = self.account, self.md5pass else: return False resp = self.api.login(account, md5pass) print(resp) if resp['code'] == 200: userid = resp['account']['id'] nickname = resp['profile']['nickname'] self.storage.login(account, md5pass, userid, nickname) self.storage.save() return True else: self.storage.logout() return False def login_status(self): result = { "logged_in": True, "username": self.username, "id": self.userid } if not self.account or not self.md5pass: result["logged_in"] = False return result def request_api(self, func, *args): result = func(*args) if result: return result if not self.autologin(): raise Exception("You need to log in") return False return result def get_my_playlists(self): playlists = self.request_api(self.api.user_playlist, self.userid) return self.api.dig_info(playlists, "playlists") def get_playlist(self, playlist_id): songs = self.api.playlist_detail(playlist_id) return self.api.dig_info(songs, "songs") def get_song(self): pass def get_song_lyrics(self, song_id): return self.api.song_lyric(song_id)
def process(local_song_path, save_path="/Users/zhangzhenhu/Music/mymusic"): global g_log_file net = NetEase() local_song = taglib.File(local_song_path) print("") print("========本地歌曲=======") # pprint.pprint(local_song.tags) if 'TITLE' not in local_song.tags or 'ARTIST' not in local_song.tags: return print(local_song.tags['TITLE'], local_song.tags['ARTIST'], local_song.tags.get('ALBUM', "")) title = local_song.tags['TITLE'][0] song_result = net.search(keywords=title, stype=1) if song_result is None or 'songs' not in song_result: print("net_song_not_found", local_song_path, file=g_log_file) return net_song = get_match_song(local_song, song_result['songs']) # pprint.pprint(net_song) if net_song is None: print("net_song_not_match", local_song_path, file=g_log_file) return print("----------网易歌曲--------") print(net_song['id'], net_song['name'], net_song['album']['name'], ','.join([x['name'] for x in net_song['artists']])) g_song_db[net_song['id']] = net_song # 歌手id,只选取第一个 artist_id = net_song['artists'][0]['id'] artist_name = net_song['artists'][0]['name'] # 获取歌手信息 artist_json = net.get_artist_desc(artist_id) # print(artist_desc) if artist_json and artist_json['code'] == 200: artist_info = artist_json['artist'] g_artist_db[artist_id] = artist_info artist_img = artist_info['img1v1Url'] artist_pic = artist_info['picUrl'] else: print("artist_not_found", local_song_path, file=g_log_file) return # 歌曲所属专辑 album_info = net_song['album'] # 包括字段 id name size artist album_name = album_info['name'] # print(album) # 获取歌曲信息 # print("========歌曲信息=======") song_info = net.songs_detail([net_song['id']])[0] album_pic = song_info['al']['picUrl'] # 专辑的图片 # print(song_lyric) # 获取专辑信息 album_tracks = net.album(album_info['id']) g_album_db[album_info['id']] = album_tracks # net_tags = { "ALBUM": album_info['name'], } if artist_info['briefDesc']: net_tags['comment'] = artist_info['briefDesc'] # 专辑发布时间 if 'publishTime' in album_info: publish_time = datetime.fromtimestamp(album_info['publishTime'] // 1000) net_tags['date'] = publish_time.strftime("%Y-%m-%d") net_tags['year'] = publish_time.strftime("%Y") album_year = publish_time.strftime("%Y") else: album_year = None # 专辑歌曲数量,以及本歌曲在第几 if len(album_tracks): net_tags['TRACKTOTAL'] = str(len(album_tracks)) track_number = get_track_number_from_album(net_song['id'], album_tracks) if track_number is not None: net_tags['TRACKNUMBER'] = str(track_number) # 获取歌词 song_lyric = net.song_lyric(net_song['id']) net_tags['Lyrics'] = '\n'.join(song_lyric), net_tags['wangyi'] = [ json.dumps({ 'song_id': net_song['id'], 'artist_id': artist_id, 'ablum_id': album_info['id'], }) ] new_artist_path = os.path.join(save_path, artist_name) if album_name is not None: new_album_path = os.path.join(new_artist_path, album_name) else: new_album_path = new_artist_path if album_year is not None: new_album_path = new_album_path # if not os.path.exists(new_album_path): os.makedirs(new_album_path, exist_ok=True) new_song_path = os.path.join(new_album_path, os.path.split(local_song_path)[-1]) # print(new_song_path) # 保存歌词 save_lrc(new_song_path, song_lyric) # 复制音频文件 if not os.path.exists(new_song_path): # copy_file(local_song_path, new_song_path) shutil.move(local_song_path, new_song_path) download_img(artist_pic, new_artist_path, 'folder') download_img(artist_pic, new_artist_path, 'fanart') download_img(album_pic, new_album_path, 'cover') save_tag(new_song_path, net_tags) # 生成nfo文件 album_nfo = { 'title': album_info['name'], 'artistdesc': artist_info['briefDesc'], 'year': album_year, 'tracks': album_tracks } save_album_nfo(new_album_path, album_nfo) # pprint.pprint(net.search(keywords="那英", stype=100)) # pprint.pprint(album_desc[0]) print("")