def view_down(self, playlist_id, path="."): list = self.get_playlist(str(playlist_id)) msg = {"success": 0, "failed": 0, "failed_list": []} for music in list['tracks']: pylog.print_info( "正在下载歌曲 {}-{}.mp3".format( tools.encode(music['name']), tools.encode(music['artists'][0]['name']) ) ) link = self.get_mp3_link(music["id"]) if link is None: msg["failed"] = msg["failed"] + 1 msg["failed_list"].append(music) continue r = requests.get(link) with open("{}/{}-{}{}".format( path, tools.encode(music['name']).replace("/", "-"), tools.encode(music['artists'][0]['name']).replace("/", "-"), ".mp3" ), "wb") as code: code.write(r.content) msg["success"] = msg["success"] + 1 pylog.print_warn( "下载成功:{} 首,下载失败:{}首".format(msg["success"], msg["failed"]) ) tb = [["歌曲名字", "艺术家", "ID"]] for music in msg["failed_list"]: n = music['name'].encode("utf-8") a = music['artists'][0]['name'].encode("utf-8") i = music['id'] tb.append([n, a, i]) print(AsciiTable(tb).table)
def print_pdf(id): data = read_playlist_json(id) if data["code"] != 200: pylog.print_warn("歌单信息拉取失败!") return document = Document() try: document.add_heading(data["result"]["name"], 0) tags = document.add_paragraph(" ".join(data["result"]["tags"])) desc = document.add_paragraph(data["result"]["description"]) for m in data["result"]["tracks"]: document.add_paragraph().add_run(m["name"]).font.color.theme_color = MSO_THEME_COLOR_INDEX.ACCENT_2 lyric = read_lyric_data(m["id"]) document.add_paragraph().add_run(lyric["lrc"]["lyric"]).font.color.theme_color = MSO_THEME_COLOR_INDEX.ACCENT_3 comments = read_comment_data(m["id"]) for c in comments["hotComments"]: author = document.add_paragraph().add_run(c["user"]["nickname"]).style = 'Emphasis' content = document.add_paragraph(c["content"]) except Exception as e: pylog.print_warn(e) document.save("{}.docx".format(data["result"]["name"])) pylog.print_info("文档 {}.docx 已经生成!".format(data["result"]["name"]))
def main(): with App() as app: try: app.run() except CaughtSignal as e: pylog.print_warn("控制台异常:{}".format(e)) except Exception as e: pylog.print_err("执行抓取任务遭遇配置异常: {}".format(e))
def clear_playlist(self,playlist_id=2098905487): m = music.Music() data = m.curl_playlist(playlist_id) for d in data["tracks"]: res = self.post_playlist_delete([str(d["id"]),],playlist_id) if res["code"] == 200: pylog.print_info("成功删除《{}》到指定歌单,歌单目前包含歌曲 {} 首".format(d["name"],res["count"])) else: pylog.print_warn("歌曲《{}》不存在于歌单中!".format(d["name"])) pylog.print_warn("删除歌单歌曲任务完成,请检查!")
def create_playlist_comment_top100(self,playlist_id=2098905487): data = settings.Session.query(pysql.Music163.song_name, pysql.Music163.song_id,pysql.Music163.comment.label("count")).order_by( pysql.Music163.comment.label("count").desc()).limit(60).all() for d in data: res = self.post_playlist_add([str(d[1]),],playlist_id) if res["code"] == 502: pylog.print_warn("歌曲《{}》已经存在于歌单中!".format(d[0])) if res["code"] == 200: pylog.print_info("成功添加《{}》到指定歌单,歌单目前包含歌曲 {} 首".format(d[0],res["count"])) pylog.print_warn("增加歌单歌曲任务完成,请检查!")
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 print_comment(count): session = settings.Session() comments = session.query(pysql.Comment163).order_by( desc(pysql.Comment163.liked)).limit(count) document = Document() workbook = Workbook() try: document.add_heading("TOP {} 评论".format(count), 0) sheet = workbook.add_sheet("TOP {} 评论".format(count)) i = 0 sheet.write(i, 0, "歌曲名字") sheet.write(i, 1, "评论作者") sheet.write(i, 2, "评论内容") sheet.write(i, 3, "点赞数量") sheet.write(i, 4, "歌曲链接") for c in comments: i = i + 1 song = session.query( pysql.Music163).filter(pysql.Music163.song_id == c.song_id) pylog.print_info("正在填充第 {} 条评论,歌曲:{}".format(i, song[0].song_name)) document.add_paragraph().add_run( "作者:{}".format(c.author) ).font.color.theme_color = MSO_THEME_COLOR_INDEX.ACCENT_2 document.add_paragraph().add_run("内容:{}".format( c.txt)).font.color.theme_color = MSO_THEME_COLOR_INDEX.ACCENT_2 document.add_paragraph().add_run( "歌曲:《{}》 链接:http://music.163.com/#/song?id={}".format( song[0].song_name, c.song_id )).font.color.theme_color = MSO_THEME_COLOR_INDEX.ACCENT_2 document.add_paragraph().add_run( "赞同:{}".format(c.liked) ).font.color.theme_color = MSO_THEME_COLOR_INDEX.ACCENT_2 document.add_paragraph("") sheet.write(i, 0, song[0].song_name) sheet.write(i, 1, c.author) sheet.write(i, 2, c.txt) sheet.write(i, 3, c.liked) sheet.write(i, 4, "http://music.163.com/#/song?id={}".format(c.song_id)) except Exception as e: pylog.print_warn(e) document.save("TOP {} 评论.docx".format(count)) pylog.print_warn("\n完成文档 TOP {} 评论.docx 的生成!\n".format(count)) workbook.save("TOP {} 评论.xls".format(count)) pylog.print_warn("\n完成文档 TOP {} 评论.xls 的生成!\n".format(count))
def top50(self): if self.app.pargs.username is None: pylog.print_warn("没有指定用户名(--username)参数,无法执行任务!") return if self.app.pargs.password is None: pylog.print_warn("没有指定密码(--password)参数,无法执行任务!") return if self.app.pargs.playlist is None: pylog.print_warn("没有指定目标歌单ID(--playlist),无法执行任务!") return username = self.app.pargs.username password = self.app.pargs.password playlist_id = self.app.pargs.playlist cmd = authorize.Command() cmd.do_login(username, password) cmd.clear_playlist(playlist_id) cmd.create_playlist_comment_top100(playlist_id)
def dropdb(): try: Base.metadata.drop_all(settings.engine) except Exception as e: pylog.print_warn("自动删除数据库表出现问题: {}".format(e))
def initdb(): try: Base.metadata.create_all(settings.engine) except Exception as e: pylog.print_warn("自动生成数据库表出现问题: {}".format(e))