1
0
mirror of https://github.com/shimunn/aria2xspf.git synced 2023-11-17 09:27:54 +01:00

basic structure

This commit is contained in:
shimunn 2019-03-15 23:02:18 +01:00
commit 5cc317cd61
3 changed files with 206 additions and 0 deletions

121
Cargo.lock generated Normal file
View File

@ -0,0 +1,121 @@
[[package]]
name = "aria2xspf"
version = "0.1.0"
dependencies = [
"derive_builder 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
"xml-rs 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "darling"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"darling_core 0.8.5 (registry+https://github.com/rust-lang/crates.io-index)",
"darling_macro 0.8.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "darling_core"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
"ident_case 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.29 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "darling_macro"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"darling_core 0.8.5 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.29 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "derive_builder"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"darling 0.8.5 (registry+https://github.com/rust-lang/crates.io-index)",
"derive_builder_core 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
"proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.29 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "derive_builder_core"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"darling 0.8.5 (registry+https://github.com/rust-lang/crates.io-index)",
"proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.29 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "fnv"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "ident_case"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "proc-macro2"
version = "0.4.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "quote"
version = "0.6.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "syn"
version = "0.15.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "unicode-xid"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "xml-rs"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum darling 0.8.5 (registry+https://github.com/rust-lang/crates.io-index)" = "52f20e669de9a5689aa54f3ca9ddc9e14ae5eef15028192bd2f7caf9376a0040"
"checksum darling_core 0.8.5 (registry+https://github.com/rust-lang/crates.io-index)" = "672c68a636ed710f415c1fbaa48805db6c7400b8dc10f6b04e851ac1ea1b8c99"
"checksum darling_macro 0.8.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e4115f0ff5e1a6092815889a480c4f1e4679991d7bfc99696b6f03980123c549"
"checksum derive_builder 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a0ca533e6abb78f9108585535ce2ae0b14c8b4504e138a9a28eaf8ba2b270c1d"
"checksum derive_builder_core 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "fb484fe06ba1dc5b82f88aff700191dfc127e02b06b35e302c169706168e2528"
"checksum fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2fad85553e09a6f881f739c29f0b00b0f01357c743266d478b68951ce23285f3"
"checksum ident_case 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3c9826188e666f2ed92071d2dadef6edc430b11b158b5b2b3f4babbcc891eaaa"
"checksum proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)" = "4d317f9caece796be1980837fd5cb3dfec5613ebdb04ad0956deea83ce168915"
"checksum quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)" = "cdd8e04bd9c52e0342b406469d494fcb033be4bdbe5c606016defbb1681411e1"
"checksum syn 0.15.29 (registry+https://github.com/rust-lang/crates.io-index)" = "1825685f977249735d510a242a6727b46efe914bb67e38d30c071b1b72b1d5c2"
"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
"checksum xml-rs 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "541b12c998c5b56aa2b4e6f18f03664eef9a4fd0a246a55594efae6cc2d964b5"

9
Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "aria2xspf"
version = "0.1.0"
authors = ["shimunn <>"]
edition = "2018"
[dependencies]
xml-rs = "0.8.0"
derive_builder = "0.7.1"

76
src/main.rs Normal file
View File

@ -0,0 +1,76 @@
#[macro_use]
extern crate derive_builder;
use std::io::{self, Write};
use std::env;
use std::fs::File;
use std::io::{BufRead, BufReader};
use xml::writer::{EventWriter, EmitterConfig, XmlEvent, Result as XResult};
#[derive(Builder, Debug)]
struct Track {
url: String,
title: String
}
impl Track {
fn new(url: String, title: String) -> Track {
Track{
url: url,
title: title
}
}
fn write_xml<W: Write>(self,w: &mut EventWriter<W>) -> XResult<()> {
let events: [XmlEvent; 8] = [
XmlEvent::start_element("track".into()).into(),
XmlEvent::start_element("location".into()).into(),
XmlEvent::characters(&self.url).into(),
XmlEvent::end_element().into(),
XmlEvent::start_element("title".into()).into(),
XmlEvent::characters(&self.title).into(),
XmlEvent::end_element().into(),
XmlEvent::end_element().into()
];
for event in &events {
w.write(event.into()).into()?;
}
Ok(())
}
}
fn main() {
let file = File::open("VIDs").expect("Failed to open file!");
let mut out = File::create("VIDs.xspf").unwrap();
let mut writer = EmitterConfig::new().perform_indent(true).create_writer(&mut out);
convert(tracks(BufReader::new(file).lines().map(|line| line.expect("Failed to read line"))), &mut writer).expect("Failed to parse");
}
fn convert<I: Iterator<Item=Track>, W: Write>(tracks: I,w: &mut EventWriter<W>) -> XResult<()> {
w.write(XmlEvent::start_element("playlist".into()).ns("", "http://xspf.org/ns/0/").ns("vlc", "http://www.videolan.org/vlc/playlist/ns/0/")).into()?;
w.write(XmlEvent::start_element("trackList".into())).into()?;
for track in tracks {
track.write_xml(w)?;
}
w.write(XmlEvent::end_element().into()).into()?;
w.write(XmlEvent::end_element().into()).into()?
}
fn tracks<'a, I: Iterator<Item=String> + 'a>(aria: I) -> Box<Iterator<Item=Track> + 'a> {
let tracks = aria.scan(TrackBuilder::default(), |mut builder, line| {
if !builder.url.is_some() {
builder.url(line);
} else {
if line.starts_with("\t") {
builder.title(line.chars().skip_while(|c| c != &'=').skip(1).collect::<String>());
let track = Some(builder.build().unwrap());
*builder = TrackBuilder::default();
return Some(track);
}
}
Some(None)
}).filter_map(|track| track);
Box::new(tracks)
}