1- use std:: { collections:: HashMap , env} ;
1+ use std:: { collections:: HashMap , env, path } ;
22
33use clap:: { Parser , Subcommand } ;
44use futures:: StreamExt ;
5+ use http:: Uri ;
56use 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" ) ]
1617struct 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 {
3233enum 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
4343const NOTION_TOKEN : & str = "NOTION_TOKEN" ;
44- const NOTION_ROOT_ID : & str = "NOTION_ROOT_ID " ;
44+ const NOTION_ROOT_PAGE : & str = "NOTION_ROOT_PAGE " ;
4545
4646type Result < T > = std:: result:: Result < T , Box < dyn std:: error:: Error > > ;
4747
@@ -59,19 +59,43 @@ async fn main() -> Result<()> {
5959impl 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