|
1 | 1 | use std::sync::Arc; |
2 | 2 |
|
3 | 3 | use log::{error, info}; |
4 | | -use serde::{Deserialize, Serialize}; |
5 | 4 |
|
6 | | -use super::api::{Client, ConnectionInfo}; |
| 5 | +use super::service::{Service, TorrentInfo}; |
7 | 6 | use crate::config::Configuration; |
8 | 7 | use crate::databases::database::{Database, DatabaseError}; |
9 | 8 | use crate::errors::ServiceError; |
10 | 9 |
|
11 | | -// If `TorrentInfo` struct is used in the future for other purposes, it should |
12 | | -// be moved to a separate file. Maybe a `ClientWrapper` struct which returns |
13 | | -// `TorrentInfo` and `TrackerKey` structs instead of `Response` structs. |
14 | | - |
15 | | -#[derive(Debug, Serialize, Deserialize)] |
16 | | -pub struct TorrentInfo { |
17 | | - pub info_hash: String, |
18 | | - pub seeders: i64, |
19 | | - pub completed: i64, |
20 | | - pub leechers: i64, |
21 | | - pub peers: Vec<Peer>, |
22 | | -} |
23 | | - |
24 | | -#[derive(Debug, Serialize, Deserialize)] |
25 | | -pub struct Peer { |
26 | | - pub peer_id: Option<PeerId>, |
27 | | - pub peer_addr: Option<String>, |
28 | | - pub updated: Option<i64>, |
29 | | - pub uploaded: Option<i64>, |
30 | | - pub downloaded: Option<i64>, |
31 | | - pub left: Option<i64>, |
32 | | - pub event: Option<String>, |
33 | | -} |
34 | | - |
35 | | -#[derive(Debug, Serialize, Deserialize)] |
36 | | -pub struct PeerId { |
37 | | - pub id: Option<String>, |
38 | | - pub client: Option<String>, |
39 | | -} |
40 | | - |
41 | 10 | pub struct StatisticsImporter { |
42 | 11 | database: Arc<Box<dyn Database>>, |
43 | | - api_client: Client, |
| 12 | + tracker_service: Arc<Service>, |
44 | 13 | tracker_url: String, |
45 | 14 | } |
46 | 15 |
|
47 | 16 | impl StatisticsImporter { |
48 | | - pub async fn new(cfg: Arc<Configuration>, database: Arc<Box<dyn Database>>) -> Self { |
| 17 | + pub async fn new(cfg: Arc<Configuration>, tracker_service: Arc<Service>, database: Arc<Box<dyn Database>>) -> Self { |
49 | 18 | let settings = cfg.settings.read().await; |
50 | | - let api_client = Client::new(ConnectionInfo::new( |
51 | | - settings.tracker.api_url.clone(), |
52 | | - settings.tracker.token.clone(), |
53 | | - )); |
54 | 19 | let tracker_url = settings.tracker.url.clone(); |
55 | 20 | drop(settings); |
56 | 21 | Self { |
57 | 22 | database, |
58 | | - api_client, |
| 23 | + tracker_service, |
59 | 24 | tracker_url, |
60 | 25 | } |
61 | 26 | } |
@@ -102,13 +67,7 @@ impl StatisticsImporter { |
102 | 67 | /// Will return an error if the HTTP request failed or the torrent is not |
103 | 68 | /// found. |
104 | 69 | pub async fn import_torrent_statistics(&self, torrent_id: i64, info_hash: &str) -> Result<TorrentInfo, ServiceError> { |
105 | | - let response = self |
106 | | - .api_client |
107 | | - .get_torrent_info(info_hash) |
108 | | - .await |
109 | | - .map_err(|_| ServiceError::InternalServerError)?; |
110 | | - |
111 | | - if let Ok(torrent_info) = response.json::<TorrentInfo>().await { |
| 70 | + if let Ok(torrent_info) = self.tracker_service.get_torrent_info(info_hash).await { |
112 | 71 | let _ = self |
113 | 72 | .database |
114 | 73 | .update_tracker_info(torrent_id, &self.tracker_url, torrent_info.seeders, torrent_info.leechers) |
|
0 commit comments