dnrops.gitlink.net/main.rs

293 lines
12 KiB
Rust
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

use doe::*;
use std::path::PathBuf;
#[allow(warnings)]
fn main() {
use doe::*;
if (args!().len() == 1 && &args!()[0] == "-h") || args!().len() < 1 {
println!("Usage:");
println!("cargo r -- -h => display help");
println!("cargo r -- deploy => deploy to gitee");
println!("cargo r -- get_img => get all images to blog_images repo");
} else if args!().len() == 1 && &args!()[0] == "blog_images_move" {
let repo_path = "D:\\CODE\\gitlink_code\\gitcode_blog\\img_list\\blog_images";
doe::fs::fs::walk_dir(repo_path)
.unwrap()
.iter()
.for_each(|p| {
if p.display().to_string().ends_with("png") {
let file_name = p
.display()
.to_string()
.split("/")
.last()
.unwrap()
.replace("?", "")
.replace("~", "")
.replace("~", "")
.replace("-", "")
.replace("", "(")
.replace("", ")")
.replace(".image", ".png");
let new_line = file_name
.push_front("D:\\CODE\\gitlink_code\\gitcode_blog\\img_list\\")
.push_front(format!("![{}](", file_name));
doe::fs::fs::move_file(
p,
&p.display()
.to_string()
.replace(
p.display().to_string().split("/").last().unwrap(),
&file_name,
)
.to_path_buf(),
)
.unwrap();
}
});
} else if args!().len() == 1 && &args!()[0] == "fix_image" {
let demo =
std::fs::read_to_string("D:\\CODE\\gitlink_code\\gitcode_blog3\\new1.txt").unwrap();
for img in demo.split_to_vec("\n") {
let img_name = img
.split_to_vec("../../img_list/")
.last()
.unwrap()
.split_to_vec(")")
.first()
.unwrap()
.to_string();
let new_img = img_name
.replace("?", "")
.replace("~", "")
.replace("~", "")
.replace("-", "")
.replace("", "(")
.replace("", ")");
let repo_path = "./src/posts";
doe::fs::fs::walk_dir(repo_path)
.unwrap()
.iter()
.for_each(|p| {
if p.display().to_string().ends_with("md") {
let md_content = std::fs::read_to_string(p).unwrap();
std::fs::write(
p,
md_content.replace(&img, &img.replace(&img_name, &new_img)),
)
.unwrap();
}
});
}
} else if args!().len() == 1 && &args!()[0] == "rename_image" {
let repo_path = "./src/posts";
doe::fs::fs::walk_dir(repo_path)
.unwrap()
.iter()
.for_each(|p| {
if p.display().to_string().ends_with("md") {
let mut new_lines = vec![];
std::fs::read_to_string(p)
.unwrap()
.split_to_vec("\n")
.iter()
.for_each(|line| {
if (line.contains(".png")
|| line.contains(".jpg")
|| line.contains(".image"))
&& line.contains("](")
&& line.contains("![")
&& !line.contains("///")
{
let file_name = line
.split("/")
.last()
.unwrap()
.replace("?", "")
.replace("~", "")
.replace("~", "")
.replace("-", "")
.replace("", "(")
.replace("", ")")
.replace(".image", ".png");
let new_line = file_name
.push_front("../../img_list/")
.push_front(format!("![{}](", file_name));
line.println();
new_lines.push(new_line.to_string().push_back("\n"));
} else {
new_lines.push(line.to_string().push_back("\n"));
}
});
// std::fs::write(p, new_lines.join("\n")).unwrap();
}
});
} else if args!().len() == 1 && &args!()[0] == "get_img_to_local" {
let repo_path = "./src/posts";
doe::fs::fs::walk_dir(repo_path)
.unwrap()
.iter()
.for_each(|p| {
if p.display().to_string().ends_with("md") {
let mut new_lines = vec![];
std::fs::read_to_string(p)
.unwrap()
.split_to_vec("\n")
.iter()
.for_each(|line| {
if (line.contains(".png")
|| line.contains(".jpg")
|| line.contains(".image"))
&& line.contains("](")
&& line.contains("![")
&& !line.contains("///")
{
if let Some(url) = extract_image_url(line) {
let file_name = url
.split("/")
.last()
.unwrap()
.replace("?", "")
.replace("~", "")
.replace("~", "")
.replace("-", "")
.replace("", "(")
.replace("", ")")
.replace(".image)", ".png");
download_image(
&url,
"D:\\CODE\\gitlink_code\\gitcode_blog\\img_list\\"
.push_back(file_name)
.to_path_buf(),
)
}
} else {
new_lines.push(line.to_string());
}
});
std::fs::write(p, new_lines.join("\n")).unwrap();
}
});
} else if args!().len() == 1 && &args!()[0] == "get_img" {
let repo_path = "./src/posts";
doe::fs::fs::walk_dir(repo_path)
.unwrap()
.iter()
.for_each(|p| {
if p.display().to_string().ends_with("md") {
let mut new_lines = vec![];
std::fs::read_to_string(p)
.unwrap()
.split_to_vec("\n")
.iter()
.for_each(|line| {
if line.contains(".image") && line.contains("p3-juejin.byteimg") {
if let Some(url) = extract_image_url(line) {
let file_name =
url.split("/").last().unwrap().replace(".image", ".png");
download_image(
&url,
"../blog_images/all_imgs/"
.push_back(file_name.clone())
.to_path_buf(),
);
let ll =
line.replace(&url, &"../../img_list/".push_back(file_name));
new_lines.push(ll);
}
} else {
new_lines.push(line.to_string());
}
});
std::fs::write(p, new_lines.join("\n")).unwrap();
}
});
} else if args!().len() == 1 && &args!()[0] == "deploy" {
system("mdbook build").unwrap();
let deploy_path = "../dnrops";
doe::fs::fs::walk_dir(deploy_path)
.unwrap()
.iter()
.for_each(|p| {
if p.starts_with("../dnrops/.git/") {
} else {
doe::remove_file_or_folder!(p);
}
});
// frist cretae dirs
doe::fs::fs::walk_dir("./book")
.unwrap()
.iter()
.for_each(|p| {
if p.is_dir() {
doe::fs::fs::move_folder(
p,
&deploy_path
.to_path_buf()
.join(p.strip_prefix("./book").unwrap()),
)
.unwrap();
}
});
// secondary move files
doe::fs::fs::walk_dir("./book")
.unwrap()
.iter()
.for_each(|p| {
if !p.is_dir() {
doe::fs::fs::move_file(
p,
&deploy_path
.to_path_buf()
.join(p.strip_prefix("./book").unwrap()),
)
.unwrap();
}
});
// push blog
system("ppush new").unwrap();
std::env::set_current_dir("../dnrops").unwrap();
// push page
system("ppush new").unwrap();
}
}
#[allow(warnings)]
fn download_image(url: &str, save_path: PathBuf) {
url.println();
if !save_path.exists() {
use std::io::Write;
let mut client = reqwest::blocking::Client::new();
let mut resp = client
.get(url)
.timeout(std::time::Duration::from_secs(60))
.send()
.unwrap();
let buf = resp.bytes().unwrap().to_vec();
let mut file = std::fs::File::create(save_path).unwrap();
file.write_all(&buf).unwrap();
println!("{},done!", url);
}
}
#[allow(warnings)]
fn extract_image_url(input: &str) -> Option<String> {
// Extract the URL between parentheses
let re = regex::Regex::new(r"\(([^)]+)\)").unwrap();
if let Some(captures) = re.captures(input) {
let url_str = captures.get(1).unwrap().as_str();
// Parse the URL using the `url` crate
let mut url = url::Url::parse(url_str).ok()?;
// Remove the query parameters and anchor tag
url.set_query(None);
url.set_fragment(None);
Some(url.to_string())
} else {
None
}
}