Skip to content

Commit

Permalink
Import and fetch specified urls (#49)
Browse files Browse the repository at this point in the history
* Import urls

* Fetch specified urls

* Update changelog
  • Loading branch information
quambene authored Nov 17, 2023
1 parent b08d65a commit a1d301e
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 86 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<!-- markdownlint-disable MD041 MD034 -->

### Unrelease

- added
- Implement `bogrep import --urls <URLs>` (import specified URLs)
- Implement `bogrep fetch --urls <URLs>` (fetch specified URLs)

### v0.4.0

- added
Expand Down
18 changes: 17 additions & 1 deletion src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub enum Subcommands {
/// for new bookmarks; delete cache for removed bookmarks.
Update(UpdateArgs),
/// Import bookmarks from the configured source files.
Import,
Import(ImportArgs),
/// Fetch and cache bookmarks.
Fetch(FetchArgs),
/// Clean up cache for removed bookmarks.
Expand Down Expand Up @@ -78,6 +78,16 @@ pub struct SetIgnoredUrls {
pub ignore: Vec<String>,
}

/// Describes the arguments for the `import` subcommand.
#[derive(ClapArgs, Debug)]
pub struct ImportArgs {
/// Import specified URLs as bookmark.
///
/// Multiple URLs are separated by a whitespace.
#[arg(long, num_args = 0.., value_delimiter = ' ')]
pub urls: Vec<String>,
}

/// Describes the arguments for the `fetch` subcommand.
#[derive(ClapArgs, Debug)]
pub struct FetchArgs {
Expand All @@ -97,6 +107,12 @@ pub struct FetchArgs {
/// Multiple urls are separated by a whitespace.
#[arg(short, long, value_name = "URLs", num_args = 0.., value_delimiter = ' ')]
pub diff: Vec<String>,
/// Fetch and cache specified URLs.
///
/// Multiple URLs are separated by a whitespace.
/// If an URL is missing in the bookmarks, it will be imported.
#[arg(long, num_args = 0.., value_delimiter = ' ')]
pub urls: Vec<String>,
}

/// Describes the arguments for the `init` subcommand.
Expand Down
8 changes: 4 additions & 4 deletions src/bookmarks/target_bookmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,16 @@ impl TargetBookmarks {
/// the target bookmarks.
pub fn update(
&mut self,
source_bookmarks: SourceBookmarks,
source_bookmarks: &SourceBookmarks,
) -> Result<(Vec<TargetBookmark>, Vec<TargetBookmark>), anyhow::Error> {
if self.bookmarks.is_empty() {
self.bookmarks = Self::from(source_bookmarks.clone()).bookmarks;
return Ok((vec![], vec![]));
}

let now = Utc::now();
let urls_to_add = self.filter_to_add(&source_bookmarks);
let bookmarks_to_remove = self.filter_to_remove(&source_bookmarks);
let urls_to_add = self.filter_to_add(source_bookmarks);
let bookmarks_to_remove = self.filter_to_remove(source_bookmarks);
let mut bookmarks_to_add = vec![];

for url in urls_to_add {
Expand Down Expand Up @@ -208,7 +208,7 @@ mod tests {
None,
)],
};
let res = target_bookmarks.update(source_bookmarks);
let res = target_bookmarks.update(&source_bookmarks);
assert!(res.is_ok());
assert_eq!(
target_bookmarks
Expand Down
54 changes: 44 additions & 10 deletions src/cmd/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
use chrono::Utc;
use colored::Colorize;
use futures::{stream, StreamExt};
use log::{debug, trace, warn};
use log::{debug, info, trace, warn};
use similar::{ChangeTag, TextDiff};

/// Fetch and cache bookmarks.
Expand All @@ -17,15 +17,26 @@ pub async fn fetch(config: &Config, args: &FetchArgs) -> Result<(), anyhow::Erro
let mut target_reader = utils::open_file_in_read_mode(&config.target_bookmark_file)?;
let mut target_writer = utils::open_and_truncate_file(&config.target_bookmark_lock_file)?;

fetch_and_cache(
&client,
&cache,
&mut target_reader,
&mut target_writer,
config.settings.max_concurrent_requests,
args.all,
)
.await?;
if args.urls.is_empty() {
fetch_and_cache(
&client,
&cache,
&mut target_reader,
&mut target_writer,
config.settings.max_concurrent_requests,
args.all,
)
.await?;
} else {
fetch_urls(
&args.urls,
&client,
&cache,
&mut target_reader,
&mut target_writer,
)
.await?;
}

utils::close_and_rename(
(target_writer, &config.target_bookmark_lock_file),
Expand All @@ -35,6 +46,29 @@ pub async fn fetch(config: &Config, args: &FetchArgs) -> Result<(), anyhow::Erro
Ok(())
}

pub async fn fetch_urls(
urls: &[String],
client: &impl Fetch,
cache: &impl Caching,
target_reader: &mut impl ReadTarget,
target_writer: &mut impl WriteTarget,
) -> Result<(), anyhow::Error> {
let now = Utc::now();
let mut target_bookmarks = TargetBookmarks::default();
target_reader.read(&mut target_bookmarks)?;

for url in urls {
let mut bookmark = TargetBookmark::new(url, now, None);
fetch_and_add(client, cache, &mut bookmark, true).await?;
info!("Fetched website for {url}");
target_bookmarks.add(&bookmark);
}

target_writer.write(&target_bookmarks)?;

Ok(())
}

pub async fn fetch_and_cache(
client: &impl Fetch,
cache: &impl Caching,
Expand Down
Loading

0 comments on commit a1d301e

Please sign in to comment.