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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
use crate::assets::css::build_css;
use crate::provider::{Insertable, OnlineProvider, ProviderPocket};
use chrono_tz::Tz;
use clap::Parser;
use cli::{
    AuthArgs, CliArgs, FetchArgs, LocalAddArgs, LocalCommands, LocalFavoriteArgs,
    PocketAddArgs, PocketCommands, PocketFavoriteArgs, Subcommands,
};
use db::{ResearchItem, Tags, DB};
use provider::local::LocalItem;
use site::Site;
use sqlx::migrate::MigrateDatabase;
use std::env;
use std::path::Path;
use std::str::FromStr;
use tokio::fs::{create_dir, metadata, read_to_string, File};
use tokio::io::AsyncWriteExt;
use util::absolute_path;

mod assets;
mod cli;
mod db;
mod handler;
mod provider;
mod site;
mod util;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let cli_args = CliArgs::parse();

    match &cli_args.subcommand {
        Some(Subcommands::Pocket { command }) => {
            handle_pocket_command(command, &cli_args).await?
        }
        Some(Subcommands::Local { command }) => {
            handle_local_command(command, &cli_args).await?
        }
        Some(Subcommands::Fetch { limit }) => handle_fetch_command(&cli_args, *limit).await?,
        Some(Subcommands::List {
            tags,
            limit,
            favorite_only,
            timezone,
        }) => {
            let favorite = if *favorite_only { Some(true) } else { None };
            let timezone = timezone
                .as_ref()
                .and_then(|tz_str| Tz::from_str(tz_str).ok());
            handle_list_command(&cli_args, tags.as_ref(), favorite, *limit, timezone).await?
        }
        Some(Subcommands::Init { path }) => handle_init_command(path, &cli_args).await?,
        Some(Subcommands::Generate {
            output,
            assets,
            download_tailwind,
            timezone,
        }) => {
            let timezone = timezone
                .as_ref()
                .and_then(|tz_str| Tz::from_str(tz_str).ok());
            handle_generate_command(output, assets, *download_tailwind, timezone, &cli_args)
                .await?
        }
        Some(Subcommands::Export { raindrop }) => {
            if *raindrop {
                let db = DB::init(&cli_args.db).await.map_err(|err| {
                    match err {
                        sqlx::Error::Database(..) => {
                            eprintln!("Database not found");
                            eprintln!("Please set the database corrdct path with --db");
                            eprintln!(
                                "Or consider initializing the database with the 'init' command"
                            );
                        }
                        _ => {
                            eprintln!("Unknown error: {err:?}");
                        }
                    }
                    err
                })?;
                db.export_to_csv("raindrop_export.csv").await?;
                println!("Exported to raindrop_export.csv");
            }
        }
        Some(Subcommands::Handle {
            register,
            unregister,
            url,
        }) => {
            if *register {
                handler::platform_register_url();
            } else if *unregister {
                handler::platform_unregister_url();
            } else if let Some(url) = url {
                handler::handle_url(url).await?;
            }
        }
        None => {
            eprintln!("No subcommand provided");
            eprintln!("Please provide a subcommand");
            eprintln!("Run with --help for more information");
        }
    }
    Ok(())
}

async fn handle_local_command(
    command: &LocalCommands,
    cli_args: &CliArgs,
) -> Result<(), Box<dyn std::error::Error>> {
    let db = DB::init(&cli_args.db).await.map_err(|err| {
        match err {
            sqlx::Error::Database(..) => {
                eprintln!("Database not found");
                eprintln!("Please set the database corrdct path with --db");
                eprintln!("Or consider initializing the database with the 'init' command");
            }
            _ => {
                eprintln!("Unknown error: {err:?}");
            }
        }
        err
    })?;
    let provider_id = db.get_provider_id("local").await?;
    match command {
        LocalCommands::Add(LocalAddArgs {
            uri,
            tag,
            title,
            excerpt,
        }) => {
            println!("{uri} {title:?} {excerpt:?} {tag:?}");
            let tags: Vec<Tags> = tag.as_ref().map_or(Vec::new(), |tags| {
                tags.iter()
                    .map(|tag| Tags {
                        tag_name: tag.clone(),
                    })
                    .collect()
            });

            let metadata = handler::fetch_metadata(uri).await?;
            let local_item = LocalItem {
                id: None,
                uri: uri.to_string(),
                title: Some(title.clone().map_or(metadata.title, |title| title.clone())),
                excerpt: Some(
                    excerpt
                        .clone()
                        .map_or(metadata.description, |excerpt| excerpt.clone()),
                ),
                time_added: chrono::Utc::now().timestamp(),
                tags: tags.clone(),
            };

            db.insert_item(local_item.to_research_item(), &tags, provider_id)
                .await?;
            println!("Inserted document successfully!");
        }
        LocalCommands::List => {
            let items = db.get_all_items_by_provider(provider_id).await?;
            println!("Items: {:?}", items.len());
            for item in items {
                println!("{:?}", item);
            }
        }
        LocalCommands::Favorite(LocalFavoriteArgs { uri, mark }) => {
            let item_id = db
                .get_item_id(uri)
                .await?
                .expect("Item uri not found in the database");
            db.mark_as_favorite(item_id, *mark).await?;
            println!("Item marked as favorite: {mark}");
        }
    }
    Ok(())
}

async fn handle_pocket_command(
    pocket_command: &PocketCommands,
    cli_args: &CliArgs,
) -> Result<(), Box<dyn std::error::Error>> {
    match pocket_command {
        PocketCommands::Auth(AuthArgs { key }) => {
            // Handle authentication with the provided consumer key
            let db = DB::init(&cli_args.db).await.map_err(|err| {
                match err {
                    sqlx::Error::Database(..) => {
                        eprintln!("Database not found");
                        eprintln!("Please set the database corrdct path with --db");
                        eprintln!(
                            "Or consider initializing the database with the 'init' command"
                        );
                    }
                    _ => {
                        eprintln!("Unknown error: {err:?}");
                    }
                }
                err
            })?;

            let provider = ProviderPocket {
                consumer_key: key.to_string(),
                ..Default::default()
            };
            let secrets = provider.authenticate().await?;
            db.set_secret(secrets).await?;
            println!("Success: Access token saved to the database! You can now run `pocket fetch` to fetch items from Pocket.")
        }
        PocketCommands::Fetch(FetchArgs { key, access, limit }) => {
            // Handle fetching items from Pocket with the provided keys
            fetch_from_pocket(&cli_args.db, key.to_string(), access.to_string(), *limit)
                .await?;
        }
        PocketCommands::Add(PocketAddArgs {
            add_args: LocalAddArgs { uri, tag, .. },
            key,
            access,
        }) => {
            // Handle adding an item to Pocket with the provided URI and tags
            let db = DB::init(&cli_args.db).await.map_err(|err| {
                match err {
                    sqlx::Error::Database(..) => {
                        eprintln!("Database not found");
                        eprintln!("Please set the database corrdct path with --db");
                        eprintln!(
                            "Or consider initializing the database with the 'init' command"
                        );
                    }
                    _ => {
                        eprintln!("Unknown error: {err:?}");
                    }
                }
                err
            })?;
            let secrets = db.get_secrets().await?;
            let consumer_key = secrets.pocket_consumer_key.or(key.clone()).expect(
                "Consumer key not found in the database, consider generating one from https://getpocket.com/developer/apps/new and running `pocket auth`",
            );
            let access_token = secrets.pocket_access_token.or(access.clone()).expect(
                "Access token not found in the database, consider running 'pocket auth'",
            );
            let provider = ProviderPocket {
                consumer_key,
                access_token: Some(access_token),
                ..Default::default()
            };
            let item_id = provider
                .add_item(
                    uri,
                    tag.as_ref().map_or(Vec::new(), |tags| {
                        tags.iter().map(|tag| tag.as_str()).collect()
                    }),
                )
                .await?;

            // add item to db
            let tags: Vec<Tags> = tag.as_ref().map_or(Vec::new(), |tags| {
                tags.iter()
                    .map(|tag| Tags {
                        tag_name: tag.clone(),
                    })
                    .collect()
            });

            println!("Saving item to database");
            let metadata = handler::fetch_metadata(uri).await?;
            let insertable_item = ResearchItem {
                id: item_id,
                uri: uri.to_string(),
                title: metadata.title,
                excerpt: metadata.description,
                time_added: chrono::Utc::now().timestamp(),
                favorite: false,
                lang: None,
            };
            let provider_id = db.get_provider_id("pocket").await?;
            println!("Item: {insertable_item:?}");
            db.insert_item(insertable_item, &tags, provider_id).await?;
        }
        PocketCommands::Favorite(PocketFavoriteArgs {
            fav_args:
                LocalFavoriteArgs {
                    uri,
                    mark: favorite,
                },
            access,
            key,
        }) => {
            // Handle adding an item to Pocket with the provided URI and tags
            let db = DB::init(&cli_args.db).await.map_err(|err| {
                match err {
                    sqlx::Error::Database(..) => {
                        eprintln!("Database not found");
                        eprintln!("Please set the database corrdct path with --db");
                        eprintln!(
                            "Or consider initializing the database with the 'init' command"
                        );
                    }
                    _ => {
                        eprintln!("Unknown error: {err:?}");
                    }
                }
                err
            })?;
            let secrets = db.get_secrets().await?;
            let consumer_key = secrets.pocket_consumer_key.or(key.clone()).expect(
                "Consumer key not found in the database, consider generating one from https://getpocket.com/developer/apps/new and running `pocket auth`",
            );
            let access_token = secrets.pocket_access_token.or(access.clone()).expect(
                "Access token not found in the database, consider running 'pocket auth'",
            );
            let provider = ProviderPocket {
                consumer_key,
                access_token: Some(access_token),
                ..Default::default()
            };
            let item_id = db
                .get_item_id(uri)
                .await?
                .expect("Item uri not found in the database");
            provider.mark_as_favorite(item_id, *favorite).await?;
            db.mark_as_favorite(item_id, *favorite).await?;
            println!("Item marked as favorite: {favorite}");
        }
    }
    Ok(())
}

async fn handle_fetch_command(
    cli_args: &CliArgs,
    limit: Option<usize>,
) -> Result<(), Box<dyn std::error::Error>> {
    // Handle fetching data from authenticated providers
    let db = DB::init(&cli_args.db).await.map_err(|err| {
        match err {
            sqlx::Error::Database(..) => {
                eprintln!("Database not found");
                eprintln!("Please set the database corrdct path with --db");
                eprintln!("Or consider initializing the database with the 'init' command");
            }
            _ => {
                eprintln!("Unknown error: {err:?}");
            }
        }
        err
    })?;
    let secrets = db.get_secrets().await?;
    let consumer_key = secrets.pocket_consumer_key.expect("Consumer key not found in the database, consider generating one from https://getpocket.com/developer/apps/new and running `pocket auth`");
    let access_token = secrets
        .pocket_access_token
        .expect("Access token not found in the database, consider running 'pocket auth'");
    fetch_from_pocket(&cli_args.db, consumer_key, access_token, limit).await?;
    Ok(())
}

async fn handle_list_command(
    cli_args: &CliArgs,
    tags: Option<&Vec<String>>,
    favorite: Option<bool>,
    limit: Option<usize>,
    timezone: Option<Tz>,
) -> Result<(), Box<dyn std::error::Error>> {
    // Handle listing items in the database
    let db = DB::init(&cli_args.db).await.map_err(|err| {
        match err {
            sqlx::Error::Database(..) => {
                eprintln!("Database not found");
                eprintln!("Please set the database corrdct path with --db");
                eprintln!("Or consider initializing the database with the 'init' command");
            }
            _ => {
                eprintln!("Unknown error: {err:?}");
            }
        }
        err
    })?;
    let mut items: Vec<ResearchItem>;
    if let Some(tags) = tags {
        items = db.get_all_items_by_tags(tags, favorite).await?;
        println!("Tags: {:?}", tags);
        println!("Total items: {}", items.len());
        if let Some(limit) = limit {
            items.truncate(limit);
        }
    } else {
        items = db.get_all_items(favorite).await?;
        println!("Total items: {}", items.len());
        if let Some(limit) = limit {
            items.truncate(limit);
        }
    }
    println!("Displaying {} items:", items.len());
    for item in items {
        println!("Research Item");
        println!("-------------");
        if let Some(id) = item.id {
            println!("ID: {}", id);
        }
        println!("{}", item.to_display_with_timezone(timezone));
    }
    Ok(())
}

async fn handle_init_command(
    db_path: &str,
    _cli_args: &CliArgs,
) -> Result<(), Box<dyn std::error::Error>> {
    // Handle initializing the database at the provided path
    let db_url = {
        let path = Path::new(&db_path).join("research.sqlite");
        path.to_str().expect("Invalid db path").to_owned()
    };
    eprintln!("Creating new database: {db_url}");
    sqlx::Sqlite::create_database(&db_url).await?;
    let pool = sqlx::SqlitePool::connect(&db_url).await?;
    DB::migrate(&pool).await?;
    eprintln!("Database created and migrated successfully!");
    Ok(())
}

async fn handle_generate_command(
    output_dir: &str,
    assets_dir: &str,
    download_tailwind: bool,
    timezone: Option<Tz>,
    cli_args: &CliArgs,
) -> Result<(), Box<dyn std::error::Error>> {
    // Handle generating a static site with the provided options
    metadata(&assets_dir)
        .await
        .unwrap_or_else(|_| panic!("Invalid assets directory: {assets_dir}"));
    const REQUIRED_FILES: [&str; 2] = ["main.css", "search.js"];
    for file in REQUIRED_FILES {
        metadata(&Path::new(&assets_dir).join(file))
            .await
            .unwrap_or_else(|_| panic!("Missing required file in assets directory: {file}"));
    }

    let output_dir = Path::new(output_dir);
    if !output_dir.exists() {
        create_dir(output_dir).await?;
    }

    let db = DB::init(&cli_args.db).await.inspect_err(|_err| {
        eprintln!("Please set the corrdct database path with --db");
    })?;
    let tags = db.get_all_tags().await?;
    let item_tags = db.get_all_item_tags().await?;

    let site = Site::build(&tags, &item_tags, "./assets", timezone)?;

    eprintln!("Output directory: {output_dir:?}");
    let mut index = File::create(output_dir.join("index.html")).await?;
    index.write_all(site.index_html.as_bytes()).await?;

    let mut search = File::create(output_dir.join("search.html")).await?;
    search.write_all(site.search_html.as_bytes()).await?;

    build_css(
        output_dir,
        &absolute_path(
            env::current_dir().expect("Failed to get current directory"),
            Path::new(assets_dir),
        ),
        download_tailwind,
        4,
    )
    .await?;

    let search_js = Path::new(assets_dir).join("search.js");
    let mut search = File::create(output_dir.join("assets").join("search.js")).await?;
    search
        .write_all(read_to_string(&search_js).await?.as_bytes())
        .await?;

    Ok(())
}

async fn fetch_from_pocket(
    db_url: &str,
    consumer_key: String,
    access_token: String,
    limit: Option<usize>,
) -> Result<(), Box<dyn std::error::Error>> {
    let provider = ProviderPocket {
        consumer_key,
        access_token: Some(access_token),
        ..Default::default()
    };

    let db = DB::init(db_url).await?;
    println!("Sqlite version: {}", db.get_sqlite_version().await?);

    let provider_id = db.get_provider_id("pocket").await?;
    let items = provider.fetch_items(limit).await?;
    eprintln!("Items: {}", items.len());

    for item in items {
        let insertable_item = item.to_research_item();
        let tags = item.to_tags();
        let title = &insertable_item.title.chars().take(8).collect::<String>();
        eprint!("{:?} ", title);
        db.insert_item(insertable_item, &tags, provider_id).await?;
    }

    Ok(())
}