From dc5e75587f275fb0b12b3953b77c8d5d5a760dcc Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Mon, 17 Jun 2024 22:27:41 +0300 Subject: [PATCH] resume and torrent parsing done --- src/main.rs | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index befbf21..9761a48 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ use std::{fs::{self, DirEntry}, path::Path}; +use bencode::decode; use clap::{Parser, Subcommand}; use rusqlite::Connection; @@ -7,7 +8,7 @@ mod bencode; #[derive(Subcommand)] enum Command { Index { - #[arg(short='p', long, default_value="torman.db", help="path to transmission dir with torrents and resume")] + #[arg(short='p', long, default_value="torman.db", help="path to transmission dir with \"torrents\" and \"resume\" dirs")] path: String } } @@ -51,7 +52,68 @@ fn index(db: Connection, path: &String) { } }; - println!("{}", hash); + // parse the resume file + let (resume, _) = { + let resume_data = match fs::read(entry.path()) { + Ok(data) => data, + Err(_) => { + eprintln!("failed to read {} resume file", hash); + continue + } + }; + + match decode(&resume_data) { + Ok(value) => value, + Err(e) => { + eprintln!("failed to parse {} resume file: {:#?}", hash, e); + continue + } + } + }; + + // parse the torrent file + let torrent_path = { + let mut torrent_name = hash.to_owned(); + torrent_name.push_str(".torrent"); + + Path::new(path).join("torrents").join(torrent_name) + }; + + let (torrent, _) = { + let torrent_data = match fs::read(torrent_path) { + Ok(data) => data, + Err(_) => { + eprintln!("failed to read {} torrent file", hash); + continue + } + }; + + match decode(&torrent_data) { + Ok(value) => value, + Err(e) => { + eprintln!("failed to parse {} torrent file: {:#?}", hash, e); + continue + } + } + }; + + // make table row + let hash = hash; + let name = resume.get_string("name"); + let destination = resume.get_string("destination"); + let downloaded = resume.get_integer("downloaded"); + let uploaded = resume.get_integer("uploaded"); + + let announce = torrent.get_string("announce"); + let comment = torrent.get_string("comment"); + let created_by = torrent.get_string("created_by"); + let creation_date = torrent.get_integer("creation_date"); + let publisher = torrent.get_string("publisher"); + let publisher_url = torrent.get_string("publisher-url"); + + dbg!(hash, name, destination, downloaded, uploaded, announce, comment, created_by, creation_date, publisher, publisher_url); + + break } }