-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotionWebsitePuppeteer.cs
233 lines (191 loc) · 7.54 KB
/
NotionWebsitePuppeteer.cs
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
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;
namespace NotionBackupTool;
internal class NotionWebsitePuppeteer
{
private readonly string _seleniumHost;
private IWebDriver _driver;
private readonly string _username;
private readonly string _password;
private readonly bool _debugMode;
public string MailHost { get; set; }
public string MailUser { get; set; }
public string MailPassword { get; set; }
public NotionWebsitePuppeteer(string seleniumHost, string notionUsername, string notionPassword, bool debugMode = false)
{
_seleniumHost = seleniumHost;
_username = notionUsername;
_password = notionPassword;
_debugMode = debugMode;
}
private void Login()
{
void TryLoginWithLink()
{
Console.WriteLine("Trying login-code login...");
// Query mail for login code
MailGrabber mg = new MailGrabber(MailHost, MailUser, MailPassword);
string loginUrl = "";
bool foundUrl = false;
while (!foundUrl)
{
Thread.Sleep(2000);
Console.WriteLine("Waiting for login code email...");
List<Tuple<string, string>> loginUrls = mg.FindUrl("[email protected]",
"https://www\\.notion\\.so/loginwithemail.*?(?=\")", String.Empty);
if (loginUrls.Count == 0) continue;
loginUrl = loginUrls.First().Item1;
foundUrl = true;
mg.Purge("[email protected]","login code");
}
Console.WriteLine("Logging in using URL in 30sec...");
Thread.Sleep(30000);
string cleanedUrl = loginUrl.Replace("https://","https").Replace("&", "&").Replace(":", "%3A").Replace("https","https://");
_driver.Navigate().GoToUrl(cleanedUrl);
Thread.Sleep(5000);
}
bool FirstLoginStep()
{
bool needsLoginCode = false;
Console.WriteLine("\tNavigating to login page...");
// Navigate to Notes GitHub auth
_driver.Navigate().GoToUrl($"https://notion.so/login");
Console.WriteLine("\tSleep 2.5sec");
Thread.Sleep(2500);
// Login to GitHub
Console.WriteLine("\tEntering credentials...");
IWebElement loginInput = _driver.FindElement(By.Id("notion-email-input-2"));
loginInput.SendKeys(_username);
IWebElement nextBtn = _driver.FindElement(By.XPath("//form//div[contains(text(), 'Continue')]"));
nextBtn.Click();
Console.WriteLine("\tSleep 10sec");
Thread.Sleep(10000);
// Check if we need to use a login code
try
{
IWebElement loginCodeBtn =
_driver.FindElement(By.XPath("//form//label[contains(text(), 'Verification code')]"));
if (loginCodeBtn != null)
{
needsLoginCode = true;
}
}
catch
{
Console.WriteLine("Not in explicit login code mode");
}
return needsLoginCode;
}
// Instantiate a ChromeDriver
var options = new ChromeOptions();
options.AddArgument("--headless=new");
if (_debugMode)
{
_driver = new ChromeDriver();
}
else
{
_driver = new RemoteWebDriver(new Uri(_seleniumHost), options);
}
WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(120));
bool needsLoginCode = FirstLoginStep();
try
{
IWebElement loginError =
_driver.FindElement(By.XPath("//div[contains(text(), 'You must login with an email login code')]"));
if (loginError != null)
{
// Need to reload site and restart to enter login code mode
Console.WriteLine("Need to restart login.");
Thread.Sleep(3000);
needsLoginCode = FirstLoginStep();
}
}
catch
{
Console.WriteLine("No login code error. continue.");
}
if (needsLoginCode)
{
TryLoginWithLink();
}
else
{
Console.WriteLine("Trying password login...");
try
{
IWebElement passInput = _driver.FindElement(By.Id("notion-password-input-1"));
passInput.SendKeys(_password);
Console.WriteLine("\tSending login...");
IWebElement loginBtn = _driver.FindElement(By.XPath("//form//div[contains(text(), 'Continue with password')]"));
loginBtn.Click();
}
catch
{
Console.WriteLine("bad login");
TryLoginWithLink();
}
}
}
public void TriggerExport(List<string> workspaceSlug)
{
Console.WriteLine("Login...");
Login();
foreach (string ws in workspaceSlug)
{
try
{
Thread.Sleep(10000);
Console.WriteLine($"Navigate to workspace {ws}...");
_driver.Navigate().GoToUrl($"https://notion.so/{ws}");
Thread.Sleep(5000);
Console.WriteLine("Navigating to Export button...");
IWebElement nextBtn = _driver.FindElement(By.XPath("//nav//div[contains(text(), 'Settings')]"));
nextBtn.Click();
Thread.Sleep(3000);
_driver.FindElement(By.XPath("//div[@class='notion-space-settings']//div[text() = 'Settings']")).Click();
Thread.Sleep(3000);
_driver.FindElement(By.XPath("//div[@class='notion-space-settings']//div[text() = 'Export all workspace content']")).Click();
Thread.Sleep(3000);
Console.WriteLine("Trigger Export...");
_driver.FindElement(By.XPath("//div[@class='notion-dialog']//div[text() = 'Export']")).Click();
Console.WriteLine($"Export running for {ws}");
}
catch (Exception ex)
{
Console.WriteLine($"Error triggering export for workspace {ws}: {ex.Message}");
}
}
// sleeping to let export init
Console.WriteLine("Waiting for export to settle...");
Thread.Sleep(15000);
_driver.Close();
}
public Cookie GrabSessionCookies()
{
Login();
//wait.Until(wd => wd.FindElement(By.Id("notion-app")));
Console.WriteLine("\tSleep 15sec");
Thread.Sleep(15000);
Console.WriteLine("Navigate to file URL...");
_driver.Navigate().GoToUrl($"https://file.notion.so/f/");
Console.WriteLine("\tSleep 5sec");
Thread.Sleep(5000);
// Wait for return to HackMD
Console.WriteLine("\tGrabbing delicious Cookies...");
// grab cookies
var cookie = _driver.Manage().Cookies.GetCookieNamed("file_token");
if (cookie == null)
{
Console.WriteLine("Unable to grab cookie. Trying again...");
Console.WriteLine("\tSleep 5sec");
Thread.Sleep(5000);
cookie = _driver.Manage().Cookies.GetCookieNamed("file_token");
}
// Close the driver
_driver.Quit();
return cookie;
}
}