class Mp3(Music163Obj): __model_name__ = Mp3Model __model_rfilter__ = { 'artists', # read from album model } __parse_recursion__ = { 'artists': Artist(), 'album': Album(__model_rfilter__={'artist', 'songs'}), 'mv': MV(), } @attr_replace(attr_name='mvid', new_name='mv') def replace_mvid(self, obj): if obj != 0: obj = get_mv_detail(obj) return obj return None def pre_save(self, doc, obj): """ :param doc: :param obj: :type doc: Mp3Model :return: """ # set artists if getattr(doc, 'album', False) and getattr(doc['album'], 'artists', False): doc.artists = [a for a in doc['album']['artists']] def download_filename_format(self, doc): """ implement pls get a path to save file, by relative path need be complete by child :param doc: :type doc: Mp3Model :return: :rtype: str """ # authors = reduce(lambda x, y: x + ',' + y, [x['name'] for x in doc.artists]) authors = ','.join([x['name'] for x in doc.artists]) author = re.sub("[\\\\/:*?\"<>|]", '', authors.strip()) mp3_name = re.sub("[\\\\/:*?\"<>|]", '', doc['name']) name = os.path.join(author, "%s - %s.mp3" % (author, mp3_name)) return name def url_load(self, doc): """ implement pls :param doc: :return: :rtype: str """ try: return get_mp3_link(doc['id']) except: return None
class Album(Music163Obj): __model_name__ = AlbumModel __model_rfilter__ = { # 'song', # TODO need delete??? 'artist', } __parse_recursion__ = { 'artists': Artist(), } @attr_replace(attr_name='songs', new_name='mp3') def replace_song(self, obj): return obj
class Video(Music163Obj): __model_name__ = VideoModel __model_rfilter__ = { } __parse_recursion__ = { 'artists': Artist(), } def download_filename_full(self, doc): """ implement pls get a path to save file, by relative path need be complete by child :param doc: :return: :rtype: str """ # todo modify authors = ",".join([x['name'] for x in doc.artists]) author = re.sub("[\\\\/:*?\"<>|]", '', authors.strip()) mp3_name = re.sub("[\\\\/:*?\"<>|]", '', doc['name']) name = os.path.join(author, "%s - %s.mp4" % (author, mp3_name)) return name def url_load(self, doc): """ implement pls :param doc: :return: :rtype: str """ try: target_r = get_target_r(doc, Config().get_mv_resolution()) doc['download_video_r'] = target_r return get_video_link(doc['id'], target_r) except: return None
#!/usr/bin/env python # -*- coding: utf-8 -*- # # created by Lipson on 2018/6/19. # email to [email protected] # from NXSpider.spider.album import Album from NXSpider.spider.artist import Artist from NXSpider.spider.mp3 import Mp3 from NXSpider.spider.mv import MV from NXSpider.spider.playlist import Playlist from NXSpider.spider.user import User no_rec_artist_mo = Artist( __parse_recursion__={} ) no_rec_album_mo = Album( __model_rfilter__={'artist', 'songs'}, __parse_recursion__={'artists': no_rec_artist_mo} ) no_rec_mv_mo = MV( __parse_recursion__={'artists': no_rec_artist_mo} ) dw_mp3_mo = Mp3( __parse_recursion__={ 'artists': no_rec_artist_mo, 'album': no_rec_album_mo, 'mv': no_rec_mv_mo,
class MV(Music163Obj): __model_name__ = Mp4Model __model_rfilter__ = {} __parse_recursion__ = { 'artists': Artist(), } def download_filename(self, doc): """ implement pls get a name to save file need be complete by child :param doc: :return: """ authors = ",".join([x['name'] for x in doc.artists]) author = re.sub("[\\\\/:*?\"<>|]", '', authors.strip()) mv_name = re.sub("[\\\\/:*?\"<>|]", '', doc['name']) name = "%s - %s.mp4" % (author, mv_name) return name def download_filename_full(self, doc): """ implement pls get a path to save file, by relative path need be complete by child :param doc: :return: :rtype: str """ authors = ",".join([x['name'] for x in doc.artists]) author = re.sub("[\\\\/:*?\"<>|]", '', authors.strip()) mv_name = re.sub("[\\\\/:*?\"<>|]", '', doc['name']) name = os.path.join(author, "%s - %s.mp4" % (author, mv_name)) return name def url_load(self, doc): """ implement pls :param doc: :return: :rtype: str """ try: target_r = get_target_r(doc, Config().get_mv_resolution()) doc['download_video_r'] = target_r return get_mv_link(doc['id'], target_r) except: return None def shortcut_self_path(self, doc): """ implement pls, not force return self short cut path :param doc: :return: """ result = [] result.extend([ os.path.join("artist", re.sub("[\\\\/:*?\"<>|]", '', x['name'])) for x in doc.artists ]) return result