From 86e60f1e5c9b5d7f20451bf6e1e65805d74d6c4a Mon Sep 17 00:00:00 2001 From: lychang <632945494@qq.com> Date: Fri, 12 Jan 2024 04:10:11 +0800 Subject: [PATCH] init --- .gitignore | 1 + .gitlab-ci.yml | 0 Cargo.toml | 11 +++++++++++ Dockerfile | 4 ++++ src/file.rs | 16 +++++++++++++++- src/main.rs | 17 ++++++++++++----- 6 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 .gitignore create mode 100644 .gitlab-ci.yml create mode 100644 Cargo.toml create mode 100644 Dockerfile diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..e69de29 diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..c06451e --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "pbc" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +reqwest = {version = "0.11", features = ["blocking"]} +scraper = "0.18.1" +bytes = "1.5.0" \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..dba645b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,4 @@ +FROM ubuntu:latest +LABEL authors="123ee" + +ENTRYPOINT ["top", "-b"] \ No newline at end of file diff --git a/src/file.rs b/src/file.rs index e969958..e94ab64 100644 --- a/src/file.rs +++ b/src/file.rs @@ -1,6 +1,6 @@ use std::fs; use std::fs::File; -use std::io::Write; +use std::io::{Read, Write}; use std::path::Path; use bytes::Bytes; use bytes; @@ -16,3 +16,17 @@ pub(crate) fn write_file(path: &str, content: Bytes) -> Result<(), std::io::Erro file.write_all(&content)?; Ok(()) } + +pub(crate) fn read_file(path: &str) -> Result { + let mut file = File::open(path)?; + let mut contents = Vec::new(); + file.read_to_end(&mut contents)?; + Ok(Bytes::from(contents)) +} + +pub(crate) fn read_file_string(path: &str) -> Result { + let mut file = File::open(path)?; + let mut contents = String::new(); + file.read_to_string(&mut contents)?; + Ok(contents) +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index f965ef1..3a4fbab 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,9 @@ +use std::io::Read; +use std::str::FromStr; use std::thread; use std::time::Duration; +use bytes::Bytes; mod file; @@ -9,18 +12,22 @@ mod web_parser; fn main() { let mut continue_flag = true; - let mut idx: i32 = 1; + let mut idx: i32; + let index = file::read_file_string("cache/index").expect("Read index error"); + if index.is_empty() { + idx = 1; + } else { + idx = i32::from_str(&index).expect("Index Corrupted"); + } while continue_flag { let name = web_parser::get_title(idx); if name == "" { continue_flag = false; } else { web_parser::download_images(idx, &name); + idx += 1; } thread::sleep(Duration::from_millis(300)); - idx += 1; - if idx > 3 { - continue_flag = false; - } } + file::write_file("cache/index", Bytes::from(idx.to_string())).expect(&format!("Write index error: {}", idx)); } \ No newline at end of file