Skip to content

Commit f11b2b6

Browse files
fix login flow and export trigger. bump to .net 9
1 parent b7944bd commit f11b2b6

File tree

5 files changed

+66
-39
lines changed

5 files changed

+66
-39
lines changed

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
FROM mcr.microsoft.com/dotnet/runtime:8.0 AS base
1+
FROM mcr.microsoft.com/dotnet/runtime:9.0 AS base
22
USER $APP_UID
33
WORKDIR /app
44

5-
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
5+
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
66
ARG BUILD_CONFIGURATION=Release
77
WORKDIR /src
88
COPY ["NotionBackupTool.csproj", "./"]

NotionBackupTool.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net8.0</TargetFramework>
5+
<TargetFramework>net9.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>

NotionWebsitePuppeteer.cs

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@ internal class NotionWebsitePuppeteer
1111
private IWebDriver _driver;
1212
private readonly string _username;
1313
private readonly string _password;
14+
private readonly bool _debugMode;
1415

1516
public string MailHost { get; set; }
1617
public string MailUser { get; set; }
1718
public string MailPassword { get; set; }
18-
public NotionWebsitePuppeteer(string seleniumHost, string notionUsername, string notionPassword)
19+
public NotionWebsitePuppeteer(string seleniumHost, string notionUsername, string notionPassword, bool debugMode = false)
1920
{
2021
_seleniumHost = seleniumHost;
2122
_username = notionUsername;
2223
_password = notionPassword;
23-
24+
_debugMode = debugMode;
2425
}
2526

2627
private void Login()
@@ -78,7 +79,7 @@ bool FirstLoginStep()
7879
try
7980
{
8081
IWebElement loginCodeBtn =
81-
_driver.FindElement(By.XPath("//form//div[contains(text(), 'Continue with login code')]"));
82+
_driver.FindElement(By.XPath("//form//label[contains(text(), 'Verification code')]"));
8283
if (loginCodeBtn != null)
8384
{
8485
needsLoginCode = true;
@@ -97,8 +98,14 @@ bool FirstLoginStep()
9798
var options = new ChromeOptions();
9899
options.AddArgument("--headless=new");
99100

100-
_driver = new RemoteWebDriver(new Uri(_seleniumHost), options);
101-
//_driver = new ChromeDriver();
101+
if (_debugMode)
102+
{
103+
_driver = new ChromeDriver();
104+
}
105+
else
106+
{
107+
_driver = new RemoteWebDriver(new Uri(_seleniumHost), options);
108+
}
102109
WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(120));
103110

104111
bool needsLoginCode = FirstLoginStep();
@@ -153,26 +160,33 @@ public void TriggerExport(List<string> workspaceSlug)
153160

154161
foreach (string ws in workspaceSlug)
155162
{
156-
Thread.Sleep(10000);
157-
Console.WriteLine($"Navigate to workspace {ws}...");
158-
_driver.Navigate().GoToUrl($"https://notion.so/{ws}");
159-
160-
Thread.Sleep(5000);
161-
Console.WriteLine("Navigating to Export button...");
162-
IWebElement nextBtn = _driver.FindElement(By.XPath("//nav//div[contains(text(), 'Settings')]"));
163-
nextBtn.Click();
164-
165-
Thread.Sleep(3000);
166-
_driver.FindElement(By.XPath("//div[text() = 'Settings']")).Click();
167-
168-
Thread.Sleep(3000);
169-
_driver.FindElement(By.XPath("//div[text() = 'Export all workspace content']")).Click();
170-
171-
Thread.Sleep(3000);
172-
Console.WriteLine("Trigger Export...");
173-
_driver.FindElement(By.XPath("//div[text() = 'Export']")).Click();
163+
try
164+
{
165+
Thread.Sleep(10000);
166+
Console.WriteLine($"Navigate to workspace {ws}...");
167+
_driver.Navigate().GoToUrl($"https://notion.so/{ws}");
168+
169+
Thread.Sleep(5000);
170+
Console.WriteLine("Navigating to Export button...");
171+
IWebElement nextBtn = _driver.FindElement(By.XPath("//nav//div[contains(text(), 'Settings')]"));
172+
nextBtn.Click();
173+
174+
Thread.Sleep(3000);
175+
_driver.FindElement(By.XPath("//div[@class='notion-space-settings']//div[text() = 'Settings']")).Click();
176+
177+
Thread.Sleep(3000);
178+
_driver.FindElement(By.XPath("//div[@class='notion-space-settings']//div[text() = 'Export all workspace content']")).Click();
179+
180+
Thread.Sleep(3000);
181+
Console.WriteLine("Trigger Export...");
182+
_driver.FindElement(By.XPath("//div[@class='notion-dialog']//div[text() = 'Export']")).Click();
174183

175-
Console.WriteLine($"Export running for {ws}");
184+
Console.WriteLine($"Export running for {ws}");
185+
}
186+
catch (Exception ex)
187+
{
188+
Console.WriteLine($"Error triggering export for workspace {ws}: {ex.Message}");
189+
}
176190
}
177191

178192
// sleeping to let export init

Program.cs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,24 @@ static string GetConfig(string envName, string defaultValue = "")
2020
static async Task Main()
2121
{
2222
// Wait for debugger
23-
/*while (!Debugger.IsAttached) // wait here until debugger is attached
23+
bool debugMode = Environment.GetEnvironmentVariable("DEBUG") == "true";
24+
if (debugMode)
2425
{
25-
Console.WriteLine("Waiting for debugger..");
26-
System.Threading.Thread.Sleep(1000); // prevent tight loop
27-
}*/
26+
Console.WriteLine("DEBUG ON");
27+
while (!Debugger.IsAttached) // wait here until debugger is attached
28+
{
29+
Console.WriteLine("Waiting for debugger..");
30+
System.Threading.Thread.Sleep(1000); // prevent tight loop
31+
}
2832

33+
}
34+
2935

3036

3137
string mode = GetConfig("MODE");
3238

3339
string webDriverEndpoint = GetConfig("WEBDRIVER_URL", "http://localhost:4444");
3440

35-
string s3Host = GetConfig("S3_HOST");
36-
string s3AccessKey = GetConfig("S3_ACCESS_KEY");
37-
string s3SecretKey = GetConfig("S3_SECRET_KEY");
38-
string s3Bucket = GetConfig("S3_BUCKET");
3941

4042
string notionUser = GetConfig("NOTION_USERNAME");
4143
string notionPassword = GetConfig("NOTION_PASSWORD");
@@ -45,9 +47,6 @@ static async Task Main()
4547
string mailPassword = GetConfig("IMAP_PASSWORD");
4648
string cachePath = GetConfig("CACHE_PATH");
4749

48-
string temporaryDir = GetConfig("TMP_DIR");
49-
string gpgPublicKey = GetConfig("GPG_PUBKEY_FILE");
50-
5150
List<Workspace> workspaces = GetConfig("WORKSPACES").Split(',').Select(w => w.Split(':')).Select(ws => new Workspace { Name = ws[0], Id = ws[1] }).ToList();
5251

5352

@@ -56,7 +55,7 @@ static async Task Main()
5655
string hcUrl = GetConfig("HEALTHCHECK_URL");
5756
await pingHttp.GetAsync($"{hcUrl}/start");
5857

59-
NotionWebsitePuppeteer grabber = new NotionWebsitePuppeteer(webDriverEndpoint,notionUser, notionPassword)
58+
NotionWebsitePuppeteer grabber = new NotionWebsitePuppeteer(webDriverEndpoint,notionUser, notionPassword, debugMode)
6059
{
6160
MailHost = mailHost,
6261
MailUser = mailUser,
@@ -65,6 +64,13 @@ static async Task Main()
6564

6665
if (mode == "download")
6766
{
67+
string temporaryDir = GetConfig("TMP_DIR");
68+
string gpgPublicKey = GetConfig("GPG_PUBKEY_FILE");
69+
string s3Host = GetConfig("S3_HOST");
70+
string s3AccessKey = GetConfig("S3_ACCESS_KEY");
71+
string s3SecretKey = GetConfig("S3_SECRET_KEY");
72+
string s3Bucket = GetConfig("S3_BUCKET");
73+
6874
string cookieCachePath = Path.Combine(cachePath, "filecookie.txt");
6975
string fileCookieValue;
7076

global.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"sdk": {
3+
"version": "9.0.0",
4+
"rollForward": "latestMajor",
5+
"allowPrerelease": true
6+
}
7+
}

0 commit comments

Comments
 (0)