1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use crate::{models, Client, Media, MediaType, NEXT_DATA_SELECTOR};
use eyre::{ensure, eyre, Result, WrapErr};
use std::{fmt, str::FromStr};
use url::Url;

/// A media serie.
#[derive(Debug)]
pub struct Serie {
    /// Serie title.
    title: String,
    /// Media list.
    media: Vec<Media>,
}

impl Serie {
    /// Initializes a new serie.
    pub async fn new(
        client: &Client,
        id: SerieID,
        media_type: MediaType,
    ) -> Result<Self> {
        // We have two way of extracting the list of media:
        // - the API
        // - the embedded JSON payload
        //
        // API can only be used if you are logged in.
        // Embedded JSON payload only contains unread media when you're
        // logged in, otherwise it's complete.
        //
        // So, if we're logged in we use the API and in guest mode we rely on
        // the JSON.
        let info = if client.is_logged_in() {
            get_info_from_api(client, id, media_type)
                .await
                .context("get serie info from API")?
        } else {
            get_info_from_web(client, id, media_type)
                .await
                .context("get serie info from web")?
        };

        info.try_into()
    }

    /// Returns the series title.
    pub fn title(&self) -> &str {
        &self.title
    }

    /// Returns the number of media.
    pub fn media_count(&self) -> usize {
        self.media.len()
    }

    /// Returns the media.
    pub fn media(&self) -> impl Iterator<Item = &Media> + '_ {
        self.media.iter()
    }
}

/// Extract serie info from Piccoma API.
async fn get_info_from_api(
    client: &Client,
    id: SerieID,
    media_type: MediaType,
) -> Result<models::serie::Data> {
    let selector = match media_type {
        MediaType::Episode => 'E',
        MediaType::Volume => 'V',
    };
    let url = Url::parse(&format!("https://piccoma.com/fr/api/haribo/api/web/v3/product/{id}/episodes?episode_type={selector}&product_id={id}")).expect("valid serie API URL");

    Ok(client
        .get_json::<models::serie::ApiResponse>(url)
        .await
        .context("call serie endpoint")?
        .data)
}

/// Extract serie info from Piccoma web page.
async fn get_info_from_web(
    client: &Client,
    id: SerieID,
    media_type: MediaType,
) -> Result<models::serie::Data> {
    let selector = match media_type {
        MediaType::Episode => "episode",
        MediaType::Volume => "volume",
    };
    // Fetch the serie page.
    let url =
        Url::parse(&format!("https://piccoma.com/fr/product/{selector}/{id}"))
            .expect("valid serie web URL");
    let html = client.get_html(url).await.context("get series page")?;

    // Extract and parse the JSON payload.
    let payload = html
        .select(&NEXT_DATA_SELECTOR)
        .next()
        .ok_or_else(|| eyre!("look for serie __NEXT_DATA__"))?
        .text()
        .collect::<String>();
    let data = serde_json::from_str::<models::serie::NextData>(&payload)
        .context("parse serie __NEXT_DATA__")?;

    Ok(data
        .props
        .page_props
        .initial_state
        .product_home
        .product_home)
}

impl TryFrom<models::serie::Data> for Serie {
    type Error = eyre::Report;

    fn try_from(value: models::serie::Data) -> Result<Self, Self::Error> {
        ensure!(!value.product.title.is_empty(), "empty serie title");

        Ok(Self {
            title: value.product.title,
            media: value
                .media_list
                .into_iter()
                .map(Media::try_from)
                .collect::<Result<Vec<_>, _>>()
                .context("extract media")?,
        })
    }
}

// -----------------------------------------------------------------------------

/// Serie ID on Piccoma.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct SerieID(u32);

impl fmt::Display for SerieID {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl From<u32> for SerieID {
    fn from(value: u32) -> Self {
        Self(value)
    }
}

impl FromStr for SerieID {
    type Err = eyre::Report;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        value.parse::<u32>().context("invalid serie ID").map(Self)
    }
}