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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
use csv::StringRecord;
use rusqlite::Row;
use serde::Deserialize;

use crate::core::RatedRow;

const N_A: &str = "N/A";

#[derive(Debug)]
pub struct CsvRow {
    id: u32,
    title: String,
    year: u32,
    cast: String,
    country: String,
    director: String,
    typ: String,
    duration: String,
    plot: String,
}

#[derive(Deserialize, Debug)]
#[allow(non_snake_case)]
pub struct OmdbSuccessResponseJson {
    Type: String,
    Runtime: String,
    Plot: String,

    Genre: String,
    Language: String,
    Writer: String,

    imdbRating: String,
    imdbVotes: String,
    imdbID: String,
}

#[derive(Deserialize, Debug)]
#[allow(non_snake_case)]
pub struct OmdbErrorResponseJson {
    pub Error: String,
}

fn json_string(s: &str) -> Option<String> {
    match s {
        N_A => None,
        _ => Some(s.to_string()),
    }
}

#[derive(Deserialize, Clone, Debug)]
pub struct JsonRow {
    typ: Option<String>,
    duration: Option<String>,
    plot: Option<String>,

    genre: String,
    language: String,
    writer: String,

    imdb_rating: Option<u32>,
    imdb_votes: Option<u32>,
    imdb_id: Option<String>,
}

impl JsonRow {
    pub fn is_missing_imdb_data(&self) -> bool {
        self.imdb_id.is_none() || self.imdb_rating.is_none()
    }
}

impl From<OmdbSuccessResponseJson> for JsonRow {
    fn from(json: OmdbSuccessResponseJson) -> Self {
        let imdb_rating = match maybe_float(&json.imdbRating) {
            Some(rating) => Some((rating * 10.0).round() as u32),
            None => None,
        };

        let imdb_votes = maybe_uint(&json.imdbVotes.replace(",", ""));
        let imdb_id = json_string(&json.imdbID);

        Self {
            typ: json_string(&json.Type),
            duration: json_string(&json.Runtime),
            plot: json_string(&json.Plot),

            genre: json.Genre,
            language: json.Language,
            writer: json.Writer,

            imdb_rating,
            imdb_votes,
            imdb_id,
        }
    }
}

fn maybe_uint(s: &str) -> Option<u32> {
    match s {
        N_A => None,
        s => s.parse::<u32>().ok(),
    }
}

fn maybe_float(s: &str) -> Option<f32> {
    match s {
        N_A => None,
        s => s.parse::<f32>().ok(),
    }
}

// 0: show_id, 1: type, 2: title, 3: director, 4: cast, 5: country, 6: date_added,
// 7: release_year, 8: rating, 9: duration, 10: listed_in, 11: description
impl From<StringRecord> for CsvRow {
    fn from(x: StringRecord) -> Self {
        Self {
            id: maybe_uint(&x.get(0).unwrap()).unwrap(),
            title: x.get(2).unwrap().to_string(),
            year: maybe_uint(&x.get(7).unwrap()).unwrap(),
            cast: x.get(4).unwrap().to_string(),
            country: x.get(5).unwrap().to_string(),
            director: x.get(3).unwrap().to_string(),
            typ: x.get(1).unwrap().to_string(),
            duration: x.get(9).unwrap().to_string(),
            plot: x.get(11).unwrap().to_string(),
        }
    }
}

impl From<CsvRow> for RatedRow {
    fn from(csv: CsvRow) -> Self {
        RatedRow {
            id: csv.id,
            title: csv.title,
            year: csv.year,
            cast: csv.cast,
            country: csv.country,
            director: csv.director,
            typ: csv.typ,
            duration: csv.duration,
            plot: csv.plot,

            genre: None,
            language: None,
            writer: None,

            imdb_rating: None,
            imdb_votes: None,
            imdb_id: None,

            last_sync: None,
        }
    }
}

pub fn rated_row_from_row(row: &Row) -> RatedRow {
    RatedRow {
        id: row.get(0).unwrap(),
        title: row.get(1).unwrap(),
        year: row.get(2).unwrap(),
        cast: row.get(3).unwrap(),
        country: row.get(4).unwrap(),
        director: row.get(5).unwrap(),
        typ: row.get(6).unwrap(),
        duration: row.get(7).unwrap(),
        plot: row.get(8).unwrap(),
        genre: row.get(9).unwrap(),
        writer: row.get(10).unwrap(),
        language: row.get(11).unwrap(),
        imdb_rating: row.get(12).unwrap(),
        imdb_votes: row.get(13).unwrap(),
        imdb_id: row.get(14).unwrap(),
        last_sync: row.get(15).unwrap(),
    }
}

impl From<(RatedRow, JsonRow, u32)> for RatedRow {
    fn from((rated, json, last_sync): (RatedRow, JsonRow, u32)) -> Self {
        let typ = json.typ.unwrap_or(rated.typ);
        let duration = json.duration.unwrap_or(rated.duration);
        let plot = json.plot.unwrap_or(rated.plot);

        Self {
            id: rated.id,
            title: rated.title,
            year: rated.year,
            cast: rated.cast,
            country: rated.country,
            director: rated.director,
            typ,
            duration,
            plot,
            genre: Some(json.genre),
            writer: Some(json.writer),
            language: Some(json.language),
            imdb_rating: json.imdb_rating,
            imdb_votes: json.imdb_votes,
            imdb_id: json.imdb_id,
            last_sync: Some(last_sync),
        }
    }
}