git,bash - 从一个远端git库只下载一个文件的方法

打印 上一主题 下一主题

主题 839|帖子 839|积分 2517

git,bash - 从一个远端git库只下载一个文件的方法

概述

github上有许多大佬上传了电子书库,如果只相中一本书,也要去迁出整个库,急死个人。
试了 Git希罕检出 的方法,不现实,由于对于一个巨大的库,那也下载好久啊(git库的元数据就很大)。
只能是用git库页面的下载raw文件的方法,只是说,是用工具来下载,还是手工点击用浏览器来下载的区别。
笔记

有的库,是卖书的人上传的,将书对应的pdf都删了,以是也没法下载raw文件。
以是只要浏览到库中的书,确实存在,就可以用下载raw文件。

如果想用bash脚本来下载,只是网络url, 可以F12来找raw file url.
开始没明白,直接下载网页上的url, 实际下载的是html.

下载后,发现.pdf尺寸不对,才200K+,打开一看,原来是这个页面的html.
在html中找raw file url
  1. "rawBlobUrl":"https://github.com/weaiken/ebook/raw/refs/heads/master/03_operating_system/UNIX%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E8%AE%BE%E8%AE%A1.pdf
复制代码
在git bash窗口中,按照本身找到的实际raw file url来下载就Ok了。
  1. curl -L -o UNIX操作系统设计1.pdf https://github.com/weaiken/ebook/raw/refs/heads/master/03_operating_system/UNIX%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E8%AE%BE%E8%AE%A1.pdf
复制代码

写一个bash脚本来自动下载

get_github_raw_file_from_url.sh

  1. #!/bin/bash
  2. # @file get_github_raw_file_from_url.sh
  3. # @brief 从url中提取文件名(包括后缀)
  4. # e.g. get_github_raw_file_from_url.sh https://github.com/weaiken/ebook/blob/master/03_operating_system/UNIX%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E8%AE%BE%E8%AE%A1.pdf
  5. clear
  6. # 参数检查
  7. if [ $# -ne 1 ]; then
  8.     echo "错误:需要且只能输入一个URL参数" >&2
  9.     exit 1
  10. fi
  11. # 提取URL中的文件名部分
  12. encoded_file=$(basename "$1")  # 或使用:encoded_file=${1##*/}
  13. # URL解码函数
  14. urldecode() {
  15.     local encoded=$1
  16.     printf '%b' "${encoded//%/\\x}" 2>/dev/null | sed 's/+/ /g'
  17. }
  18. # 执行解码并输出结果
  19. decoded_file=$(urldecode "$encoded_file")
  20. echo "原始文件为: $encoded_file"
  21. echo "提取的中文文件名:$decoded_file"
  22. curl -L -o $decoded_file $1
  23. ./rename_file.sh $decoded_file $decoded_file html
  24. html_file="$decoded_file.html"
  25. echo "html file = $html_file"
  26. raw_url=$(./find_key_value.sh $html_file "rawBlobUrl")
  27. echo "raw_url = $raw_url"
  28. curl -L -o $decoded_file $raw_url
  29. echo "file download over : $decoded_file"
复制代码
reanme_file.sh

  1. #!/bin/bash
  2. # @file reanme_file.sh
  3. # @brief 将参数1的文件名 改名为 参数2.参数3
  4. # e.g. rename_file.sh a.pdf a.pdf html
  5. # a.pdf => a.pdf.html
  6. set -euo pipefail
  7. # 参数校验
  8. if [ $# -ne 3 ]; then
  9.     echo "错误:需要3个参数,用法:$0 原文件名 前缀 后缀" >&2
  10.     exit 1
  11. fi
  12. original_file="$1"
  13. new_name="$2.$3"
  14. # 文件存在性检查
  15. if [ ! -f "$original_file" ]; then
  16.     echo "错误:文件 '$original_file' 不存在" >&2
  17.     exit 1
  18. fi
  19. # 执行重命名
  20. mv -v "$original_file" "$new_name"
  21. echo "重命名成功:$original_file -> $new_name"
复制代码
find_key_value.sh

  1. #!/bin/bash
  2. # find_key_value.sh
  3. # 用法:./a.sh  <html文件> <键名>
  4. html_file=$1
  5. key_name=$2
  6. # 使用正则表达式匹配JSON格式的键值对
  7. value=$(grep -oP ""$key_name"\s*:\s*"\K[^"]+" "$html_file" | head -1)
  8. # 验证结果并输出
  9. if [ -z "$value" ]; then
  10.     echo "未找到 $key_name 对应的值" >&2
  11.     exit 1
  12. else
  13.     echo "$value"
  14.     exit 0
  15. fi
复制代码
执行命令

  1. git库文件的html url, 可以从浏览器url标题栏拷贝
  2. https://www.github.com/weaiken/ebook/blob/master/03_operating_system/UNIX操作系统设计.pdf
复制代码
在git bash命令行窗口中执行命令
脚本写的粗糙,必须包管网络正常。
  1. ./get_github_raw_file_from_url.sh https://github.com/weaiken/ebook/blob/master/03_operating_system/UNIX%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E8%AE%BE%E8%AE%A1.pdf
复制代码

这个工具脚本已经实现了从库文件浏览到的实际html用户页面下载实际的库文件对应的raw原始文件。
END


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

小小小幸运

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表