research 0.1.22

Manage your reading lists and generate a static site with your saved articles.
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
use crate::util::serialize::from_str;
use crate::util::serialize::option_bool_from_int_string;
use crate::util::serialize::option_status_from_int_string;
use crate::util::serialize::option_string_date_unix_timestamp_format;
use crate::util::serialize::optional_vec_from_map;
use crate::util::serialize::serialize_as_string;
use crate::util::serialize::to_comma_delimited_string;
use crate::util::serialize::try_url_from_string;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use url::Url;

#[derive(Serialize)]
struct PocketOAuthRequest<'a> {
    consumer_key: &'a str,
    redirect_uri: &'a str,
    state: Option<&'a str>,
}

#[derive(Deserialize, Debug)]
struct PocketOAuthResponse {
    code: String,
    #[allow(dead_code)]
    state: Option<String>,
}

#[derive(Serialize)]
struct PocketAuthorizeRequest<'a> {
    consumer_key: &'a str,
    code: &'a str,
}

#[derive(Deserialize, Debug)]
#[allow(dead_code)]
struct PocketAuthorizeResponse {
    access_token: String,
    username: String,
    state: Option<String>,
}

/// Authenticate with Pocket
/// Returns the access token
pub async fn login(
    client: &reqwest::Client,
    consumer_key: &str,
) -> Result<String, Box<dyn std::error::Error>> {
    let body = PocketOAuthRequest {
        consumer_key,
        redirect_uri: "0.0.0.0",
        state: Some("pocket-research"),
    };
    let req = client
        .post("https://getpocket.com/v3/oauth/request")
        .json(&body)
        .header("X-Accept", "application/json")
        .send()
        .await?;
    let resp = req.json::<PocketOAuthResponse>().await?;
    let code = resp.code;

    let authorize_url = {
        let params = vec![
            ("request_token", code.clone()),
            ("redirect_uri", "0.0.0.0".into()),
        ];
        let mut url = Url::parse("https://getpocket.com/auth/authorize").unwrap();
        url.query_pairs_mut().extend_pairs(params.into_iter());
        url
    };

    println!("Follow the url to provide access:\n{}", authorize_url);
    println!("Press enter to continue...");
    let _ = std::io::stdin().read_line(&mut String::new());

    let body = &PocketAuthorizeRequest {
        consumer_key,
        code: &code,
    };

    let req = client
        .post("https://getpocket.com/v3/oauth/authorize")
        .json(&body)
        .header("X-Accept", "application/json")
        .send()
        .await?;

    let resp: PocketAuthorizeResponse = req.json().await?;
    println!("{:?}", resp);

    Ok(resp.access_token)
}

#[derive(Serialize)]
pub struct PocketRequest<'a, T> {
    consumer_key: &'a str,
    access_token: &'a str,
    #[serde(flatten)]
    request: T,
}

#[derive(Serialize)]
#[serde(rename_all = "lowercase")]
#[allow(dead_code)]
enum PocketGetState {
    Unread,
    Archive,
    All,
}

#[derive(Serialize, Debug)]
#[serde(rename_all = "lowercase")]
#[allow(dead_code)]
enum PocketGetSort {
    Newest,
    Oldest,
    Title,
    Site,
}

#[derive(Serialize, Debug)]
#[serde(rename_all = "lowercase")]
#[allow(dead_code)]
enum PocketGetDetail {
    Simple,
    Complete,
}

#[derive(Serialize, Default)]
#[serde(rename_all = "camelCase")]
struct PocketGetParams {
    state: Option<PocketGetState>,
    sort: Option<PocketGetSort>,
    detail_type: Option<PocketGetDetail>,
    count: Option<u32>,
    offset: Option<u32>,
}

#[derive(Deserialize, Debug)]
#[serde(untagged)]
enum PocketResponse {
    Success(PocketGetResponse),
    Error { error: String },
}

#[derive(Deserialize, Debug)]
struct PocketGetResponse {
    list: Option<serde_json::Map<String, Value>>,
    // status: Option<u16>,
}

#[derive(Deserialize, Debug, Clone, PartialEq)]
pub struct ItemTag {
    #[serde(deserialize_with = "from_str")]
    pub item_id: u64,
    pub tag: String,
}

#[derive(Deserialize, Debug, PartialEq, Clone)]
pub struct PocketItem {
    #[serde(deserialize_with = "from_str")]
    pub item_id: u64,
    #[serde(default, deserialize_with = "try_url_from_string")]
    pub given_url: Option<Url>,
    #[serde(default)]
    pub given_title: Option<String>,

    #[serde(default, deserialize_with = "option_string_date_unix_timestamp_format")]
    pub time_added: Option<DateTime<Utc>>,
    #[serde(default, deserialize_with = "option_string_date_unix_timestamp_format")]
    pub time_read: Option<DateTime<Utc>>,
    #[serde(default, deserialize_with = "option_string_date_unix_timestamp_format")]
    pub time_updated: Option<DateTime<Utc>>,
    pub resolved_title: Option<String>,

    #[serde(default, deserialize_with = "option_bool_from_int_string")]
    pub favorite: Option<bool>,

    #[serde(default, deserialize_with = "try_url_from_string")]
    pub resolved_url: Option<Url>,

    #[serde(default, deserialize_with = "optional_vec_from_map")]
    pub tags: Option<Vec<ItemTag>>,
    pub excerpt: Option<String>,

    pub lang: Option<String>,
    #[serde(default, deserialize_with = "option_status_from_int_string")]
    pub status: Option<ItemStatus>,
}

#[derive(Debug, Deserialize, PartialEq, Clone)]
#[serde(rename_all = "lowercase")]
pub enum ItemStatus {
    Normal = 0,
    Archived = 1,
    Deleted = 2,
}

/// Isn't very reliable and subject to change without notice
/// @refer https://getpocket.com/developer/docs/v3/retrieve
pub async fn get(
    access_token: &str,
    consumer_key: &str,
    client: &reqwest::Client,
    limit: Option<usize>,
) -> Result<Vec<PocketItem>, Box<dyn std::error::Error>> {
    println!("Starting to fetch Pocket items");
    let mut all_items = Vec::new();
    let mut offset = 0;
    let count = 30; // Maximum items per request
    let mut empty_responses = 0;
    let max_empty_responses = 2; // Maximum number of consecutive empty responses before stopping

    loop {
        println!("Fetching items with offset: {}", offset);
        let body = &PocketRequest {
            access_token,
            consumer_key,
            request: PocketGetParams {
                state: Some(PocketGetState::All),
                sort: Some(PocketGetSort::Newest),
                detail_type: Some(PocketGetDetail::Complete),
                count: Some(count),
                offset: Some(offset),
            },
        };

        let req = client
            .post("https://getpocket.com/v3/get")
            .json(&body)
            .header("X-Accept", "application/json")
            .send()
            .await?;

        let status = req.status();
        println!("Received response with status: {}", status);

        let raw_response = req.text().await?;

        let resp: PocketResponse = serde_json::from_str(&raw_response)
            .map_err(|e| format!("Error parsing {} response: {}", raw_response, e))?;

        match resp {
            PocketResponse::Success(resp_json) => {
                if let Some(list) = resp_json.list {
                    let items: Vec<PocketItem> = list
                        .into_iter()
                        .filter_map(|(key, value)| {
                            match serde_json::from_value::<PocketItem>(value.clone()) {
                                Ok(item) => Some(item),
                                Err(e) => {
                                    // Print the problematic fields
                                    eprintln!("Failed to parse item {}: {}", key, e);
                                    if let Some(obj) = value.as_object() {
                                        for (field, field_value) in obj {
                                            if let Err(field_err) =
                                                serde_json::from_value::<serde_json::Value>(
                                                    field_value.clone(),
                                                )
                                            {
                                                eprintln!(
                                                    "  Field '{}' error: {}",
                                                    field, field_err
                                                );
                                            }
                                        }
                                    }
                                    eprintln!("Raw JSON: {}", value);
                                    None
                                }
                            }
                        })
                        .collect();

                    let items_count = items.len();
                    println!("Parsed {} items from response", items_count);

                    if items.is_empty() {
                        empty_responses += 1;
                        println!(
                            "Received empty list. Empty response count: {}",
                            empty_responses
                        );
                        if empty_responses >= max_empty_responses {
                            println!("Reached maximum number of consecutive empty responses. Breaking loop.");
                            break;
                        }
                    } else {
                        empty_responses = 0; // Reset the counter when we receive items
                        all_items.extend(items);
                    }

                    offset += count;
                    println!("Total items fetched so far: {}", all_items.len());

                    // Check if we've reached the limit
                    if let Some(limit) = limit {
                        if all_items.len() >= limit {
                            println!("Reached item limit. Breaking loop.");
                            all_items.truncate(limit);
                            break;
                        }
                    }
                } else {
                    println!(
                        "Received response with no list field. Continuing to next request."
                    );
                }
            }
            PocketResponse::Error { error } => {
                eprintln!("API returned error: {}", error);
                // Instead of returning immediately, we'll log the error and continue
                println!("Continuing to next request despite error.");
            }
        }

        println!("Sleeping before next request");
        tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
    }

    let all_items: Vec<PocketItem> = all_items
        .into_iter()
        .filter(|item| item.status != Some(ItemStatus::Deleted))
        .collect();

    println!(
        "Finished fetching Pocket items. Total items: {}",
        all_items.len()
    );
    Ok(all_items)
}

#[derive(Serialize, Debug, Clone)]
pub struct PocketAddRequest<'a> {
    pub url: &'a str,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<&'a str>,
    #[serde(serialize_with = "to_comma_delimited_string")]
    pub tags: Option<&'a [&'a str]>,
}

pub async fn add(
    client: &reqwest::Client,
    access_token: &str,
    consumer_key: &str,
    add_request: PocketAddRequest<'_>,
) -> Result<i64, Box<dyn std::error::Error>> {
    println!("Starting Pocket add request");

    let body = &PocketRequest {
        access_token,
        consumer_key,
        request: add_request,
    };

    let response = client
        .post("https://getpocket.com/v3/add")
        .json(&body)
        .header("X-Accept", "application/json")
        .send()
        .await?;

    if response.status().is_success() {
        println!("Successfully added item to Pocket");
        // get the item.item_id from the response
        let item_id = response.json::<serde_json::Value>().await?;
        let item_id = item_id
            .get("item")
            .ok_or("item object not found in response")?;
        let item_id = item_id
            .get("item_id")
            .ok_or("item_id not found in response")?;
        let item_id = item_id.as_str().ok_or("item_id not a string")?;
        Ok(item_id.parse()?)
    } else {
        let error_message = format!(
            "Failed to add item to Pocket. Status: {}",
            response.status()
        );
        println!("{}", error_message);
        Err(error_message.into())
    }
}

#[derive(Serialize)]
struct PocketSendRequest {
    actions: Vec<PocketFavoriteRequest>,
}

#[derive(Serialize)]
#[serde(rename_all = "lowercase")]
enum SendAction {
    Favorite,
    Unfavorite,
}

#[derive(Serialize)]
struct PocketFavoriteRequest {
    #[serde(serialize_with = "serialize_as_string")]
    item_id: i64,
    action: SendAction,
    time: Option<String>,
}

pub async fn favorite(
    client: &reqwest::Client,
    access_token: &str,
    consumer_key: &str,
    item_id: i64,
    mark: bool,
) -> Result<(), Box<dyn std::error::Error>> {
    println!("Starting Pocket favorite request");

    let body = &PocketRequest {
        access_token,
        consumer_key,
        request: PocketSendRequest {
            actions: vec![PocketFavoriteRequest {
                item_id,
                time: None,
                action: if mark {
                    SendAction::Favorite
                } else {
                    SendAction::Unfavorite
                },
            }],
        },
    };

    let response = client
        .post("https://getpocket.com/v3/send")
        .json(&body)
        .header("X-Accept", "application/json")
        .send()
        .await?;

    if response.status().is_success() {
        println!("Successfully marked item as favorite in Pocket");
        Ok(())
    } else {
        let error_message = format!(
            "Failed to mark item as favorite in Pocket. Status: {}",
            response.status()
        );
        println!("{}", error_message);
        Err(error_message.into())
    }
}