diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 53 |
1 files changed, 37 insertions, 16 deletions
diff --git a/src/main.rs b/src/main.rs index c91861a..cb8b02d 100644 --- a/src/main.rs +++ b/src/main.rs | |||
| @@ -6,12 +6,14 @@ extern crate regex; | |||
| 6 | extern crate yaml_rust; | 6 | extern crate yaml_rust; |
| 7 | 7 | ||
| 8 | extern crate walkdir; | 8 | extern crate walkdir; |
| 9 | use walkdir::WalkDir; | 9 | use walkdir::{WalkDir, DirEntry}; |
| 10 | 10 | ||
| 11 | use std::io::prelude::*; | 11 | use std::io::prelude::*; |
| 12 | use std::fs::File; | 12 | use std::fs::File; |
| 13 | use std::env; | 13 | use std::env; |
| 14 | use std::path::Path; | 14 | use std::path::Path; |
| 15 | use std::sync::{Arc, Mutex}; | ||
| 16 | use std::thread; | ||
| 15 | 17 | ||
| 16 | mod pre_process; | 18 | mod pre_process; |
| 17 | mod section; | 19 | mod section; |
| @@ -23,6 +25,8 @@ mod tags; | |||
| 23 | mod dl_list; | 25 | mod dl_list; |
| 24 | mod tiles; | 26 | mod tiles; |
| 25 | 27 | ||
| 28 | static MAX_THREADS: u32 = 12; | ||
| 29 | |||
| 26 | fn main() { | 30 | fn main() { |
| 27 | let raw_files = env::var("RAW_FILES").unwrap_or("characters".into()); | 31 | let raw_files = env::var("RAW_FILES").unwrap_or("characters".into()); |
| 28 | let out_files = env::var("OUT").unwrap_or("json".into()); | 32 | let out_files = env::var("OUT").unwrap_or("json".into()); |
| @@ -30,24 +34,41 @@ fn main() { | |||
| 30 | let out_path = Path::new(&out_files); | 34 | let out_path = Path::new(&out_files); |
| 31 | 35 | ||
| 32 | 36 | ||
| 33 | for entry in WalkDir::new(base_path).min_depth(1).into_iter().filter_map(|e| e.ok()) { | 37 | |
| 34 | let mut f = File::open(entry.path()).expect("could not open file"); | 38 | let files: Arc<Mutex<Vec<DirEntry>>> = Arc::new(Mutex::new(WalkDir::new(base_path).min_depth(1).into_iter().filter_map(|e| e.ok()).collect())); |
| 35 | let mut buf = String::new(); | 39 | |
| 36 | if let Err(_) = f.read_to_string(&mut buf) { | 40 | for 1..MAX_THREADS { |
| 37 | println!("invalid file: {}", entry.path().to_str().unwrap()); | 41 | |
| 38 | continue; | 42 | std::thread(move || { |
| 39 | } | 43 | let entry: Option<DirEntry> = None; |
| 44 | { | ||
| 45 | entry = files.lock().unwrap().pop(); | ||
| 46 | |||
| 47 | if entry.is_none() { | ||
| 48 | println!("thread finished"); | ||
| 49 | return; | ||
| 50 | } | ||
| 51 | } | ||
| 52 | let entry = entry.unwrap(); | ||
| 53 | |||
| 54 | let mut f = File::open(entry.path()).expect("could not open file"); | ||
| 55 | let mut buf = String::new(); | ||
| 56 | if let Err(_) = f.read_to_string(&mut buf) { | ||
| 57 | println!("invalid file: {}", entry.path().to_str().unwrap()); | ||
| 58 | continue; | ||
| 59 | } | ||
| 40 | 60 | ||
| 41 | let buf = pre_process::strip_irrelevant_content(&buf); | 61 | let buf = pre_process::strip_irrelevant_content(&buf); |
| 42 | 62 | ||
| 43 | let mut char = Character::new(); | 63 | let mut char = Character::new(); |
| 44 | char.parse(&buf); | 64 | char.parse(&buf); |
| 45 | 65 | ||
| 46 | let json = serde_json::to_string(&char).unwrap(); | 66 | let json = serde_json::to_string(&char).unwrap(); |
| 47 | 67 | ||
| 48 | let out_file = out_path.join(entry.file_name().to_str().unwrap().replace("html", "json")); | 68 | let out_file = out_path.join(entry.file_name().to_str().unwrap().replace("html", "json")); |
| 49 | let mut o = File::create(&out_file).unwrap(); | 69 | let mut o = File::create(&out_file).unwrap(); |
| 50 | o.write_all(json.as_bytes()).unwrap(); | 70 | o.write_all(json.as_bytes()).unwrap(); |
| 51 | println!("{:?}", out_file); | 71 | println!("{:?}", out_file); |
| 72 | }); | ||
| 52 | } | 73 | } |
| 53 | } | 74 | } |
