def get_playlist(self, playlist_id): url = uapi.playlist_api.format(playlist_id) try: data = tools.curl(url,self.__headers) playlist = data['result'] return playlist except Exception as e: raise
def view_lyric(self, song_id): url = uapi.lyric_url.format(str(song_id)) try: data = tools.curl(url,self.__headers) lrc = data['lrc']['lyric'] if pysql.single("lyric163", "song_id", song_id): self.session.add(pysql.Lyric163(song_id=song_id, txt=lrc)) self.session.query(pysql.Music163).filter(pysql.Music163.song_id == song_id).update({"has_lyric": "Y"}) self.session.commit() except Exception as e: self.session.query(pysql.Music163).filter(pysql.Music163.song_id == song_id).update({"has_lyric": "E"}) self.session.commit() pylog.log.error("抓取歌词出现问题:{} 歌曲ID:{}".format(e, song_id))
def get_music(self, music_id): self.view_capture(int(music_id), 1) url = uapi.music_api.format(music_id, music_id) data = tools.curl(url,self.__headers) music = data['songs'] print("《" + tools.encode(music[0]['name']) + "》") author = [] for a in music[0]['artists']: author.append(tools.encode(a['name'])) album = str(tools.encode(music[0]['album']['name'])) print("演唱:{} 专辑:{}".format(",".join(author), album)) comments = self.session.query(pysql.Comment163).filter( pysql.Comment163.song_id == int(music_id) ) tb = AsciiTable([["序号", "作者", "评论", "点赞"]]) max_width = tb.column_max_width(2) - tb.column_max_width(2) % 3 cnt = 0 try: for cmt in comments: cnt = cnt + 1 au = tools.encode(cmt.author) txt = "" length = 0 for u in cmt.txt: txt = txt + u if ord(u) < 128: length = length + 3 else: length = length + 1 if length == max_width: txt = txt + "\n" length = 0 liked = str(cmt.liked) tb.table_data.append([str(cnt), str(au), str(txt), liked]) print(tb.table) except UnicodeEncodeError: pylog.log.info("获取歌曲详情编码存在问题,转为非表格形式,歌曲ID:{}".format(music_id)) for cmt in comments: print("评论: {}".format(tools.encode(cmt.txt))) print( "作者: {} 点赞: {}".format( tools.encode(cmt.author), str(cmt.liked) ) ) print("") except Exception as e: pylog.print_warn("获取歌曲时出现异常: {} 歌曲ID:{}".format(e, music_id))
def curl_playlist(self,playlist_id): url = uapi.playlist_api.format(playlist_id) try: data = tools.curl(url, self.__headers) playlist = data['result'] self.session.query(pysql.Playlist163).\ filter(pysql.Playlist163.link == playlist_id).\ update({"playCount": playlist["playCount"], "shareCount": playlist["shareCount"], "commentCount": playlist["commentCount"], "description": playlist["description"], "tags":",".join(playlist["tags"])}) return playlist except Exception as e: pylog.Log("抓取歌单页面存在问题:{} 歌单ID:{}".format(e, playlist_id)) # pylog.print_warn("抓取歌单页面存在问题:{} 歌单ID:{}".format(e, playlist_id)) self.session.query(pysql.Playlist163).filter(pysql.Playlist163.link == playlist_id).update({'done': 'E'}) self.session.commit()
def view_capture(self, page, type="全部"): play_url = self.__play_url.format(type, page * 35) titles = [] try: acmsk = {'class': 'msk'} scnb = {'class': 'nb'} dcu = {'class': 'u-cover u-cover-1'} ucm = {'class': 'm-cvrlst f-cb'} data = tools.curl(play_url,self.__headers,type=const.RETURE_HTML) lst = data.find('ul', ucm) for play in lst.find_all('div', dcu): title = tools.encode(play.find('a', acmsk)['title']) link = tools.encode(play.find('a', acmsk)['href']).replace("/playlist?id=", "") cnt = tools.encode(play.find('span', scnb).text).replace('万', '0000') if pysql.single("playlist163","link",link) is True: pl = pysql.Playlist163(title=title, link=link, cnt=int(cnt), dsc="曲风:{}".format(type)) self.session.add(pl) self.session.commit() titles.append(title) return titles except Exception as e: pylog.log.error("抓取歌单出现问题:{} 歌单类型:{} 页码:{}".format(e, type, page)) raise
def read_lyric_data(id): url = uapi.lyric_url.format(id) data = tools.curl(url,uapi.header) return data
def read_playlist_json(id): url = uapi.playlist_api.format(id) data = tools.curl(url,uapi.header) return data