예제 #1
0
        def tracking2(infos, datas):
            image = utils.choose_img(infos["ALB_PICTURE"])
            datas["image"] = image
            song = "{} - {}".format(datas["music"], datas["artist"])

            if not not_interface:
                print("Downloading: %s" % song)

            try:
                nams = check_quality_song(infos, datas)
            except exceptions.TrackNotFound:
                try:
                    ids = utils.not_found(song, datas["music"])
                except IndexError:
                    raise exceptions.TrackNotFound("Track not found: %s" %
                                                   song)

                json_data = {"sng_id": ids}

                infos = get_infos(self.get_song_data, json_data)
                nams = check_quality_song(infos, datas)

            return nams
예제 #2
0
    def download(self,
                 link,
                 name,
                 quality,
                 recursive_quality,
                 recursive_download,
                 datas,
                 not_interface,
                 directory,
                 zips=False):
        if not quality in qualities:
            raise exceptions.QualityNotFound(
                "The qualities have to be FLAC or MP3_320 or MP3_256 or MP3_128"
            )

        self.token = self.get_api("deezer.getUserData")['checkForm']

        def get_infos(method, json):
            infos = None

            while not "MD5_ORIGIN" in str(infos):
                infos = self.get_api(method, self.token, json)

            return infos

        def ultimatum(infos, datas, name, quality):
            extension = ".mp3"
            ids = infos['SNG_ID']
            key = "FILESIZE_" + quality

            if int(infos[key]) > 0 and quality == "FLAC":
                quality = "9"
                extension = ".flac"
                qualit = "FLAC"

            elif int(infos[key]) > 0 and quality == "MP3_320":
                quality = "3"
                qualit = "320"

            elif int(infos[key]) > 0 and quality == "MP3_256":
                quality = "5"
                qualit = "256"

            elif int(infos[key]) > 0 and quality == "MP3_128":
                quality = "1"
                qualit = "128"

            else:
                if not recursive_quality:
                    raise exceptions.QualityNotFound(
                        "The quality chosen can't be downloaded")

                for a in qualities:
                    if int(infos['FILESIZE_' + a]) > 0:
                        quality = qualities[a]['quality']
                        extension = qualities[a]['extension']
                        qualit = qualities[a]['qualit']
                        break
                    else:
                        if a == "MP3_128":
                            raise exceptions.TrackNotFound(
                                "There isn't any quality avalaible for download this song: %s"
                                % name)

            name += " ({}){}".format(qualit, extension)

            if os.path.isfile(name):
                if recursive_download:
                    return name

                ans = input(
                    "Track %s already exists, do you want to redownload it?(y or n):"
                    % name)

                if not ans in answers:
                    return name

            try:
                md5 = infos['FALLBACK']['MD5_ORIGIN']
            except KeyError:
                md5 = infos['MD5_ORIGIN']

            hashs = genurl(md5, quality, ids, infos['MEDIA_VERSION'])

            try:
                crypt = request(
                    "https://e-cdns-proxy-{}.dzcdn.net/mobile/1/{}".format(
                        md5[0], hashs))
            except IndexError:
                raise exceptions.TrackNotFound("Track: %s not found:(" % name)

            if len(crypt.content) == 0:
                raise exceptions.TrackNotFound(
                    "Something is wrong with %s :(" % name)

            encry = open(name, "wb")
            encry.write(crypt.content)
            encry.close()
            decry = open(name, "wb")

            decryptfile(crypt.iter_content(2048), calcbfkey(ids), decry)

            write_tags(name, add_more_tags(datas, infos, ids))

            return name

        def add_more_tags(datas, infos, ids):
            json = {"sng_id": ids}

            try:
                datas['author'] = " & ".join(
                    infos['SNG_CONTRIBUTORS']['author'])
            except:
                datas['author'] = ""

            try:
                datas['composer'] = " & ".join(
                    infos['SNG_CONTRIBUTORS']['composer'])
            except:
                datas['composer'] = ""

            try:
                datas['lyricist'] = " & ".join(
                    infos['SNG_CONTRIBUTORS']['lyricist'])
            except:
                datas['lyricist'] = ""

            try:
                datas['version'] = infos['VERSION']
            except KeyError:
                datas['version'] = ""

            need = self.get_api("song.getLyrics", self.token, json)

            try:
                datas['lyric'] = need['LYRICS_TEXT']
                datas['copyright'] = need['LYRICS_COPYRIGHTS']
                datas['lyricist'] = need['LYRICS_WRITERS']
            except KeyError:
                datas['lyric'] = ""
                datas['copyright'] = ""
                datas['lyricist'] = ""

            return datas

        ids = link.split("/")[-1]

        if "track" in link:
            json = {"sng_id": ids}

            infos = get_infos(method_get_track, json)
            image = choose_img(infos['ALB_PICTURE'])
            datas['image'] = image
            song = "{} - {}".format(datas['music'], datas['artist'])

            if not not_interface:
                print("Downloading: %s" % song)

            try:
                nams = ultimatum(infos, datas, name, quality)
            except exceptions.TrackNotFound:
                url = request(
                    "https://api.deezer.com/search/track/?q=%s" %
                    song.replace("#", ""), True).json()

                try:
                    for b in range(url['total'] + 1):
                        if url['data'][b]['title'] == datas['music'] or datas[
                                'music'] in url['data'][b]['title_short']:
                            URL = url['data'][b]['link']
                            break
                except IndexError:
                    raise exceptions.TrackNotFound("Track not found: %s" %
                                                   song)

                json = {"sng_id": URL.split("/")[-1]}

                infos = get_infos(method_get_track, json)
                nams = ultimatum(infos, datas, name, quality)

            return nams

        nams = []
        detas = {}
        zip_name = ""
        quali = ""

        json = {"alb_id": ids, "nb": -1}

        infos = get_infos(method_get_album, json)['data']

        image = choose_img(infos[0]['ALB_PICTURE'])

        detas['image'] = image
        detas['album'] = datas['album']
        detas['year'] = datas['year']
        detas['genre'] = datas['genre']
        detas['ar_album'] = datas['ar_album']
        detas['label'] = datas['label']

        for a in tqdm(range(len(name)), disable=not_interface):
            detas['music'] = datas['music'][a]
            detas['artist'] = datas['artist'][a]
            detas['tracknum'] = datas['tracknum'][a]
            detas['discnum'] = datas['discnum'][a]
            detas['bpm'] = datas['bpm'][a]
            detas['gain'] = datas['gain'][a]
            detas['duration'] = datas['duration'][a]
            detas['isrc'] = datas['isrc'][a]
            song = "{} - {}".format(detas['music'], detas['artist'])

            try:
                nams.append(ultimatum(infos[a], detas, name[a], quality))
            except exceptions.TrackNotFound:
                try:
                    url = request(
                        "https://api.deezer.com/search/track/?q=%s" %
                        song.replace("#", ""), True).json()

                    for b in range(url['total'] + 1):
                        if url['data'][b]['title'] == detas['music'] or detas[
                                'music'] in url['data'][b]['title_short']:
                            URL = url['data'][b]['link']
                            break

                    json = {"sng_id": URL.split("/")[-1]}

                    nams.append(
                        ultimatum(get_infos(method_get_track, json), detas,
                                  name[a], quality))
                except (exceptions.TrackNotFound, IndexError,
                        exceptions.InvalidLink):
                    nams.append(name[a])
                    print("Track not found: %s :(" % song)
                    continue

            quali = (nams[a].split("(")[-1].split(")")[0])

        if zips:
            zip_name = ("%s%s (%s).zip" %
                        (directory, directory.split("/")[-2], quali))

            create_zip(zip_name, nams)

        return nams, zip_name
예제 #3
0
    def download(
        self,
        link,
        details,
        recursive_quality=None,
        recursive_download=None,
        not_interface=None,
        zips=False,
    ):
        if not details["quality"] in self.qualities:
            raise exceptions.QualityNotFound(
                "The qualities have to be FLAC or MP3_320 or MP3_256 or MP3_128"
            )

        self.token = self.get_api(self.get_user_data)["checkForm"]
        ids = utils.get_ids(link)
        datas = details["datas"]
        quality = details["quality"]
        output = details["output"]

        def get_infos(method, json_data):
            infos = self.get_api(method, self.token, json_data)
            return infos

        def check_quality_song(infos, datas):
            ids = infos["SNG_ID"]
            num_quality = self.qualities[quality]["n_quality"]
            file_format = self.qualities[quality]["f_format"]
            song_quality = self.qualities[quality]["s_quality"]
            song_md5, version = utils.check_md5_song(infos)
            song_hash = download_utils.genurl(song_md5, num_quality, ids,
                                              version)

            try:
                crypted_audio = utils.song_exist(song_md5[0], song_hash)
            except (IndexError, exceptions.TrackNotFound):
                if not recursive_quality:
                    raise exceptions.QualityNotFound(
                        "The quality chosen can't be downloaded")

                for a in self.qualities:
                    if details["quality"] == a:
                        continue

                    num_quality = self.qualities[a]["n_quality"]
                    file_format = self.qualities[a]["f_format"]
                    song_quality = self.qualities[a]["s_quality"]
                    song_hash = download_utils.genurl(song_md5, num_quality,
                                                      ids,
                                                      infos["MEDIA_VERSION"])

                    try:
                        crypted_audio = utils.song_exist(
                            song_md5[0], song_hash)
                    except exceptions.TrackNotFound:
                        raise exceptions.TrackNotFound(
                            "Error with this song %s" % link)

            album = utils.var_excape(datas["album"])

            directory = "%s%s %s/" % ("%s/" % output, album, datas["upc"])

            name = "%s%s CD %s TRACK %s" % (
                directory,
                album,
                datas["discnum"],
                datas["tracknum"],
            )

            utils.check_dir(directory)
            name += " ({}){}".format(song_quality, file_format)

            if isfile(name):
                if recursive_download:
                    return name

                ans = input(
                    "Track %s already exists, do you want to redownload it?(y or n):"
                    % name)

                if not ans in answers:
                    return name

            decrypted_audio = open(name, "wb")

            download_utils.decryptfile(
                crypted_audio.iter_content(2048),
                download_utils.calcbfkey(ids),
                decrypted_audio,
            )

            utils.write_tags(name, add_more_tags(datas, infos, ids))

            return name

        def add_more_tags(datas, infos, ids):
            json_data = {"sng_id": ids}

            try:
                datas["author"] = " & ".join(
                    infos["SNG_CONTRIBUTORS"]["author"])
            except:
                datas["author"] = ""

            try:
                datas["composer"] = " & ".join(
                    infos["SNG_CONTRIBUTORS"]["composer"])
            except:
                datas["composer"] = ""

            try:
                datas["lyricist"] = " & ".join(
                    infos["SNG_CONTRIBUTORS"]["lyricist"])
            except:
                datas["lyricist"] = ""

            try:
                datas["version"] = infos["VERSION"]
            except KeyError:
                datas["version"] = ""

            need = get_infos(self.get_lyric, json_data)

            try:
                datas["lyric"] = need["LYRICS_TEXT"]
                datas["copyright"] = need["LYRICS_COPYRIGHTS"]
                datas["lyricist"] = need["LYRICS_WRITERS"]
            except KeyError:
                datas["lyric"] = ""
                datas["copyright"] = ""
                datas["lyricist"] = ""

            return datas

        def tracking2(infos, datas):
            image = utils.choose_img(infos["ALB_PICTURE"])
            datas["image"] = image
            song = "{} - {}".format(datas["music"], datas["artist"])

            if not not_interface:
                print("Downloading: %s" % song)

            try:
                nams = check_quality_song(infos, datas)
            except exceptions.TrackNotFound:
                try:
                    ids = utils.not_found(song, datas["music"])
                except IndexError:
                    raise exceptions.TrackNotFound("Track not found: %s" %
                                                   song)

                json_data = {"sng_id": ids}

                infos = get_infos(self.get_song_data, json_data)
                nams = check_quality_song(infos, datas)

            return nams

        if "track" in link:
            json_data = {"sng_id": ids}

            infos = get_infos(self.get_song_data, json_data)
            nams = tracking2(infos, datas)
            return nams

        zip_name = ""

        if "album" in link:
            nams = []
            detas = {}
            quali = ""

            json_data = {"alb_id": ids, "nb": -1}

            infos = get_infos(self.get_album, json_data)["data"]

            image = utils.choose_img(infos[0]["ALB_PICTURE"])

            detas["image"] = image
            detas["album"] = datas["album"]
            detas["year"] = datas["year"]
            detas["genre"] = datas["genre"]
            detas["ar_album"] = datas["ar_album"]
            detas["label"] = datas["label"]
            detas["upc"] = datas["upc"]

            t = tqdm(range(len(infos)),
                     desc=detas["album"],
                     disable=not_interface)

            for a in t:
                detas["music"] = datas["music"][a]
                detas["artist"] = datas["artist"][a]
                detas["tracknum"] = datas["tracknum"][a]
                detas["discnum"] = datas["discnum"][a]
                detas["bpm"] = datas["bpm"][a]
                detas["duration"] = datas["duration"][a]
                detas["isrc"] = datas["isrc"][a]
                song = "{} - {}".format(detas["music"], detas["artist"])
                t.set_description_str(song)

                try:
                    nams.append(check_quality_song(infos[a], detas))
                except exceptions.TrackNotFound:
                    try:
                        ids = utils.not_found(song, detas["music"])

                        json = {"sng_id": ids}

                        nams.append(
                            check_quality_song(
                                get_infos(self.get_song_data, json), detas))
                    except (exceptions.TrackNotFound, IndexError):
                        nams.append(song)
                        print("Track not found: %s :(" % song)
                        continue

                quali = nams[a].split("(")[-1].split(")")[0]

            if zips:
                album = utils.var_excape(datas["album"])

                directory = "%s%s %s/" % ("%s/" % output, album, datas["upc"])

                zip_name = "%s%s (%s).zip" % (directory, album, quali)

                try:
                    utils.create_zip(zip_name, nams)
                except FileNotFoundError:
                    raise exceptions.QualityNotFound(
                        'Can\'t download album "{}" in {} quality'.format(
                            album, details["quality"]))

        elif "playlist" in link:
            json_data = {"playlist_id": ids, "nb": -1}

            infos = get_infos(methods.method_get_playlist_data,
                              json_data)["data"]
            nams = []

            for a in range(len(infos)):
                try:
                    nams.append(tracking2(infos[a], datas[a]))
                except TypeError:
                    c = infos[a]
                    song = "{} - {}".format(c["SNG_TITLE"], c["ART_NAME"])
                    nams.append("Track not found")

            quali = "ALL"

            if zips:
                zip_name = "%s %s (%s).zip" % (
                    "%s/playlist" % output,
                    ids,
                    quali,
                )

                utils.create_zip(zip_name, nams)

        return nams, zip_name
예제 #4
0
    def download(self,
                 link,
                 details,
                 recursive_quality=None,
                 recursive_download=None,
                 not_interface=None,
                 zips=False):
        if not details['quality'] in self.qualities:
            raise exceptions.QualityNotFound(
                "The qualities have to be FLAC or MP3_320 or MP3_256 or MP3_128"
            )

        self.token = self.get_api(self.get_user_data)['checkForm']
        ids = utils.get_ids(link)
        datas = details['datas']
        quality = details['quality']
        output = details['output']

        def get_infos(method, json_data):
            infos = self.get_api(method, self.token, json_data)
            return infos

        def check_quality_song(infos, datas):
            ids = infos['SNG_ID']
            num_quality = self.qualities[quality]['n_quality']
            file_format = self.qualities[quality]['f_format']
            song_quality = self.qualities[quality]['s_quality']
            song_md5, version = utils.check_md5_song(infos)
            song_hash = download_utils.genurl(song_md5, num_quality, ids,
                                              version)

            try:
                crypted_audio = utils.song_exist(song_md5[0], song_hash)
            except (IndexError, exceptions.TrackNotFound):
                if not recursive_quality:
                    raise exceptions.QualityNotFound(
                        "The quality chosen can't be downloaded")

                for a in self.qualities:
                    if details['quality'] == a:
                        continue

                    num_quality = self.qualities[a]['n_quality']
                    file_format = self.qualities[a]['f_format']
                    song_quality = self.qualities[a]['s_quality']
                    song_hash = download_utils.genurl(song_md5, num_quality,
                                                      ids,
                                                      infos['MEDIA_VERSION'])

                    try:
                        crypted_audio = utils.song_exist(
                            song_md5[0], song_hash)
                    except exceptions.TrackNotFound:
                        raise exceptions.TrackNotFound(
                            "Error with this song %s" % link)

            album = utils.var_excape(datas['album'])

            directory = ("%s%s %s/" % ("%s/" % output, album, datas['upc']))

            name = ("%s%s CD %s TRACK %s" %
                    (directory, album, datas['discnum'], datas['tracknum']))

            utils.check_dir(directory)
            name += " ({}){}".format(song_quality, file_format)

            if isfile(name):
                if recursive_download:
                    return name

                ans = input(
                    "Track %s already exists, do you want to redownload it?(y or n):"
                    % name)

                if not ans in answers:
                    return name

            decrypted_audio = open(name, "wb")

            download_utils.decryptfile(crypted_audio.iter_content(2048),
                                       download_utils.calcbfkey(ids),
                                       decrypted_audio)

            utils.write_tags(name, add_more_tags(datas, infos, ids))

            return name

        def add_more_tags(datas, infos, ids):
            json_data = {"sng_id": ids}

            try:
                datas['author'] = " & ".join(
                    infos['SNG_CONTRIBUTORS']['author'])
            except:
                datas['author'] = ""

            try:
                datas['composer'] = " & ".join(
                    infos['SNG_CONTRIBUTORS']['composer'])
            except:
                datas['composer'] = ""

            try:
                datas['lyricist'] = " & ".join(
                    infos['SNG_CONTRIBUTORS']['lyricist'])
            except:
                datas['lyricist'] = ""

            try:
                datas['version'] = infos['VERSION']
            except KeyError:
                datas['version'] = ""

            need = get_infos(self.get_lyric, json_data)

            try:
                datas['lyric'] = need['LYRICS_TEXT']
                datas['copyright'] = need['LYRICS_COPYRIGHTS']
                datas['lyricist'] = need['LYRICS_WRITERS']
            except KeyError:
                datas['lyric'] = ""
                datas['copyright'] = ""
                datas['lyricist'] = ""

            return datas

        def tracking2(infos, datas):
            image = utils.choose_img(infos['ALB_PICTURE'])
            datas['image'] = image
            song = "{} - {}".format(datas['music'], datas['artist'])

            if not not_interface:
                print("Downloading: %s" % song)

            try:
                nams = check_quality_song(infos, datas)
            except exceptions.TrackNotFound:
                try:
                    ids = utils.not_found(song, datas['music'])
                except IndexError:
                    raise exceptions.TrackNotFound("Track not found: %s" %
                                                   song)

                json_data = {"sng_id": ids}

                infos = get_infos(self.get_song_data, json_data)
                nams = check_quality_song(infos, datas)

            return nams

        if "track" in link:
            json_data = {"sng_id": ids}

            infos = get_infos(self.get_song_data, json_data)
            nams = tracking2(infos, datas)
            return nams

        elif "album" in link:
            nams = []
            detas = {}
            zip_name = ""
            quali = ""

            json_data = {"alb_id": ids, "nb": -1}

            infos = get_infos(self.get_album, json_data)['data']

            image = utils.choose_img(infos[0]['ALB_PICTURE'])

            detas['image'] = image
            detas['album'] = datas['album']
            detas['year'] = datas['year']
            detas['genre'] = datas['genre']
            detas['ar_album'] = datas['ar_album']
            detas['label'] = datas['label']
            detas['upc'] = datas['upc']

            t = tqdm(range(len(infos)),
                     desc=detas['album'],
                     disable=not_interface)

            for a in t:
                detas['music'] = datas['music'][a]
                detas['artist'] = datas['artist'][a]
                detas['tracknum'] = datas['tracknum'][a]
                detas['discnum'] = datas['discnum'][a]
                detas['bpm'] = datas['bpm'][a]
                detas['duration'] = datas['duration'][a]
                detas['isrc'] = datas['isrc'][a]
                song = "{} - {}".format(detas['music'], detas['artist'])
                t.set_description_str(song)

                try:
                    nams.append(check_quality_song(infos[a], detas))
                except exceptions.TrackNotFound:
                    try:
                        ids = utils.not_found(song, detas['music'])

                        json = {"sng_id": ids}

                        nams.append(
                            check_quality_song(
                                get_infos(self.get_song_data, json), detas))
                    except (exceptions.TrackNotFound, IndexError):
                        nams.append(song)
                        print("Track not found: %s :(" % song)
                        continue

                quali = (nams[a].split("(")[-1].split(")")[0])

            if zips:
                directory = ("%s%s %s/" %
                             ("%s/" % output, datas['album'], datas['upc']))

                zip_name = ("%s%s (%s).zip" %
                            (directory, datas['album'], quali))

                utils.create_zip(zip_name, nams)

        elif "playlist" in link:
            json_data = {"playlist_id": ids, "nb": -1}

            infos = get_infos(methods.method_get_playlist_data,
                              json_data)['data']
            nams = []

            for a in range(len(infos)):
                nams.append(tracking2(infos[a], datas[a]))

            quali = "ALL"

            if zips:
                zip_name = ("%s %s (%s).zip" %
                            ("%s/playlist" % output, ids, quali))

                utils.create_zip(zip_name, nams)

        return nams, zip_name