Skip to content

Instantly share code, notes, and snippets.

@AngusFu
Last active March 20, 2023 02:46
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save AngusFu/67de31347b06c6027e6332c1de8bdd2d to your computer and use it in GitHub Desktop.
Save AngusFu/67de31347b06c6027e6332c1de8bdd2d to your computer and use it in GitHub Desktop.
声享 ppt 备份(导出 html 文件)






curl -fsSL https://gist.githubusercontent.com/AngusFu/67de31347b06c6027e6332c1de8bdd2d/raw/ppt.sh \
   | bash -s "你的 thinkjs Cookie"  

image













const { existsSync, writeFile, readFile } = require('fs-extra');
const { basename } = require('path');
const cheerio = require('cheerio');
async function readFile2(file) {
return decoder.decode(await readFile(file));
}
global.atob = b64Encoded => Buffer.from(b64Encoded, 'base64').toString()
const [file, output] = process.argv.slice(2)
const decoder = new TextDecoder("utf-8");
const encoder = new TextEncoder();
async function main() {
if (file && existsSync(file)) {
const $ = cheerio.load(await readFile2(file));
$("link[rel=manifest]").remove();
$("div[hidden]").remove();
{
const scripts = $("script").toArray();
[
...scripts.slice(-2),
...scripts.filter((el) =>
($(el).html() || "").indexOf("hm.baidu.com") > -1
),
].forEach((el) => $(el).remove());
}
{
const SD = {};
const scriptContent = $("#server_data").html() || "";
eval(scriptContent);
// 隐藏个人信息
SD.myself = {
"id": 0,
"user_url": "",
"user_open_id": "",
"user_from": "",
"user_name": "",
"user_email": "",
"user_desc": "",
"user_avatar": "",
"user_type": 1,
"user_ip": "",
"created_at": "",
"updated_at": "",
"banned_time": "",
"user_mobile": "",
"user_social_accounts": [
{
"id": 82,
"user_id": 102,
"social_id": "2496785271",
"social_name": null,
"social_type": "weibo",
"created_at": "",
},
],
"user_avatar_clip": "",
"isAdmin": false,
};
// 修复资源路径问题
const json = JSON.stringify(SD.slide)
// qhres.com 失效
.replace(/\.qhres\.com/g, ".qhres2.com",)
// p[5-9].ssl.qhimg.com 域名失效
.replace(/p[5-9]\.ssl\.qhimg\.com/g, 'p.ssl.qhimg.com')
$("#server_data").html(`
SD.env = ${JSON.stringify(SD.env)};
SD.myself = ${JSON.stringify(SD.myself)};
SD.slide = JSON.parse(decodeURIComponent("${encodeURIComponent(json)}"))
`);
}
$("head").append(`<style>
.Header-userMenu, .PagePresent-console { display: none!important; }
.PagePresent-shadow,
.PagePresent-panel,
.PagePresent-panel-expander{
display: none!important;
}
.PagePresent-pageSlide--frozen {
transform: none!important;
}
</style>`);
$("style").toArray().forEach((el) => {
$(el).html(
($(el).html() || "").replace(
/url\((['"])\/\//g,
(_, g1) => `url(${g1}https://`,
),
);
});
await writeFile(
`./${basename(file)}`,
encoder.encode($.html()),
);
}
}
main()
opt_file() {
echo "处理文件 $1"
node ./ppt-opt.js "$1" "$2"
}
if [[ -z $1 ]]; then
echo "没有指定 ppt 所在文件夹"
exit
fi
# output lib
mkdir -p "$1-optimized"
curl -fsSL https://gist.githubusercontent.com/AngusFu/67de31347b06c6027e6332c1de8bdd2d/raw/ppt-opt.js > $1-optimized/ppt-opt.js
cd "$1-optimized"
npm init -y
npm i cheerio fs-extra --registry=https://registry.npmmirror.com
cd -
ls -1 "$1" | while read line; do
cd "$1-optimized" >>/dev/null
opt_file "../$1/$line" "$1-optimized"
cd - >>/dev/null
done
cd "$1-optimized"
rm -rf node_modules package.json package-lock.json ppt-opt.js
set -e -u
COOKIE="Cookie: thinkjs=$1;" # 使用浏览器登录之后拿 cookie
RESOLVE="--resolve ppt.baomitu.com:443:101.199.97.241"
list_json=$(curl $RESOLVE -s -k -H "$COOKIE" "https://ppt.baomitu.com/api/slide/list?page=1&epage=500&tag_id=-1")
raw_js=$(echo "console.log((" $list_json ").data.data.map(item => item.slide_title.trim().replace(/\s{2,}/g, ' ') + '#' + item.slide_id).join('\n'))")
data=$(node -e "$raw_js")
dir_name="./ppt-baomitu"
rm -rf $dir_name
mkdir $dir_name
echo "$data" | while read line; do
title=$(echo $line | cut -d "#" -f 1 | tr / -)
id=$(echo $line | cut -d "#" -f 2)
echo $title
target="${dir_name}/${title}.html"
if [[ -f $target ]]; then
target="${dir_name}/${title}.${id}.html"
fi
curl $RESOLVE -s -k -H "$COOKIE" "https://ppt.baomitu.com/present?slide_id=${id}&offline=1" >"$target"
done
sed -i "" 's#id\=offline_resource#hidden#g' $dir_name/*.html
opt_sh_url="https://gist.githubusercontent.com/AngusFu/67de31347b06c6027e6332c1de8bdd2d/raw/ppt-opt.sh"
curl -fsSL $opt_sh_url | bash -s $dir_name
@lizheming
Copy link

俊哥这脚本真好用啊,直接一条龙服务下了所有的 PPT,甚至还温馨的做了优化将所有资源都整到一个 HTML 中,太感人了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment