Browse code

Store the more recent favourite and retrieve everything TILL this element

Julio Biason authored on 29/05/2019 11:09:00
Showing 4 changed files

... ...
@@ -7,5 +7,6 @@
7 7
 **/*.rs.bk
8 8
 
9 9
 mastodon.toml
10
+downfav.toml
10 11
 
11 12
 data
... ...
@@ -279,6 +279,9 @@ dependencies = [
279 279
  "html2md 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)",
280 280
  "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
281 281
  "reqwest 0.9.17 (registry+https://github.com/rust-lang/crates.io-index)",
282
+ "serde 1.0.91 (registry+https://github.com/rust-lang/crates.io-index)",
283
+ "serde_derive 1.0.91 (registry+https://github.com/rust-lang/crates.io-index)",
284
+ "toml 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
282 285
 ]
283 286
 
284 287
 [[package]]
... ...
@@ -6,7 +6,10 @@ edition = "2018"
6 6
 
7 7
 [dependencies]
8 8
 elefren = { version = "0.19", features = ["toml"] }
9
+env_logger = "0.6.1"
9 10
 html2md = "0.2.9"
10
-reqwest = "0.9.17"
11 11
 log = "0.4.6"
12
-env_logger = "0.6.1"
12
+reqwest = "0.9.17"
13
+serde = "*"
14
+serde_derive = "*"
15
+toml = "0.5"
... ...
@@ -6,22 +6,26 @@ use std::path::PathBuf;
6 6
 use elefren::entities::attachment::Attachment;
7 7
 use elefren::entities::status::Status;
8 8
 use elefren::helpers::cli;
9
-use elefren::helpers::toml;
9
+use elefren::helpers::toml as elefren_toml;
10 10
 use elefren::prelude::*;
11 11
 
12 12
 use reqwest;
13 13
 
14 14
 use log;
15 15
 use env_logger;
16
+use toml;
17
+use serde_derive::Serialize;
18
+use serde_derive::Deserialize;
16 19
 
17
-// struct Config {
18
-//     last_favorite: Option<u>
19
-// }
20
+#[derive(Serialize, Deserialize)]
21
+struct Config {
22
+    last_favorite: String
23
+}
20 24
 
21 25
 fn main() {
22 26
     env_logger::init();
23 27
 
24
-    let client = if let Ok(data) = toml::from_file("mastodon.toml") {
28
+    let client = if let Ok(data) = elefren_toml::from_file("mastodon.toml") {
25 29
         Mastodon::from(data)
26 30
     } else {
27 31
         let registration = Registration::new("https://functional.cafe")
... ...
@@ -29,20 +33,20 @@ fn main() {
29 33
             .build()
30 34
             .unwrap();
31 35
         let mastodon = cli::authenticate(registration).unwrap();
32
-        toml::to_file(&*mastodon, "mastodon.toml").unwrap();
36
+        elefren_toml::to_file(&*mastodon, "mastodon.toml").unwrap();
33 37
         mastodon
34 38
     };
35 39
 
36
-    // let last_favorite = if let Ok(fp) 
40
+    let top = get_top_favourite();
37 41
 
38
-    let top = "";
39 42
     log::info!("Starting up...");
43
+    log::debug!("Going all the way till {}", top);
44
+
40 45
     let most_recent_favourite = client
41 46
         .favourites()
42 47
         .unwrap()
43 48
         .items_iter()
44 49
         .take_while(|record| record.id != top)
45
-        .take(2)
46 50
         .map(|record| { dump_record(&record); record })
47 51
         .fold(None, {|first, current| {
48 52
             if let Some(_) = first {
... ...
@@ -52,10 +56,32 @@ fn main() {
52 56
             }
53 57
         }});
54 58
     log::debug!("First favourite: {:?}", most_recent_favourite);
59
+
60
+    if let Some(id) = most_recent_favourite {
61
+        let new_configuration = Config { last_favorite: id };
62
+        let content = toml::to_string(&new_configuration).unwrap();
63
+
64
+        if let Ok(mut fp) = File::create("downfav.toml") {
65
+            fp.write_all(content.as_bytes()).unwrap();
66
+        }
67
+    }
68
+}
69
+
70
+fn get_top_favourite() -> String {
71
+    if let Ok(mut fp) = File::open("downfav.toml") {
72
+        let mut contents = String::new();
73
+        fp.read_to_string(&mut contents).unwrap();
74
+        
75
+        let config: Config = toml::from_str(&contents)
76
+            .unwrap_or( Config { last_favorite: "".to_string() } );
77
+        config.last_favorite
78
+    } else {
79
+        "".to_string()
80
+    }
55 81
 }
56 82
 
57 83
 fn dump_record(record: &Status) -> () {
58
-    log::debug!("Retriving record {}", record.id);
84
+    log::debug!("Retrieving record {}", record.id);
59 85
     log::debug!("Content: {:?}", record);
60 86
     create_structure(&record);
61 87
     save_content(&record);
... ...
@@ -104,7 +130,7 @@ fn save_attachment(attachment: &Attachment, base_path: &PathBuf) -> () {
104 130
 fn get_attachment_filename(url: &str) -> String {
105 131
     let mut frags = url.rsplitn(2, '/');
106 132
     log::debug!("URL fragments: {:?}", frags);
107
-    if let Some(path_part) = frags.next() { 
133
+    if let Some(path_part) = frags.next() {
108 134
         log::debug!("Found path in the attachment URL: {:?}", path_part);
109 135
         path_part
110 136
             .split('?')