eureka_manager_cli/commands/download/
chapter.rs

1use std::{fs::File, io::BufReader, path::PathBuf};
2
3use actix::Addr;
4use clap::{Args, ValueEnum};
5use eureka_mmanager::{
6    download::{
7        chapter::{task::DownloadMode, ChapterDownloadMessage},
8        cover::CoverDownloadMessage,
9        manga::MangaDownloadMessage,
10        state::DownloadMessageState,
11    },
12    history::service::messages::is_in::IsInMessage,
13    prelude::*,
14};
15use indicatif::ProgressBar;
16use log::{info, trace};
17use mangadex_api_types_rust::RelationshipType;
18use uuid::Uuid;
19
20use crate::commands::{AsyncRun, AsyncRunContext};
21
22#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash, ValueEnum, Default)]
23pub enum ChapterDownloadMode {
24    /// the default download mode
25    #[default]
26    Data,
27    /// the economic mode
28    DataSaver,
29}
30
31impl From<ChapterDownloadMode> for DownloadMode {
32    fn from(value: ChapterDownloadMode) -> Self {
33        match value {
34            ChapterDownloadMode::Data => Self::Normal,
35            ChapterDownloadMode::DataSaver => Self::DataSaver,
36        }
37    }
38}
39
40#[derive(Debug, Args)]
41pub struct ChapterDownloadArgs {
42    /// Manga ids
43    #[arg(long = "id")]
44    pub ids: Vec<Uuid>,
45    #[arg(long)]
46    pub id_text_file: Vec<PathBuf>,
47    #[arg(short, long)]
48    pub mode: ChapterDownloadMode,
49}
50
51impl ChapterDownloadArgs {
52    pub fn get_id_and_modes(&self) -> Vec<(Uuid, ChapterDownloadMode)> {
53        let mut res = Vec::new();
54        let mut push_res = |value: (Uuid, ChapterDownloadMode)| {
55            res.push(value);
56        };
57
58        self.ids
59            .iter()
60            .map(|id| (*id, self.mode))
61            .for_each(&mut push_res);
62
63        self.id_text_file
64            .iter()
65            .map(|e| (e, File::open(e)))
66            .flat_map(|(path, res)| match res {
67                Ok(file) => Some(id_list_txt_reader::IdListTxtReader::new(BufReader::new(
68                    file,
69                ))),
70                Err(err) => {
71                    log::error!("Cannot open the {} file: {}", path.to_string_lossy(), err);
72                    None
73                }
74            })
75            .flat_map(|reader| {
76                reader.flat_map(|entry| -> Option<(Uuid, ChapterDownloadMode)> {
77                    if entry.contains(';') {
78                        let mut split = entry.split(';');
79                        let id: Uuid = split.next()?.parse().ok()?;
80                        let mode = split
81                            .next()
82                            .and_then(|part| ChapterDownloadMode::from_str(part, true).ok())
83                            .unwrap_or(self.mode);
84                        Some((id, mode))
85                    } else {
86                        Some((entry.parse().ok()?, self.mode))
87                    }
88                })
89            })
90            .for_each(&mut push_res);
91        res
92    }
93}
94
95impl AsyncRun for ChapterDownloadArgs {
96    async fn run(&self, ctx: AsyncRunContext) -> anyhow::Result<()> {
97        let ids = self.get_id_and_modes();
98        let mut progress = ProgressBar::new(ids.len() as u64);
99        progress = ctx.progress.add(progress);
100        trace!(
101            "Downloading {} chapters with their titles and cover if needed",
102            ids.len()
103        );
104        for (id, mode) in ids {
105            let manager = ctx.manager.clone();
106            let task = async move {
107                trace!("Downloading chapter {id}");
108                let dirs =
109                    <Addr<DownloadManager> as GetManagerStateData>::get_dir_options(&manager)
110                        .await?;
111                let manga = {
112                    let chapter_manager = <Addr<DownloadManager> as GetManager<
113                        ChapterDownloadManager,
114                    >>::get(&manager)
115                    .await?;
116                    let mut task = chapter_manager
117                        .send(
118                            ChapterDownloadMessage::new(id)
119                                .state(DownloadMessageState::Downloading)
120                                .mode(mode),
121                        )
122                        .await?;
123                    let data = task.wait().await?.await?;
124                    info!(
125                        "downloaded chapter {} = {:?}",
126                        data.id, data.attributes.title
127                    );
128                    data.find_first_relationships(RelationshipType::Manga)
129                        .ok_or(anyhow::Error::msg(format!(
130                            "Cannot find the chapter {} title",
131                            id
132                        )))?
133                        .clone()
134                };
135                if !dirs
136                    .send(IsInMessage(HistoryEntry::new(
137                        manga.id,
138                        RelationshipType::Manga,
139                    )))
140                    .await?
141                {
142                    let cover = {
143                        trace!("Downloading title {}", manga.id);
144                        let manga_manager = <Addr<DownloadManager> as GetManager<
145                            MangaDownloadManager,
146                        >>::get(&manager)
147                        .await?;
148                        let mut task = manga_manager
149                            .send(
150                                MangaDownloadMessage::new(manga.id)
151                                    .state(DownloadMessageState::Downloading),
152                            )
153                            .await?;
154                        let data = task.wait().await?.await?;
155                        info!(
156                            "downloaded title {} = {:?}",
157                            data.id,
158                            data.attributes.title.values().next()
159                        );
160                        data.find_first_relationships(RelationshipType::CoverArt)
161                            .ok_or(anyhow::Error::msg(format!(
162                                "Cannot find the title {} cover art",
163                                manga.id
164                            )))?
165                            .clone()
166                    };
167                    if !dirs
168                        .send(IsInMessage(HistoryEntry::new(
169                            cover.id,
170                            RelationshipType::CoverArt,
171                        )))
172                        .await?
173                    {
174                        trace!("Downloading {} cover art", cover.id);
175                        let cover_manager = <Addr<DownloadManager> as GetManager<
176                            CoverDownloadManager,
177                        >>::get(&manager)
178                        .await?;
179                        let mut task = cover_manager
180                            .send(
181                                CoverDownloadMessage::new(cover.id)
182                                    .state(DownloadMessageState::Downloading),
183                            )
184                            .await?;
185                        task.wait().await?.await?;
186                        info!("Downloaded {} cover art", cover.id);
187                    }
188                }
189                Ok::<_, anyhow::Error>(())
190            };
191            let res = task.await;
192
193            if let Err(err) = res {
194                log::error!("{}", err);
195            }
196            progress.inc(1);
197        }
198        progress.finish();
199        ctx.progress.remove(&progress);
200        Ok(())
201    }
202}