def ana_song(weblink): ml = mylogger(logfile, get_funcname()) html = op_simple(weblink, header)[0] # html = op_requests(url,verify=False).content bsObj = BeautifulSoup(html, "html.parser") # ml.debug(bsObj) # title = bsObj.find('title') # print(title) song_name = bsObj.find('em', {'class': 'f-ff2'}) songname = modstr(song_name.text.strip()) ml.info(songname) aa = bsObj.findAll('p', {'class': 'des s-fc4'}) artistname = modstr(aa[0].span.a.text) albumname = modstr(aa[1].a.text) ml.info(artistname) ml.info(albumname) cover = bsObj.find('div', {'class': 'u-cover u-cover-6 f-fl'}) cover = cover.img.attrs['href'] ml.info(cover) songmid = weblink.split('=')[-1] sDict = { 'artist': artistname, 'song_name': songname, 'songmid': songmid, 'cover': cover } ml.debug(sDict) return sDict
def dl(albumlink, force=False): '''main function to download album''' ml = mylogger(logfile, get_funcname()) adict = ana_cd(albumlink) coverlink = adict['cover'] artist = adict['artist'] year = adict['year'] albumname = adict['albumname'] albumdir = f'{artist} - {year} - {albumname}' if find_album(albumdir) and force == False: ml.warning(f'Album alread archived') else: albumfulldir = create_folder(dldir, albumdir) cover = os.path.join(albumfulldir, albumdir + '.jpg') m_cover = os.path.join(albumfulldir, albumdir + '.png') if os.path.isfile(cover): ml.warning('---- Big Cover download already !') else: ml.info('Download big cover') myget.dl(coverlink, out=cover) if os.path.isfile(m_cover): ml.warning('---- Small cover ready !') else: shutil.copy(cover, m_cover) squaresize(m_cover) for tracknum in range(1, adict['number'] + 1): songid = adict[tracknum]['id'] singer = modstr(adict[tracknum]['singer']) songname = modstr(adict[tracknum]['songname']) songfullname = f'{singer} - {songname}.mp3' mp3 = os.path.join(albumfulldir, songfullname) ml.info(f'{tracknum} {singer} - {songname}') if os.path.isfile(mp3): ml.warning('---- Track download already !') else: try: dlurl = get_dlurl(songid) myget.dl(dlurl, out=mp3) except TypeError: ml.error('Not published Track') continue except Exception as e: ml.error(e) ml.error("Content incomplete -> retry") myget.dl(dlurl, out=mp3) else: addtag(mp3, songname, albumname, artist, singer, m_cover, year, tracknum) mywait(random.randint(1, 3)) try: os.remove(m_cover) clean_f(albumfulldir, 'tmp') ml.info(f'Complete download {albumdir}') except FileNotFoundError: pass
def download_album(self, workfolder, album_detail): ml = mylogger(logfile, get_funcname()) artist_name = album_detail['artist_name'] album_name = album_detail['album_name'] year = album_detail['year'] albumdir = f'{artist_name} - {year} - {album_name}' albumfulldir = create_folder(workfolder, albumdir) try: coverlink = album_detail['coverlink'] cover = os.path.join(albumfulldir, albumdir + '.jpg') m_cover = os.path.join(albumfulldir, albumdir + '.png') if os.path.isfile(cover): ml.warn('---- Big Cover download already !') else: ml.info('Download big cover') myget.dl(coverlink, out=cover) if os.path.isfile(m_cover): ml.warn('---- Small cover ready !') else: shutil.copy(cover, m_cover) squaresize(m_cover) songid_list = album_detail['songid_list'] download_url_dict = self.get_song_download_url(songid_list) ml.dbg(download_url_dict) for s in songid_list: singers = modstr( album_detail['song_detail_list'][s]['singers']) songname = modstr( album_detail['song_detail_list'][s]['songname']) songfullname = f'{singers} - {songname}.mp3' mp3 = os.path.join(albumfulldir, songfullname) if os.path.isfile(mp3): ml.warn(f'---- {songname} download already !') else: cdserial = str( album_detail['song_detail_list'][s]['cdserial']) track = str(album_detail['song_detail_list'][s]['track']) ml.info(f'{cdserial}.{track} {singers} - {songname}') if dlurl := download_url_dict[s]: try: myget.dl(dlurl, out=mp3) mywait(random.randint(1, 3)) addtag(mp3, songname, album_name, artist_name, singers, m_cover, year, track, cdserial) except AttributeError as e: if "urllib.error" in str(e): pass except Exception as e: print(e) if "HTTP Error 404: Not Found" in str(e): ml.err("File Not Found") else: raise os.remove(m_cover) clean_f(albumfulldir, 'tmp') ml.info('Download Complete')
def ana_album(weblink): ml = mylogger(logfile, get_funcname()) html = op_simple(weblink, header=ran_header(ref=ref))[0] bsObj = BeautifulSoup(html, "html.parser") #;print(bsObj) album_name = bsObj.find('h1', {'class': 'data__name_txt'}) album_name = modstr(album_name.text) ml.debug(album_name) artist_name = bsObj.find('a', {'class': 'js_singer data__singer_txt'}) artist_name = modstr(artist_name.text) ml.debug(artist_name) year = bsObj.find(text=re.compile('^发行时间'))[5:9] ml.debug(year) cover = bsObj.find('img', {'id': 'albumImg'}) cover = 'http:' + cover.attrs['src'] ml.debug('Cover link: ' + cover) fullname = artist_name + ' - ' + year + ' - ' + album_name aDict = { 'album': album_name, 'artist': artist_name, 'year': year, 'cover': cover, 'fullname': fullname } song = bsObj.findAll('div', {'class': 'songlist__number'}) n = 0 songtmp = [] # name duplicate check for i in song: n += 1 tracknumber = i.text ml.debug('Find track ' + str(tracknumber)) tmp = i.next_sibling.next_sibling si = tmp.find('span', {'class': 'songlist__songname_txt'}).a songmid = si.attrs['href'].split('/')[-1][:-5] songname = si.text if songname in songtmp: songname = songname + '_' + tracknumber songtmp.append(songname) ml.debug(songname) singers = tmp.parent.findAll('a', {'class': "singer_name"}) if len(singers) > 1: s = list(map(lambda x: x.text, singers)) singer = ','.join(s) else: singer = singers[0].text ml.debug(singer) si = [songmid, songname, singer] aDict[int(tracknumber)] = si aDict['TrackNum'] = n # ml.info(aDict) return aDict # Album dictionary
def ana_song(weblink): # return song dictionary ml = mylogger(logfile, get_funcname()) songmid = weblink.split('/')[-1] songmid = songmid.split('.')[0] ml.debug(songmid) html = op_simple(weblink)[0] bsObj = BeautifulSoup(html, "html.parser") artist_name = bsObj.find('div', {'class': 'data__singer'}) artist_name = artist_name.attrs['title'] ml.debug(artist_name) song_name = bsObj.find('h1', {'class': 'data__name_txt'}) song_name = modstr(song_name.text.strip()) ml.debug(song_name) cover = bsObj.find('img', {'class': 'data__photo'}) cover = 'http:' + cover.attrs['src'] ml.debug('Cover link: ' + cover) sDict = { 'artist': artist_name, 'song_name': song_name, 'songmid': songmid, 'cover': cover } ml.debug(sDict) return sDict
def xm_json(year=None, force=True): '''Analyze json album data''' ml = mylogger(logfile, get_funcname()) j = json.loads(get_text_clipboard()) # pprint(j) # j = f2json(get_text_clipboard()) j = j['result']['data']['albumDetail'] album_detail = {} # if year is None: # year = input('Publish year>>') artist_name = modstr(j['artistName']) album_name = modstr(j['albumName']) coverlink = j['albumLogo'] album_detail['year'] = year album_detail['artist_name'] = artist_name album_detail['album_name'] = album_name album_detail['coverlink'] = coverlink albumdir = f'{artist_name} - {year} - {album_name}' if find_album(albumdir) and force == False: ml.warn(f'{albumdir} alread archived') return None else: coverlink = j['albumLogo'] cdcount = j['cdCount'] songcount = j['songCount'] song_detail_list = {} songid_list = [] for s in range(songcount): songid_list.append(j['songs'][s]['songId']) song_detail_list[j['songs'][s]['songId']] = { "singers": modstr(j['songs'][s]['singers']), "songname": modstr(j['songs'][s]['songName']), "cdserial": j['songs'][s]['cdSerial'], "track": j['songs'][s]['track'] } # pprint(song_detail_list) # pprint(songid_list) album_detail['song_detail_list'] = song_detail_list album_detail['songid_list'] = songid_list ml.dbg(album_detail) return album_detail
def xm_json(workfolder, year=None, force=False): '''Analyze json album data''' ml = mylogger(logfile, get_funcname()) j = json.loads(get_text_clipboard()) print(j) # j = f2json(get_text_clipboard()) j = j['result']['data']['albumDetail'] if year is None: year = input('Publish year>>') artist_name = modstr(j['artistName']) album_name = modstr(j['albumName']) albumdir = f'{artist_name} - {year} - {album_name}' if find_album(albumdir) and force == False: ml.warn(f'{albumdir} alread archived') else: albumfulldir = create_folder(workfolder, albumdir) try: coverlink = j['albumLogo'] cover = os.path.join(albumfulldir, albumdir + '.jpg') m_cover = os.path.join(albumfulldir, albumdir + '.png') if os.path.isfile(cover): ml.warn('---- Big Cover download already !') else: ml.info('Download big cover') myget.dl(coverlink, out=cover) if os.path.isfile(m_cover): ml.warn('---- Small cover ready !') else: shutil.copy(cover, m_cover) squaresize(m_cover) cdcount = j['cdCount'] songcount = j['songCount'] for s in range(songcount): singers = modstr(j['songs'][s]['singers']) songname = modstr(j['songs'][s]['songName']) songfullname = f'{singers} - {songname}.mp3' mp3 = os.path.join(albumfulldir, songfullname) cdserial = str(j['songs'][s]['cdSerial']) track = str(j['songs'][s]['track']) if os.path.isfile(mp3): ml.warn(f'---- {songname} download already !') else: try: ml.info(f'{cdserial}.{track} {singers} - {songname}') songid = j['songs'][s]['songId'] location = get_songlocation(songid) dlurl = decry(location) print(dlurl) # dlurl = 'http:'+decry(location) # myget.dl(dlurl,out=mp3) myget.simpledl(dlurl, mp3, verify=False, headers=headers) except Exception as e: ml.err(e) ml.err("Content incomplete -> retry") # myget.dl(dlurl,out=mp3) mywait(random.randint(1, 3)) addtag(mp3, songname, album_name, artist_name, singers, m_cover, year, track, cdserial) os.remove(m_cover) clean_f(albumfulldir, 'tmp') ml.info('Download Complete') except FileNotFoundError: pass