timeline.rs (1378B)
1 use clap::ArgMatches;
2 use reqwest::Client;
3
4 use std::collections::BinaryHeap;
5
6 use crate::config::Config;
7 use crate::entry::Entry;
8
9 /// Print timeline from following.
10 pub fn timeline(config: &Config, _subcommand: &ArgMatches) {
11 // Store (post_time, nick, content).
12 let mut all_tweets = BinaryHeap::<Entry>::new();
13
14 // Pull and parse twtxt files from user and each followed source.
15 for (nick, twturl) in config
16 .following
17 .iter()
18 .chain(vec![(&config.nick, &config.twturl)].into_iter())
19 {
20 all_tweets.append(&mut parse_twtxt(twturl, nick));
21 }
22
23 // Print the most recent tweets.
24 for _ in 0..config.limit_timeline {
25 if let Some(tweet) = all_tweets.pop() {
26 if config.use_abs_time {
27 println!("\n{}", tweet);
28 } else {
29 println!("\n{:#}", tweet);
30 }
31 }
32 }
33 }
34
35 /// Parses given twtxt url, returns a Vec of (post_time, content).
36 fn parse_twtxt(twturl: &str, nick: &str) -> BinaryHeap<Entry> {
37 let client = Client::new();
38 let mut tweets = BinaryHeap::new();
39
40 if let Ok(resp_text) = client.get(twturl).send().and_then(|mut resp| resp.text()) {
41 for line in resp_text.lines() {
42 if let Ok(parsed) = Entry::parse(line, Some(nick)) {
43 tweets.push(parsed);
44 };
45 }
46 };
47
48 tweets
49 }