forked from torrust/torrust-index
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [torrust#56] console command to import tracker stats for all to…
…rrents
- Loading branch information
1 parent
19d054e
commit 5a7d875
Showing
6 changed files
with
111 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
//! Import Tracker Statistics command. | ||
//! It imports the number of seeders and leechers for all torrent from the linked tracker. | ||
//! You can execute it with: `cargo run --bin import_tracker_statistics` | ||
|
||
use torrust_index_backend::console::commands::import_tracker_statistics::run_importer; | ||
|
||
#[actix_web::main] | ||
async fn main() { | ||
run_importer().await; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
//! It imports statistics for all torrents from the linked tracker. | ||
|
||
use std::env; | ||
use std::sync::Arc; | ||
|
||
use derive_more::{Display, Error}; | ||
use text_colorizer::*; | ||
|
||
use crate::config::Configuration; | ||
use crate::databases::database::connect_database; | ||
use crate::tracker::TrackerService; | ||
|
||
const NUMBER_OF_ARGUMENTS: usize = 0; | ||
|
||
#[derive(Debug)] | ||
pub struct Arguments {} | ||
|
||
#[derive(Debug, Display, PartialEq, Error)] | ||
#[allow(dead_code)] | ||
pub enum ImportError { | ||
#[display(fmt = "internal server error")] | ||
WrongNumberOfArgumentsError, | ||
} | ||
|
||
fn parse_args() -> Result<Arguments, ImportError> { | ||
let args: Vec<String> = env::args().skip(1).collect(); | ||
|
||
if args.len() != NUMBER_OF_ARGUMENTS { | ||
eprintln!( | ||
"{} wrong number of arguments: expected {}, got {}", | ||
"Error".red().bold(), | ||
NUMBER_OF_ARGUMENTS, | ||
args.len() | ||
); | ||
print_usage(); | ||
return Err(ImportError::WrongNumberOfArgumentsError); | ||
} | ||
|
||
Ok(Arguments {}) | ||
} | ||
|
||
fn print_usage() { | ||
eprintln!( | ||
"{} - imports torrents statistics from linked tracker. | ||
cargo run --bin upgrade SOURCE_DB_FILE DESTINY_DB_FILE TORRENT_UPLOAD_DIR | ||
For example: | ||
cargo run --bin import_tracker_statistics | ||
", | ||
"Upgrader".green() | ||
); | ||
} | ||
|
||
pub async fn run_importer() { | ||
import(&parse_args().unwrap()).await; | ||
} | ||
|
||
pub async fn import(_args: &Arguments) { | ||
println!("Importing statistics from linked tracker ..."); | ||
|
||
let cfg = match Configuration::load_from_file().await { | ||
Ok(config) => Arc::new(config), | ||
Err(error) => { | ||
panic!("{}", error) | ||
} | ||
}; | ||
|
||
let settings = cfg.settings.read().await; | ||
|
||
let tracker_url = settings.tracker.url.clone(); | ||
|
||
eprintln!("Tracker url: {}", tracker_url.green()); | ||
|
||
let database = Arc::new( | ||
connect_database(&settings.database.connect_url) | ||
.await | ||
.expect("Database error."), | ||
); | ||
|
||
let tracker_service = Arc::new(TrackerService::new(cfg.clone(), database.clone())); | ||
|
||
tracker_service.update_torrents().await.unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod import_tracker_statistics; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod commands; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters