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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
use ansi_term::Style;
use chrono::offset::Utc;
use chrono::DateTime;
use dialoguer::{theme::SimpleTheme, MultiSelect};
use feed_rs::parser;
use futures::stream::StreamExt;
use itertools::Itertools;
use reqwest::Client;
use serde::{Deserialize, Serialize};
use std::cell::RefCell;
use std::fs;
use std::fs::File;
use std::io::Write;
use std::path::Path;

#[derive(Debug, Clone)]
pub struct ProcessedFeed {
    pub title: String,
    pub items: Vec<String>,
}

impl std::fmt::Display for ProcessedFeed {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "📖 {}\n\t{}",
            Style::new().bold().paint(&self.title),
            format!("{}", self.items.iter().format("\n\t"))
        )
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Feed {
    pub uri: String,
    pub last_accessed: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ConfigObj {
    pub feeds: Vec<Feed>,
}

pub fn find_config() -> std::path::PathBuf {
    let homedir: std::path::PathBuf = dirs::home_dir().expect("no home dir");
    //let path_to_config: &Path =
    Path::new(&homedir).join(".hemrc")
}

fn config_to_rust() -> Result<ConfigObj, Box<dyn std::error::Error>> {
    let config_path = find_config();
    let config = match fs::read_to_string(&config_path) {
        Ok(config) => config,
        Err(e) => {
            if e.kind() == std::io::ErrorKind::NotFound {
                eprintln!("Didn't find a .hemrc, creating it now...");
                // create the file and populate it with an empty array
                let mut configfile = fs::OpenOptions::new()
                    .read(true)
                    .write(true)
                    .create_new(true)
                    .open(&config_path)?;
                configfile.write_all(r#"{"feeds": []}"#.as_bytes())?;
                let contents = String::from(r#"{"feeds": []}"#);
                contents
            } else {
                return Err(Box::from("Catastrophe!"));
            }
        }
    };
    Ok(serde_json::from_str(&config).unwrap())
}

pub fn rust_to_config(content: &[u8]) {
    let mut file = match File::create(find_config()) {
        Err(why) => panic!("config file access failed: {}", why),
        Ok(file) => file,
    };
    file.write_all(content)
        .expect("Writing to .hemrc failed :(");
}

pub fn add_feed(feed: &str) {
    let mut my_feeds: ConfigObj = config_to_rust().unwrap();
    my_feeds.feeds.push(Feed {
        uri: feed.to_owned(),
        last_accessed: Utc::now().to_rfc3339().to_owned(),
    });
    rust_to_config(serde_json::to_string(&my_feeds).unwrap().as_bytes());
}

pub fn list_feeds() {
    let config: ConfigObj = config_to_rust().unwrap();
    // let mut uris: Vec<String> = Vec::new();
    for f in config.feeds {
        println!("{}", f.uri);
    }
}

pub fn get_uris_and_update() -> Vec<Feed> {
    let mut config = config_to_rust().unwrap();
    let mut uris: Vec<Feed> = Vec::new();
    let len = config.feeds.len();
    for i in 0..len {
        let x = config.feeds[i].to_owned();
        uris.push(x);
        config.feeds[i].last_accessed = Utc::now().to_rfc3339().to_owned();
    }
    rust_to_config(serde_json::to_string(&config).unwrap().as_bytes());
    uris
}

pub fn remove() {
    let mut config: ConfigObj = config_to_rust().unwrap();
    let mut uris: Vec<String> = Vec::new();
    let feeds_list = &config.feeds;
    for f in feeds_list {
        uris.push(f.uri.clone());
    }
    let multiselected = uris;
    let mut selections = MultiSelect::with_theme(&SimpleTheme)
        .with_prompt("Use arrow keys to move up or down. Press the space bar to select a feed. Press enter when you're done to remove all selected feeds")
        .items(&multiselected[..])
        .interact()
        .unwrap();

    // println!("{:?}", selections);
    if selections.is_empty() {
        println!("You did not select anything :(");
    } else {
        println!("Removing these feeds:");
        selections.reverse();
        for selection in selections {
            println!("  {}", multiselected[selection]);
            config.feeds.remove(selection);
        }
    }
    rust_to_config(serde_json::to_string(&config).unwrap().as_bytes())
}

pub async fn read_feed_fast(num: usize) -> Result<Vec<ProcessedFeed>, Box<dyn std::error::Error>> {
    let client = &Client::builder().build()?;

    let config_obj = config_to_rust().unwrap();
    if config_obj.feeds.len() == 0 {
        return Err(Box::from(
            "Your feeds list is empty! use `hem add` to add a feed.",
        ));
    };
    let processed = RefCell::new(Vec::<ProcessedFeed>::new());
    let fetches = futures::stream::iter(config_obj.feeds.into_iter().map(|feed| {
        let y = &processed;
        async move {
            match client.get(&feed.uri).send().await {
                Ok(resp) => match resp.text().await {
                    Ok(text) => {
                        let feed = parser::parse(text.as_bytes()).unwrap();
                        let title = feed.title.unwrap();
                        let title_owned = title.content.to_owned();

                        let entries = feed.entries.iter().enumerate();
                        let mut processed_items = Vec::<String>::new();
                        for (j, e) in entries {
                            if j < num {
                                let e_title = e.title.as_ref().unwrap();
                                processed_items.push(format!(
                                    "{} \n\t  {}\n",
                                    Style::new().italic().paint(e_title.content.clone()),
                                    e.links[0].href
                                ));
                            } else {
                                break;
                            }
                        }
                        let feed_to_add = ProcessedFeed {
                            title: title_owned,
                            items: processed_items,
                        };
                        y.borrow_mut().push(feed_to_add);
                    }
                    Err(_) => {
                        println!("ERROR reading {}", feed.uri);
                    }
                },
                Err(_) => {
                    println!("ERROR reading {}", feed.uri);
                }
            };
        }
    }))
    .buffer_unordered(20)
    .collect::<Vec<()>>();

    fetches.await;
    let x = processed.borrow();
    Ok(x.to_vec())
}

pub async fn read_feed_fast_duration() -> Result<Vec<ProcessedFeed>, Box<dyn std::error::Error>> {
    let client = &Client::builder().build()?;

    // let config_obj = config_to_rust().unwrap();
    // if config_obj.feeds.len() == 0 {
    //     return Err(Box::from(
    //         "Your feeds list is empty! use `hem add` to add a feed.",
    //     ));
    // };
    let uris = get_uris_and_update();
    if uris.len() == 0 {
        return Err(Box::from(
            "Your feeds list is empty! use `hem add` to add a feed.",
        ));
    }
    let processed = RefCell::new(Vec::<ProcessedFeed>::new());
    let fetches = futures::stream::iter(uris.into_iter().map(|config_feed| {
        let y = &processed;
        async move {
            match client.get(&config_feed.uri).send().await {
                Ok(resp) => match resp.text().await {
                    Ok(text) => {
                        let feed = parser::parse(text.as_bytes()).unwrap();
                        let last_accessed_parsed = DateTime::from(
                            DateTime::parse_from_rfc3339(&config_feed.last_accessed).unwrap(),
                        );
                        let title = feed.title.unwrap();
                        let title_owned = title.content.to_owned();

                        let entries = feed.entries.iter().enumerate();
                        let mut processed_items = Vec::<String>::new();
                        let mut entry_date;
                        for (j, e) in entries {
                            if e.updated.is_none() {
                                entry_date = e.published.unwrap();
                            } else {
                                entry_date = e.updated.unwrap();
                            }
                            let entry_duration = last_accessed_parsed - entry_date; //e.updated.unwrap();
                            if j < 5 && entry_duration.num_seconds() < 0 {
                                let e_title = e.title.as_ref().unwrap();
                                processed_items.push(format!(
                                    "{} \n\t  {}\n",
                                    Style::new().italic().paint(e_title.content.clone()),
                                    e.links[0].href
                                ));
                            } else {
                                break;
                            }
                        }
                        if processed_items.len() == 0 {
                            processed_items = vec![String::from("Nothing new here...")];
                        }
                        let feed_to_add = ProcessedFeed {
                            title: title_owned,
                            items: processed_items,
                        };
                        y.borrow_mut().push(feed_to_add);
                    }
                    Err(_) => {
                        println!("ERROR reading {}", config_feed.uri);
                    }
                },
                Err(_) => {
                    println!("ERROR reading {}", config_feed.uri);
                }
            };

            // config_feed.last_accessed = Utc::now().to_rfc3339().to_owned();
        }
    }))
    .buffer_unordered(20)
    .collect::<Vec<()>>();
    fetches.await;
    let x = processed.borrow();
    // rust_to_config(serde_json::to_string(&config_obj).unwrap().as_bytes());
    Ok(x.to_vec())
}