Skip to content

Commit a5813d0

Browse files
committed
change NOTION_ROOT_ID to NOTION_ROOT_PAGE, support page link now
1 parent 7f21e31 commit a5813d0

File tree

5 files changed

+43
-15
lines changed

5 files changed

+43
-15
lines changed

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
NOTION_TOKEN =
2+
NOTION_ROOT_PAGE =

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/target
22
.DS_Store
3-
/notion.db
43
/.env
54
/*.log
65
/samples/
6+
/*.db

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ serde_json = "1.0"
1919
futures = "0.3.30"
2020
dotenvy = "0.15.7"
2121
clap = { version = "4.5.16", features = ["derive"] }
22+
http = "1.1.0"
2223

2324
[dependencies.sqlx]
2425
version = "0.8.1"

src/main.rs

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use std::{collections::HashMap, env};
1+
use std::{collections::HashMap, env, path};
22

33
use clap::{Parser, Subcommand};
44
use futures::StreamExt;
5+
use http::Uri;
56
use notion_async::{
67
init_db, insert_or_update_block, insert_or_update_comment, insert_or_update_database,
78
insert_or_update_page,
@@ -14,9 +15,9 @@ use sqlx::SqliteConnection;
1415
#[command(name = "notion-async")]
1516
#[command(version = "0.1")]
1617
struct Cli {
17-
/// Notion integration token, can get from:
18-
/// https://www.notion.so/my-integrations. If it's not set, will read from
19-
/// env var NOTION_TOKEN.
18+
/// Notion integration token, can get from
19+
/// https://www.notion.so/my-integrations. Read from env var NOTION_TOKEN
20+
/// if not set.
2021
#[arg(long)]
2122
token: Option<String>,
2223

@@ -32,16 +33,15 @@ struct Cli {
3233
enum Commands {
3334
/// Sync all pages/databases/comments into db, recursively.
3435
Sync {
35-
/// Specify the root notion page/database ID, can be find in the
36-
/// page/database link. If it's not set, will read from env var
37-
/// NOTION_ROOT_ID.
38-
#[arg(long)]
39-
id: Option<String>,
36+
/// A LINK or ID of a notion page/database. Everything in it will be
37+
/// downloaded, in recursive way. Read from env var NOTION_ROOT_PAGE if
38+
/// not set.
39+
page: Option<String>,
4040
},
4141
}
4242

4343
const NOTION_TOKEN: &str = "NOTION_TOKEN";
44-
const NOTION_ROOT_ID: &str = "NOTION_ROOT_ID";
44+
const NOTION_ROOT_PAGE: &str = "NOTION_ROOT_PAGE";
4545

4646
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
4747

@@ -59,19 +59,43 @@ async fn main() -> Result<()> {
5959
impl Cli {
6060
async fn run(&self, db: &mut SqliteConnection) -> Result<()> {
6161
match &self.command {
62-
Commands::Sync { id } => {
63-
let page_id = match id {
62+
Commands::Sync { page } => {
63+
let page = match page {
6464
Some(id) => id.to_owned(),
6565
None => {
66-
let Ok(id) = env::var(NOTION_ROOT_ID) else {
66+
let Ok(id) = env::var(NOTION_ROOT_PAGE) else {
6767
return Err(
68-
format!("Neither --id nor env {NOTION_ROOT_ID} is set.").into()
68+
format!("Neither --id nor env {NOTION_ROOT_PAGE} is set.").into()
6969
);
7070
};
7171
id
7272
}
7373
};
7474

75+
let page_id = if page.starts_with("https://") {
76+
match page.parse::<Uri>() {
77+
Ok(uri) => {
78+
let p = uri.path();
79+
let p = path::Path::new(p).file_name();
80+
if let Some(last) = p.and_then(|x| x.to_str()) {
81+
if let Some((_, id)) = last.rsplit_once("-") {
82+
id.to_owned()
83+
} else {
84+
last.to_owned()
85+
}
86+
} else {
87+
return Err(format!(
88+
"Can't extract ID from NOTION_ROOT_PAGE, which value is {page}"
89+
)
90+
.into());
91+
}
92+
}
93+
Err(_) => page,
94+
}
95+
} else {
96+
page
97+
};
98+
7599
run_sync(&self.get_token()?, &page_id, db).await;
76100
}
77101
};

0 commit comments

Comments
 (0)