Compare commits
2 Commits
92cc323f93
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
75906abc47 | ||
|
|
ac67753f01 |
@@ -9,12 +9,14 @@ authors = ["François Girault <fgirault@gmail.com>"]
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
rss = "2.0"
|
rss = "2.0"
|
||||||
url = "2.5.2"
|
# url = "2.5.2"
|
||||||
json = "0.12.4"
|
json = "0.12.4"
|
||||||
clap = { version = "4.5.9", features = ["derive"] }
|
clap = { version = "4.5.9", features = ["derive"] }
|
||||||
chrono = "0.4.38"
|
chrono = "0.4.38"
|
||||||
env_logger = "0.11.3"
|
env_logger = "0.11.3"
|
||||||
log = "0.4.22"
|
log = "0.4.22"
|
||||||
reqwest = { version = "0.12.5", features = ["blocking"] }
|
reqwest = { version = "0.12.5", features = ["blocking"] }
|
||||||
regex = "1.10.5"
|
# polodb_core = "4.4.1"
|
||||||
|
# serde = "1.0.204"
|
||||||
|
# regex = "1.10.5"
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ rss2json converts rss xml feeds to a json format.
|
|||||||
|
|
||||||
## About
|
## About
|
||||||
|
|
||||||
This is my first Rust tutorial project, so surely not coded with all the best practices.
|
**This is my first Rust tutorial project**, so surely not coded with all the best practices, and clearly useless.
|
||||||
|
|
||||||
Output format is a complete personnal choice, inspired by some formats I've seen when using other's api.
|
Output format is a complete personnal choice, inspired by some formats I've seen when using other's api.
|
||||||
|
|
||||||
@@ -12,12 +12,16 @@ Output format is a complete personnal choice, inspired by some formats I've seen
|
|||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
Setup Rust using rust-up, then run in a terminal:
|
Setup Rust using [rustup](https://www.rust-lang.org/tools/install) and [set channels to nigthly](https://rust-lang.github.io/rustup/concepts/channels.html).
|
||||||
|
|
||||||
|
Then run in a terminal:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
cargo install --git https://git.tetalab.org/Mutah/rss2json.git
|
cargo install --git https://git.tetalab.org/Mutah/rss2json.git
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
|||||||
2
rust-toolchain.toml
Normal file
2
rust-toolchain.toml
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
[toolchain]
|
||||||
|
channel = "nightly"
|
||||||
21
src/args.rs
Normal file
21
src/args.rs
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
use clap::Parser;
|
||||||
|
|
||||||
|
#[derive(Parser, Debug)]
|
||||||
|
#[clap(author, version, about, long_about = None)]
|
||||||
|
pub struct Args {
|
||||||
|
#[clap(short, long, required = true)]
|
||||||
|
pub input: String,
|
||||||
|
|
||||||
|
#[clap(short, long, default_value_t=String::new())]
|
||||||
|
pub output: String,
|
||||||
|
|
||||||
|
#[clap(short, long, default_value_t = false)]
|
||||||
|
pub pretty: bool,
|
||||||
|
|
||||||
|
#[clap(long, default_value_t = 4)]
|
||||||
|
pub indentation: u16,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse_args()-> Args {
|
||||||
|
return Args::parse();
|
||||||
|
}
|
||||||
30
src/channel_lib.rs
Normal file
30
src/channel_lib.rs
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
use std::fs::File;
|
||||||
|
use std::io::{BufReader, Cursor};
|
||||||
|
|
||||||
|
use log::{error, info};
|
||||||
|
pub use rss::Channel;
|
||||||
|
|
||||||
|
pub fn build_channel(input: &String) -> Channel {
|
||||||
|
let channel: Channel;
|
||||||
|
|
||||||
|
if input.starts_with("http://") || input.starts_with("https://") {
|
||||||
|
// process download
|
||||||
|
match reqwest::blocking::get(input).unwrap().bytes() {
|
||||||
|
Ok(content) => {
|
||||||
|
info!("Extracted {} bytes", content.len());
|
||||||
|
let cursor = Cursor::new(content);
|
||||||
|
channel = Channel::read_from(cursor).unwrap();
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
error!("{}", e);
|
||||||
|
std::process::exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// read locale file
|
||||||
|
let file = File::open(input).unwrap();
|
||||||
|
channel = Channel::read_from(BufReader::new(file)).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
return channel;
|
||||||
|
}
|
||||||
70
src/json_lib.rs
Normal file
70
src/json_lib.rs
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
use chrono::DateTime;
|
||||||
|
pub use json::{object, JsonValue};
|
||||||
|
use log::error;
|
||||||
|
use rss::Channel;
|
||||||
|
|
||||||
|
// Build a JsonValue object reflecting a Channel instance
|
||||||
|
pub fn build_json(channel: &Channel) -> JsonValue {
|
||||||
|
// Initialize root object with channel informations, using object macro from json crate
|
||||||
|
let mut data = object! {
|
||||||
|
channel: object!{
|
||||||
|
title: channel.title(),
|
||||||
|
link: channel.link(),
|
||||||
|
description: channel.description(),
|
||||||
|
last_build_date: channel.last_build_date(),
|
||||||
|
language: channel.language(),
|
||||||
|
copyright: channel.copyright(),
|
||||||
|
generator: channel.generator()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// declare an mutable array to populate it with channel item data
|
||||||
|
let mut items_data = json::JsonValue::new_array();
|
||||||
|
|
||||||
|
// populate the items array
|
||||||
|
for item in channel.items() {
|
||||||
|
let pub_datetime = DateTime::parse_from_rfc2822(item.pub_date().unwrap()).unwrap();
|
||||||
|
|
||||||
|
// create object to hold item data
|
||||||
|
let mut item_data = object! {
|
||||||
|
title: item.title(),
|
||||||
|
link: item.link(),
|
||||||
|
pub_date: item.pub_date(),
|
||||||
|
pub_ts: pub_datetime.timestamp(),
|
||||||
|
description: item.description(),
|
||||||
|
// author: item.author()
|
||||||
|
};
|
||||||
|
|
||||||
|
// populate categories
|
||||||
|
let mut categories = json::JsonValue::new_array();
|
||||||
|
|
||||||
|
for category in item.categories() {
|
||||||
|
match categories.push(category.name()) {
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(e) => {
|
||||||
|
// memory overflow ? as a beginner, style puzzled by rust
|
||||||
|
error!("Error pushing to items_data {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
item_data["categories"] = categories;
|
||||||
|
|
||||||
|
if item.content().is_some() {
|
||||||
|
item_data["content"] = json::JsonValue::String(item.content().unwrap().to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
match items_data.push(item_data) {
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(e) => {
|
||||||
|
// memory overflow ? as a beginner, style puzzled by rust
|
||||||
|
error!("Error pushing to items_data {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// attach the items to the json data root
|
||||||
|
data["items"] = items_data;
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
||||||
149
src/main.rs
149
src/main.rs
@@ -1,145 +1,32 @@
|
|||||||
/**
|
/**
|
||||||
* A simple personnal tutorial for my first baby steps with Rust.
|
* A simple personnal tutorial for my first baby steps with Rust.
|
||||||
*/
|
*/
|
||||||
use std::fs;
|
// TODO refactor and unit tests to improve code while knowledge grows !
|
||||||
use std::fs::File;
|
// TODO handle remote http feed discovery by parsing index and search for the feed link
|
||||||
use std::io::{BufReader, Cursor};
|
// TODO serialize to local document database CRUD
|
||||||
|
|
||||||
|
// use log::{debug, info, warn, error };
|
||||||
|
// use log::{error, info};
|
||||||
|
mod args;
|
||||||
|
use crate::args::parse_args;
|
||||||
|
|
||||||
use chrono::DateTime;
|
mod channel_lib;
|
||||||
use clap::Parser;
|
use crate::channel_lib::{build_channel, Channel};
|
||||||
use json::object;
|
|
||||||
use rss::Channel;
|
|
||||||
|
|
||||||
// use log::debug;
|
mod json_lib;
|
||||||
use log::error;
|
use crate::json_lib::{build_json, JsonValue};
|
||||||
use log::info;
|
|
||||||
// use log::warn;
|
|
||||||
|
|
||||||
#[derive(Parser, Debug)]
|
mod render;
|
||||||
#[clap(author, version, about, long_about = None)]
|
use crate::render::render_json;
|
||||||
struct Args {
|
|
||||||
#[clap(short, long, required = true)]
|
|
||||||
input: String,
|
|
||||||
|
|
||||||
#[clap(short, long, default_value_t=String::new())]
|
fn main() {
|
||||||
output: String,
|
|
||||||
|
|
||||||
#[clap(short, long, default_value_t = false)]
|
|
||||||
pretty: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() -> std::io::Result<()> {
|
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
||||||
let args = Args::parse();
|
let args = parse_args();
|
||||||
|
|
||||||
// TODO handle remote http feed discovery by parsing index and search for the feed link
|
let channel: Channel = build_channel(&args.input);
|
||||||
|
|
||||||
let channel: Channel;
|
let data: JsonValue = build_json(&channel);
|
||||||
|
|
||||||
if args.input.starts_with("http://") || args.input.starts_with("https://") {
|
render_json(&data, args.pretty, args.indentation, &args.output);
|
||||||
// process download
|
|
||||||
match reqwest::blocking::get(args.input).unwrap().bytes() {
|
|
||||||
Ok(content) => {
|
|
||||||
info!("Extracted {} bytes", content.len());
|
|
||||||
let cursor = Cursor::new(content);
|
|
||||||
channel = Channel::read_from(cursor).unwrap();
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
error!("{}", e);
|
|
||||||
std::process::exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// read locale file
|
|
||||||
let file = File::open(args.input).unwrap();
|
|
||||||
channel = Channel::read_from(BufReader::new(file)).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize root object with channel informations, using object macro from json crate
|
|
||||||
let mut data = object! {
|
|
||||||
channel: object!{
|
|
||||||
title: channel.title(),
|
|
||||||
link: channel.link(),
|
|
||||||
description: channel.description(),
|
|
||||||
last_build_date: channel.last_build_date(),
|
|
||||||
language: channel.language(),
|
|
||||||
copyright: channel.copyright(),
|
|
||||||
generator: channel.generator()
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// declare an mutable array to populate it with channel item data
|
|
||||||
let mut items_data = json::JsonValue::new_array();
|
|
||||||
|
|
||||||
// populate the items array
|
|
||||||
for item in channel.items() {
|
|
||||||
let pub_datetime = DateTime::parse_from_rfc2822(item.pub_date().unwrap()).unwrap();
|
|
||||||
|
|
||||||
// create object to hold item data
|
|
||||||
let mut item_data = object! {
|
|
||||||
title: item.title(),
|
|
||||||
link: item.link(),
|
|
||||||
pub_date: item.pub_date(),
|
|
||||||
pub_ts: pub_datetime.timestamp(),
|
|
||||||
description: item.description(),
|
|
||||||
// author: item.author()
|
|
||||||
};
|
|
||||||
|
|
||||||
// populate categories
|
|
||||||
let mut categories = json::JsonValue::new_array();
|
|
||||||
|
|
||||||
for category in item.categories() {
|
|
||||||
match categories.push(category.name()) {
|
|
||||||
Ok(_) => {}
|
|
||||||
Err(e) => {
|
|
||||||
// memory overflow ? as a beginner, style puzzled by rust
|
|
||||||
error!("Error pushing to items_data {}", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
item_data["categories"] = categories;
|
|
||||||
|
|
||||||
if item.content().is_some() {
|
|
||||||
item_data["content"] = json::JsonValue::String(item.content().unwrap().to_string());
|
|
||||||
}
|
|
||||||
|
|
||||||
match items_data.push(item_data) {
|
|
||||||
Ok(_) => {}
|
|
||||||
Err(e) => {
|
|
||||||
// memory overflow ? as a beginner, style puzzled by rust
|
|
||||||
error!("Error pushing to items_data {}", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// attach the items to the json data root
|
|
||||||
data["items"] = items_data;
|
|
||||||
|
|
||||||
// output result
|
|
||||||
let output_string: String = if args.pretty {
|
|
||||||
data.pretty(4)
|
|
||||||
} else {
|
|
||||||
data.dump()
|
|
||||||
};
|
|
||||||
|
|
||||||
if args.output.len() > 0 {
|
|
||||||
let output_length = output_string.len();
|
|
||||||
let filename = args.output.to_string();
|
|
||||||
match fs::write(filename, output_string) {
|
|
||||||
Ok(_) => {
|
|
||||||
info!("saving {} characters to {}", output_length, args.output);
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
error!("{:?}", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
// no file name specified, dump json to stdout
|
|
||||||
println!("{}", output_string);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
28
src/render.rs
Normal file
28
src/render.rs
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
use json::JsonValue;
|
||||||
|
use log::{error, info};
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
|
pub fn render_json(value: &JsonValue, pretty: bool, spaces: u16, output: &String) {
|
||||||
|
// output result
|
||||||
|
let output_string: String = if pretty {
|
||||||
|
value.pretty(spaces)
|
||||||
|
} else {
|
||||||
|
value.dump()
|
||||||
|
};
|
||||||
|
|
||||||
|
if output.len() > 0 {
|
||||||
|
let output_length = output_string.len();
|
||||||
|
let filename = output.to_string();
|
||||||
|
match fs::write(filename, output_string) {
|
||||||
|
Ok(_) => {
|
||||||
|
info!("saved {} characters to {}", output_length, output);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
error!("{:?}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// no file name specified, dump json to stdout
|
||||||
|
println!("{}", output_string);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user