<s> import sys <EOL> import config , util , logging , log , downloader <EOL> import xiami as xm <EOL> import netease <EOL> import re <EOL> import hist_handler <EOL> from threadpool import ThreadPool <EOL> from time import sleep <EOL> from os import path <EOL> from threadpool import Terminate_Watcher <EOL> from proxypool import ProxyPool <EOL> if config . LANG . upper ( ) == '<STR_LIT>' : <EOL> import i18n . msg_cn as msgTxt <EOL> else : <EOL> import i18n . msg_en as msgTxt <EOL> LOG = log . get_logger ( "<STR_LIT>" ) <EOL> total_songs = [ ] <EOL> total = <NUM_LIT:0> <EOL> done = <NUM_LIT:0> <EOL> fmt_parsing = msgTxt . fmt_parsing <EOL> fmt_has_song_nm = msgTxt . fmt_has_song_nm <EOL> fmt_single_song = msgTxt . fmt_single_song <EOL> border = log . hl ( u'<STR_LIT:%s>' % ( '<STR_LIT:=>' * <NUM_LIT> ) , '<STR_LIT>' ) <EOL> pat_xm = r'<STR_LIT>' <EOL> pat_163 = r'<STR_LIT>' <EOL> ppool = None <EOL> xiami_obj = None <EOL> def __init_xiami_obj ( option ) : <EOL> global xiami_obj <EOL> if not xiami_obj : <EOL> xiami_obj = xm . Xiami ( config . XIAMI_LOGIN_EMAIL , config . XIAMI_LOGIN_PASSWORD , option ) <EOL> def shall_I_begin ( option ) : <EOL> Terminate_Watcher ( ) <EOL> global ppool , xiami_obj <EOL> if option . need_proxy_pool : <EOL> LOG . info ( msgTxt . init_proxypool ) <EOL> ppool = ProxyPool ( ) <EOL> option . proxies = ppool <EOL> LOG . info ( msgTxt . fmt_init_proxypool_done % len ( ppool . proxies ) ) <EOL> m163 = netease . Netease ( option ) <EOL> if option . inFile : <EOL> from_file ( m163 , option ) <EOL> elif re . match ( pat_xm , option . inUrl ) : <EOL> __init_xiami_obj ( option ) <EOL> from_url_xm ( xiami_obj , option . inUrl ) <EOL> elif re . match ( pat_163 , option . inUrl ) : <EOL> from_url_163 ( m163 , option . inUrl ) <EOL> print border <EOL> skipped_songs = [ ] <EOL> skipped_hists = [ ] <EOL> dl_songs = [ ] <EOL> if option . incremental_dl : <EOL> skipped_songs , skipped_hists = hist_handler . filter_songs ( total_songs ) <EOL> LOG . warning ( msgTxt . fmt_skip_dl_nm % len ( skipped_songs ) ) <EOL> dl_songs = [ song for song in total_songs if song not in skipped_songs ] <EOL> dl_num = len ( dl_songs ) <EOL> skip_num = len ( skipped_songs ) <EOL> output_num = '<STR_LIT>' % dl_num if not skip_num else "<STR_LIT>" % ( dl_num + skip_num , skip_num , dl_num ) <EOL> if len ( dl_songs ) : <EOL> LOG . info ( msgTxt . fmt_total_dl_nm % output_num ) <EOL> sleep ( <NUM_LIT:3> ) <EOL> downloader . start_download ( dl_songs , skipped_hists ) <EOL> else : <EOL> LOG . warning ( msgTxt . no_dl_task ) <EOL> def from_url_163 ( m163 , url , verbose = True ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( '<STR_LIT>' % url ) <EOL> msg = u'<STR_LIT>' <EOL> if '<STR_LIT>' in url : <EOL> song = netease . NeteaseSong ( m163 , url = url ) <EOL> total_songs . append ( song ) <EOL> msg = fmt_parsing % ( m163_url_abbr ( url ) , msgTxt . song , song . song_name ) <EOL> elif '<STR_LIT>' in url : <EOL> album = netease . NeteaseAlbum ( m163 , url ) <EOL> total_songs . extend ( album . songs ) <EOL> msgs = [ fmt_parsing % ( m163_url_abbr ( url ) , msgTxt . album , album . artist_name + u'<STR_LIT>' + album . album_name ) ] <EOL> if verbose : <EOL> for s in album . songs : <EOL> msgs . append ( fmt_single_song % s . song_name ) <EOL> msg = u'<STR_LIT>' . join ( msgs ) <EOL> else : <EOL> msgs . append ( fmt_has_song_nm % len ( album . songs ) ) <EOL> msg = u'<STR_LIT>' . join ( msgs ) <EOL> elif '<STR_LIT>' in url : <EOL> playlist = netease . NeteasePlayList ( m163 , url ) <EOL> total_songs . extend ( playlist . songs ) <EOL> msgs = [ fmt_parsing % ( m163_url_abbr ( url ) , msgTxt . playlist , playlist . playlist_name ) ] <EOL> if verbose : <EOL> for s in playlist . songs : <EOL> msgs . append ( fmt_single_song % s . song_name ) <EOL> msg = u'<STR_LIT>' . join ( msgs ) <EOL> else : <EOL> msgs . append ( fmt_has_song_nm % len ( playlist . songs ) ) <EOL> msg = u'<STR_LIT>' . join ( msgs ) <EOL> elif '<STR_LIT>' in url : <EOL> topsong = netease . NeteaseTopSong ( m163 , url ) <EOL> total_songs . extend ( topsong . songs ) <EOL> msgs = [ fmt_parsing % ( m163_url_abbr ( url ) , msgTxt . artistTop , topsong . artist_name ) ] <EOL> if verbose : <EOL> for s in topsong . songs : <EOL> msgs . append ( fmt_single_song % s . song_name ) <EOL> msg = u'<STR_LIT>' . join ( msgs ) <EOL> else : <EOL> msgs . append ( fmt_has_song_nm % len ( topsong . songs ) ) <EOL> msg = u'<STR_LIT>' . join ( msgs ) <EOL> global total , done <EOL> done += <NUM_LIT:1> <EOL> pre = ( '<STR_LIT>' % ( done , total ) ) if not verbose else '<STR_LIT>' <EOL> if not msg : <EOL> LOG . error ( msgTxt . fmt_163_unknow_url % ( pre , url ) ) <EOL> else : <EOL> LOG . info ( u'<STR_LIT>' % ( pre , msg ) ) <EOL> def from_url_xm ( xm_obj , url , verbose = True ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( '<STR_LIT>' % url ) <EOL> msg = u'<STR_LIT>' <EOL> if '<STR_LIT>' in url : <EOL> collect = xm . Collection ( xm_obj , url ) <EOL> total_songs . extend ( collect . songs ) <EOL> msgs = [ fmt_parsing % ( xiami_url_abbr ( url ) , msgTxt . collection , collect . collection_name ) ] <EOL> if verbose : <EOL> for s in collect . songs : <EOL> msgs . append ( fmt_single_song % s . song_name ) <EOL> msg = u'<STR_LIT>' . join ( msgs ) <EOL> else : <EOL> msgs . append ( fmt_has_song_nm % len ( collect . songs ) ) <EOL> msg = u'<STR_LIT>' . join ( msgs ) <EOL> elif '<STR_LIT>' in url : <EOL> song = xm . XiamiSong ( xm_obj , url = url ) <EOL> total_songs . append ( song ) <EOL> msg = fmt_parsing % ( xiami_url_abbr ( url ) , msgTxt . song , song . song_name ) <EOL> elif '<STR_LIT>' in url : <EOL> album = xm . Album ( xm_obj , url ) <EOL> total_songs . extend ( album . songs ) <EOL> msgs = [ fmt_parsing % ( xiami_url_abbr ( url ) , msgTxt . album , album . artist_name + u'<STR_LIT>' + album . album_name ) ] <EOL> if verbose : <EOL> for s in album . songs : <EOL> msgs . append ( fmt_single_song % s . song_name ) <EOL> msg = u'<STR_LIT>' . join ( msgs ) <EOL> else : <EOL> msgs . append ( fmt_has_song_nm % len ( album . songs ) ) <EOL> msg = u'<STR_LIT>' . join ( msgs ) <EOL> elif '<STR_LIT>' in url : <EOL> if verbose : <EOL> LOG . warning ( msgTxt . warning_many_collections ) <EOL> fav = xm . Favorite ( xm_obj , url , verbose ) <EOL> total_songs . extend ( fav . songs ) <EOL> msgs = [ fmt_parsing % ( xiami_url_abbr ( url ) , msgTxt . Favorite , '<STR_LIT>' ) ] <EOL> if verbose : <EOL> for s in fav . songs : <EOL> msgs . append ( fmt_single_song % s . song_name ) <EOL> msg = u'<STR_LIT>' . join ( msgs ) <EOL> else : <EOL> msgs . append ( fmt_has_song_nm % len ( fav . songs ) ) <EOL> msg = u'<STR_LIT>' . join ( msgs ) <EOL> elif re . search ( r'<STR_LIT>' , url ) : <EOL> topsong = xm . TopSong ( xm_obj , url ) <EOL> total_songs . extend ( topsong . songs ) <EOL> msgs = [ fmt_parsing % ( xiami_url_abbr ( url ) , msgTxt . artistTop , topsong . artist_name ) ] <EOL> if verbose : <EOL> for s in topsong . songs : <EOL> msgs . append ( fmt_single_song % s . song_name ) <EOL> msg = u'<STR_LIT>' . join ( msgs ) <EOL> else : <EOL> msgs . append ( fmt_has_song_nm % len ( topsong . songs ) ) <EOL> msg = u'<STR_LIT>' . join ( msgs ) <EOL> global total , done <EOL> done += <NUM_LIT:1> <EOL> pre = ( '<STR_LIT>' % ( done , total ) ) if not verbose else '<STR_LIT>' <EOL> if not msg : <EOL> LOG . error ( msgTxt . fmt_xm_unknown_url % ( pre , url ) ) <EOL> else : <EOL> LOG . info ( u'<STR_LIT>' % ( pre , msg ) ) <EOL> def from_file ( m163 , option ) : <EOL> """<STR_LIT>""" <EOL> urls = [ ] <EOL> with open ( option . inFile ) as f : <EOL> urls = f . readlines ( ) <EOL> global total , done , xiami_obj <EOL> total = len ( urls ) <EOL> print border <EOL> LOG . info ( msgTxt . fmt_links_in_file % total ) <EOL> print border <EOL> pool = ThreadPool ( config . THREAD_POOL_SIZE ) <EOL> for link in [ u for u in urls if u ] : <EOL> link = link . rstrip ( '<STR_LIT:\n>' ) <EOL> if re . match ( pat_xm , link ) : <EOL> __init_xiami_obj ( option ) <EOL> pool . add_task ( from_url_xm , xiami_obj , link , verbose = False ) <EOL> elif re . match ( pat_163 , link ) : <EOL> pool . add_task ( from_url_163 , m163 , link , verbose = False ) <EOL> else : <EOL> LOG . warning ( msgTxt . fmt_skip_unknown_url % link ) <EOL> pool . wait_completion ( ) <EOL> def xiami_url_abbr ( url ) : <EOL> return re . sub ( pat_xm , msgTxt . short_xm , url ) <EOL> def m163_url_abbr ( url ) : <EOL> return re . sub ( pat_163 , msgTxt . short_163 , url ) </s>
