Esempio n. 1
0
def download_song_by_song(song, download_folder, sub_folder=True):
    # get song info
    api = CloudApi()
    song_id = song['id']
    song_name = format_string(song['name'])
    artist_name = format_string(song['artists'][0]['name'])
    album_name = format_string(song['album']['name'])

    # update song file name by config
    song_file_name = '{}.mp3'.format(song_name)
    switcher_song = {
        1: song_file_name,
        2: '{} - {}.mp3'.format(artist_name, song_name),
        3: '{} - {}.mp3'.format(song_name, artist_name)
    }
    song_file_name = switcher_song.get(config.SONG_NAME_TYPE, song_file_name)

    # update song folder name by config, if support sub folder
    if sub_folder:
        switcher_folder = {
            1: download_folder,
            2: os.path.join(download_folder, artist_name),
            3: os.path.join(download_folder, artist_name, album_name),
        }
        song_download_folder = switcher_folder.get(config.SONG_FOLDER_TYPE,
                                                   download_folder)
    else:
        song_download_folder = download_folder

    # download song
    song_url = api.get_song_url(song_id)
    if song_url is None:
        print('Song <<{}>> is not available due to copyright issue!'.format(
            song_name))
        return
    is_already_download = download_file(song_url, song_file_name,
                                        song_download_folder)
    if is_already_download:
        try:
            print('Mp3 file already download:', song_file_name)
        except:
            print('Mp3 file already download:',
                  song_file_name.encode("GBK", 'ignore'))
        return

    # download cover
    cover_url = song['album']['blurPicUrl']
    cover_file_name = 'cover_{}.jpg'.format(song_id)
    download_file(cover_url, cover_file_name, song_download_folder)

    # resize cover
    resize_img(os.path.join(song_download_folder, cover_file_name))

    # add metadata for song
    song_file_path = os.path.join(song_download_folder, song_file_name)
    cover_file_path = os.path.join(song_download_folder, cover_file_name)
    add_metadata_to_song(song_file_path, cover_file_path, song)

    # delete cover file
    os.remove(cover_file_path)
def download_song_by_song(song, download_folder, gener, sub_folder=True):
    # get song info
    api = CloudApi()
    song_id = song['id']
    song_name = re.sub(r'[\\/:*?"<>|]', ' ',
                       song['name'])  # Replace illegal character with ' '
    artist_name = song['artists'][0]['name']
    album_name = song['album']['name']
    lyric = api.get_lyric(song_id)

    # update song file name by config
    song_file_name = '{}.mp3'.format(song_name)
    switcher_song = {
        1: song_file_name,
        2: '{} - {}.mp3'.format(artist_name, song_name),
        3: '{} - {}.mp3'.format(song_name, artist_name)
    }
    song_file_name = switcher_song.get(config.SONG_NAME_TYPE, song_file_name)

    # update song folder name by config, if support sub folder
    if sub_folder:
        switcher_folder = {
            1: download_folder,
            2: os.path.join(download_folder, artist_name),
            3: os.path.join(download_folder, artist_name, album_name),
        }
        song_download_folder = switcher_folder.get(config.SONG_FOLDER_TYPE,
                                                   download_folder)
    else:
        song_download_folder = download_folder

    # download song
    song_url = api.get_song_url(song_id)
    if song_url is None:
        print('Song <<{}>> is not available due to copyright issue!'.format(
            song_name))
        return
    is_already_download = download_file(song_url, song_file_name,
                                        song_download_folder)
    if is_already_download:
        print('Mp3 file already download:', song_file_name)
        return

    # download cover
    cover_url = song['album']['blurPicUrl']
    cover_file_name = 'cover_{}.jpg'.format(song_id)
    download_file(cover_url, cover_file_name, song_download_folder)
    write_file(lyric, song_file_name.replace(".mp3", ".lrc"),
               song_download_folder)
    # add metadata for song
    song_file_path = os.path.join(song_download_folder, song_file_name)
    cover_file_path = os.path.join(song_download_folder, cover_file_name)
    add_metadata_to_song(song_file_path, cover_file_path, song, gener, lyric)

    # delete cover file
    os.remove(cover_file_path)
Esempio n. 3
0
def download_song_by_song(song, download_folder, sub_folder=True):
    # get song info
    api = CloudApi()
    song_id = song['id']
    song_name = format_string(song['name'])
    if song_name is None:
        print('no song_name')
    artist_name = format_string(song['artists'][0]['name'])
    if artist_name is None:
        print('no artist_name')
    album_name = format_string(song['album']['name'])
    if album_name is None:
        print('no album_name')
        album_name = ""

    # update song file name by config
    song_file_name = '{}.mp3'.format(song_name)
    switcher_song = {
        1: song_file_name,
        2: '{} - {}.mp3'.format(artist_name, song_name),
        3: '{} - {}.mp3'.format(song_name, artist_name)
    }
    song_file_name = switcher_song.get(config.SONG_NAME_TYPE, song_file_name)

    lrc_file_name = '{}.lrc'.format(song_name)
    switcher_song = {
        1: lrc_file_name,
        2: '{} - {}.lrc'.format(artist_name, song_name),
        3: '{} - {}.lrc'.format(song_name, artist_name)
    }
    lrc_file_name = switcher_song.get(config.SONG_NAME_TYPE, lrc_file_name)
    # update song folder name by config, if support sub folder
    if sub_folder:
        switcher_folder = {
            1: download_folder,
            2: os.path.join(download_folder, artist_name),
            3: os.path.join(download_folder, artist_name, album_name),
        }
        song_download_folder = switcher_folder.get(config.SONG_FOLDER_TYPE,
                                                   download_folder)
    else:
        song_download_folder = download_folder

    # download song
    song_url = api.get_song_url(song_id)
    if song_url is None:
        print('Song <<{}>> is not available due to copyright issue!'.format(
            song_name))
        return
    is_already_download = download_file(song_url, song_file_name,
                                        song_download_folder)
    if is_already_download:
        print('Mp3 file already download:', song_file_name)
        return

    #download_lrc
    lrc = api.get_lrc(song_id)
    if 'no lrc' in lrc:
        print(lrc_file_name, 'is no')
    else:
        lrc = lrc.encode('utf-8')
        if not os.path.exists(song_download_folder):
            os.makedirs(song_download_folder)
        file_path = os.path.join(song_download_folder, lrc_file_name)
        with open(file_path, 'ab+') as f:
            f.write(lrc)

    #download_tran lrc
    lrc = api.get_lrc_tran(song_id)
    if 'no tran lrc' in lrc:
        print(lrc_file_name, 'tran is no')
    else:
        lrc = lrc.encode('utf-8')
        if not os.path.exists(song_download_folder):
            os.makedirs(song_download_folder)
        file_path = os.path.join(song_download_folder, lrc_file_name)
        with open(file_path, 'ab+') as f:
            f.write(lrc)

    # download cover
    cover_url = song['album']['blurPicUrl']
    if cover_url is None:
        print('no cover_url')
    else:
        cover_file_name = 'cover_{}.jpg'.format(song_id)
        download_file(cover_url, cover_file_name, song_download_folder)

        # resize cover
        resize_img(os.path.join(song_download_folder, cover_file_name))

        # add metadata for song
        song_file_path = os.path.join(song_download_folder, song_file_name)
        cover_file_path = os.path.join(song_download_folder, cover_file_name)
        add_metadata_to_song(song_file_path, cover_file_path, song)

        # delete cover file
        os.remove(cover_file_path)
Esempio n. 4
0
def download_song_by_id(song_id, download_folder, sub_folder=True):
    # get song info
    api = CloudApi()
    song = api.get_song(song_id)
    download_song_by_song(song, download_folder, sub_folder)
Esempio n. 5
0
import re
from lxml import etree
from pprint import pprint

from urllib.parse import urlparse, parse_qs
from ncm import config
from ncm.api import CloudApi
from ncm.api import WangYiYunSpider
from ncm.downloader import download_song_by_id
from ncm.downloader import download_song_by_song
from ncm.downloader import format_string
from ncm.downloader import download_file

# load the config first
config.load_config()
api = CloudApi()


def download_hot_songs(artist_id):
    songs = api.get_hot_songs(artist_id)
    folder_name = format_string(songs[0]['artists'][0]['name']) + ' - hot50'
    folder_path = os.path.join(config.DOWNLOAD_DIR, folder_name)
    download_count = config.DOWNLOAD_HOT_MAX if (0 < config.DOWNLOAD_HOT_MAX < 50) else config.DOWNLOAD_HOT_MAX_DEFAULT
    for i, song in zip(range(download_count), songs):
        print('{}: {}'.format(i + 1, song['name']))
        download_song_by_song(song, folder_path, False)


def download_album_songs(album_id):
    songs = api.get_album_songs(album_id)
    folder_name = format_string(songs[0]['album']['name']) + ' - album'
Esempio n. 6
0
def get_song_info_by_id(song_id):
    api = CloudApi()
    song = api.get_song(song_id)
    return song