Генератор списка файлов "events.xml"
This commit is contained in:
		| @@ -2,11 +2,7 @@ | |||||||
| authors = [ "Valentin Popov <info@valentineus.link>" ] | authors = [ "Valentin Popov <info@valentineus.link>" ] | ||||||
| edition = "2018" | edition = "2018" | ||||||
| name = "bbb2json" | name = "bbb2json" | ||||||
| version = "0.1.0" | version = "0.2.0" | ||||||
|  |  | ||||||
| [dependencies] | [dependencies] | ||||||
| clap = { version = "2.32.0", features = [ "yaml" ] } | walkdir = "2.3.1" | ||||||
| serde = "1.0.85" |  | ||||||
| serde_derive = "1.0.85" |  | ||||||
| serde_json = "1.0.37" |  | ||||||
| xml-rs = "0.8.0" |  | ||||||
|   | |||||||
| @@ -1,10 +0,0 @@ | |||||||
| name: "bbb2json" |  | ||||||
| version: "0.1.0" |  | ||||||
| author: "Valentin Popov <info@valentineus.link>" |  | ||||||
| about: "BigBlueButton records analyzer. Displays basic recording information." |  | ||||||
|  |  | ||||||
| args: |  | ||||||
|     - FILE: |  | ||||||
|         help: "The path to a file 'events.xml'." |  | ||||||
|         index: 1 |  | ||||||
|         required: true |  | ||||||
							
								
								
									
										39
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										39
									
								
								src/main.rs
									
									
									
									
									
								
							| @@ -1,31 +1,22 @@ | |||||||
| #[macro_use] | use walkdir::WalkDir; | ||||||
| extern crate serde_derive; |  | ||||||
| extern crate serde_json; |  | ||||||
|  |  | ||||||
| extern crate clap; | /// Создание списка "events.xml" файлов. | ||||||
| extern crate xml; | fn get_list(root: &str) -> Vec<String> { | ||||||
|  |     let mut array: Vec<String> = vec![]; | ||||||
|  |  | ||||||
| use std::fs::File; |     for entry in WalkDir::new(root) { | ||||||
| use std::io::BufReader; |         let path = entry.unwrap().path().display().to_string(); | ||||||
| use std::path::Path; |  | ||||||
|  |  | ||||||
| use clap::{load_yaml, App}; |         if path.contains("events.xml") { | ||||||
|  |             array.push(path); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
| mod parser; |     return array; | ||||||
| use parser::*; | } | ||||||
|  |  | ||||||
| fn main() { | fn main() { | ||||||
|     let yaml = load_yaml!("cli/en.yml"); |     let root = "tests"; | ||||||
|     let matches = App::from_yaml(yaml).get_matches(); |     let list = get_list(root); | ||||||
|  |     dbg!(list); | ||||||
|     let path = matches.value_of("FILE").unwrap(); |  | ||||||
|  |  | ||||||
|     match Path::new(path).exists() { |  | ||||||
|         true => { |  | ||||||
|             let file: File = File::open(path).unwrap(); |  | ||||||
|             let data: ParserResult = parser(BufReader::new(file)); |  | ||||||
|             println!("{}", serde_json::to_string_pretty(&data).unwrap()); |  | ||||||
|         } |  | ||||||
|         _ => panic!("File {:#?} does not exist or is not available.", path), |  | ||||||
|     }; |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1,60 +0,0 @@ | |||||||
| use std::fs::File; |  | ||||||
| use std::io::BufReader; |  | ||||||
|  |  | ||||||
| use serde_json::Value; |  | ||||||
| use xml::reader::{EventReader, XmlEvent}; |  | ||||||
|  |  | ||||||
| #[derive(Serialize)] |  | ||||||
| pub struct ParserResult { |  | ||||||
|     pub context: Value, |  | ||||||
|     pub external_id: String, |  | ||||||
|     pub meeting_id: String, |  | ||||||
|     pub meeting_name: String, |  | ||||||
|     pub meeting_url: String, |  | ||||||
| } |  | ||||||
|  |  | ||||||
| pub fn parser(content: BufReader<File>) -> ParserResult { |  | ||||||
|     let mut data = ParserResult { |  | ||||||
|         context: serde_json::from_str("{}").unwrap(), |  | ||||||
|         external_id: "".to_string(), |  | ||||||
|         meeting_id: "".to_string(), |  | ||||||
|         meeting_name: "".to_string(), |  | ||||||
|         meeting_url: "".to_string(), |  | ||||||
|     }; |  | ||||||
|  |  | ||||||
|     for element in EventReader::new(content) { |  | ||||||
|         match element { |  | ||||||
|             Ok(XmlEvent::StartElement { |  | ||||||
|                 name, attributes, .. |  | ||||||
|             }) => { |  | ||||||
|                 let el_name = name.local_name.to_string(); |  | ||||||
|  |  | ||||||
|                 for attribute in attributes { |  | ||||||
|                     let attr_name: String = attribute.name.local_name.to_string(); |  | ||||||
|                     let attr_value: String = attribute.value.to_string(); |  | ||||||
|  |  | ||||||
|                     if el_name == "metadata" && attr_name == "bn-recording-status" { |  | ||||||
|                         data.context = serde_json::from_str(&attr_value).unwrap(); |  | ||||||
|                     } |  | ||||||
|  |  | ||||||
|                     if el_name == "metadata" && attr_name == "meetingId" { |  | ||||||
|                         data.external_id = attr_value.clone(); |  | ||||||
|                     } |  | ||||||
|  |  | ||||||
|                     if el_name == "recording" && attr_name == "meeting_id" { |  | ||||||
|                         data.meeting_id = attr_value.clone(); |  | ||||||
|                         data.meeting_url = format!("{}{}", "https://bbb.styleschool.ru/playback/presentation/0.9.0/playback.html?meetingId=", attr_value); |  | ||||||
|                     } |  | ||||||
|  |  | ||||||
|                     if el_name == "metadata" && attr_name == "meetingName" { |  | ||||||
|                         data.meeting_name = attr_value.clone(); |  | ||||||
|                     } |  | ||||||
|                 } |  | ||||||
|             } |  | ||||||
|             Err(error) => panic!(error), |  | ||||||
|             _ => {} |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     return data; |  | ||||||
| } |  | ||||||
		Reference in New Issue
	
	Block a user