Ejemplo n.º 1
0
def main():
    fail_list = []

    if os.path.exists(LOVE_PLAYLIST_FILE):
        with open(LOVE_PLAYLIST_FILE, encoding='utf-8') as lpf:
            love_playlist = json.load(lpf)
    else:
        api = NetEase()
        user = api.login(USERNAME, md5(PASSWORD).hexdigest())
        print(user)
        user_id = user['account']['id']
        prase = Parse()
        ps = prase.playlists(api.user_playlist(user_id))

        love_playlist_id = [m for m in ps if
                            m['playlist_name'] == f"{m['creator_name']}喜欢的音乐"][
            0]['playlist_id']
        print(love_playlist_id)
        love_playlist = api.playlist_detail(love_playlist_id)
        with open(LOVE_PLAYLIST_FILE, mode='w', encoding='utf-8') as lpf:
            json.dump(love_playlist, lpf)
    for i, d in enumerate(love_playlist):
        try:
            search_and_download_music(d['name'], d['ar'][0]['name'],
                                      d['al']['name'])
            print(f'{i+1}/{len(love_playlist)} {d["name"]} 下载完成')
        except Exception as e:
            fail_list.append(d["name"])
            print(f'{i+1}/{len(love_playlist)} {d["name"]} 下载失败!!!!:{e}')
    print('-' * 20, '下载失败的歌曲', '-' * 20)
    print(fail_list)
Ejemplo n.º 2
0
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)