-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbearcli.py
More file actions
580 lines (445 loc) · 16.1 KB
/
bearcli.py
File metadata and controls
580 lines (445 loc) · 16.1 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
#!/usr/bin/env python3
import argparse
import json
import os
import requests
from bs4 import BeautifulSoup
# Config paths
CONFIG_PATH_HOME = os.path.expanduser("~/.bearblog")
SESSION_PATH = os.path.expanduser("~/.bearblog_session")
def load_config():
# 1. Config next to script
script_dir = os.path.dirname(os.path.abspath(__file__))
local_config = os.path.join(script_dir, ".bearblog")
# 2. Config in home directory
if os.path.exists(local_config):
config_path = local_config
elif os.path.exists(CONFIG_PATH_HOME):
config_path = CONFIG_PATH_HOME
else:
raise RuntimeError("Missing .bearblog config file in script directory or home directory")
email = None
password = None
blog_name = None
with open(config_path, "r") as f:
for line in f:
if line.startswith("EMAIL="):
email = line.split("=", 1)[1].strip()
if line.startswith("PASSWORD="):
password = line.split("=", 1)[1].strip()
if line.startswith("BLOG_NAME="):
blog_name = line.split("=", 1)[1].strip()
if not email or not password or not blog_name:
raise RuntimeError("Config missing EMAIL, PASSWORD, or BLOG_NAME")
blog_url = f"https://bearblog.dev/{blog_name}"
return email, password, blog_url
def extract_csrf(html):
soup = BeautifulSoup(html, "html.parser")
token = soup.find("input", {"name": "csrf_token"})
return token["value"] if token else None
def get_session():
session = requests.Session()
email, password, blog_url = load_config()
# Load existing cookie
if os.path.exists(SESSION_PATH):
with open(SESSION_PATH, "r") as f:
cookie = f.read().strip()
session.cookies.set("session", cookie, domain="bearblog.dev")
session.headers.update({
"User-Agent": (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/122.0.0.0 Safari/537.36"
),
"Accept": (
"text/html,application/xhtml+xml,application/xml;"
"q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,"
"application/signed-exchange;v=b3;q=0.7"
),
"Accept-Language": "en-US,en;q=0.9",
"Cache-Control": "max-age=0",
"Sec-Ch-Ua": '"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"',
"Sec-Ch-Ua-Mobile": "?0",
"Sec-Ch-Ua-Platform": '"Windows"',
"Sec-Fetch-Dest": "document",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-Site": "none",
"Sec-Fetch-User": "?1",
"Upgrade-Insecure-Requests": "1",
})
# Test if session is already valid
r = session.get(f"{blog_url}/dashboard/posts")
if "Log in" not in r.text and "Sign In" not in r.text:
if "challenge-error-text" in r.text:
print(f"Exception: Blocked by CloudFlare\nResponse: {r.text[:100]}\n\n")
raise Exception("Blocked by CloudFlare")
return session
# Correct login URL
login_url = "https://bearblog.dev/accounts/login/"
# Load login page
login_page = session.get(login_url)
soup = BeautifulSoup(login_page.text, "html.parser")
# Extract Django CSRF token
csrf_input = soup.find("input", {"name": "csrfmiddlewaretoken"})
if not csrf_input:
raise RuntimeError("Could not find CSRF token on login page")
csrf = csrf_input["value"]
# Correct payload based on your form
payload = {
"login": email, # correct field name
"password": password, # correct field name
"remember": "on", # optional
"csrfmiddlewaretoken": csrf # correct CSRF field
}
headers = {
"Referer": login_url # Django requires this
}
# Submit login
r = session.post(login_url, data=payload, headers=headers)
# Correct success check
if "Sign out" not in r.text and "Log out" not in r.text:
print(r.text)
raise RuntimeError("Login failed")
# Save cookie
cookie = session.cookies.get("session", domain="bearblog.dev")
if cookie:
with open(SESSION_PATH, "w") as f:
f.write(cookie)
return session
# -----------------------------
# LIST POSTS
# -----------------------------
def cmd_list(args):
session = get_session()
email, password, blog_url = load_config()
r = session.get(f"{blog_url}/dashboard/posts")
soup = BeautifulSoup(r.text, "html.parser")
posts = []
for li in soup.select("ul.post-list li"):
a = li.find("a")
if not a:
continue
href = a.get("href", "").strip()
title = a.text.strip()
# Extract post ID from URL
parts = href.strip("/").split("/")
post_id = parts[-1]
posts.append({
"id": post_id,
"title": title,
"href": href
})
print(json.dumps(posts, indent=2))
# -----------------------------
# NEW POST
# -----------------------------
def cmd_new(args):
session = get_session()
email, password, blog_url = load_config()
# Read file content safely
try:
with open(args.file, "r", encoding="utf-8") as f:
content = f.read()
except UnicodeDecodeError:
with open(args.file, "r", encoding="utf-8-sig") as f:
content = f.read()
# --- Load the "new post" page to get CSRF ---
new_post_url = f"{blog_url}/dashboard/posts/new/"
r = session.get(new_post_url)
soup = BeautifulSoup(r.text, "html.parser")
csrf_input = soup.find("input", {"name": "csrfmiddlewaretoken"})
if not csrf_input:
print(r.text)
raise RuntimeError("Could not find CSRF token on new post page")
csrf = csrf_input["value"]
# --- Build correct payload ---
payload = {
"csrfmiddlewaretoken": csrf,
"publish": "true", # auto-publish
"header_content": f"title: {args.title}",
"body_content": content
}
# --- Submit the form (POST /new/) ---
r = session.post(new_post_url, data=payload, allow_redirects=False)
# --- Extract post ID from redirect ---
if "Location" not in r.headers:
print(r.text)
raise RuntimeError("Post creation failed (no redirect)")
redirect_url = r.headers["Location"].rstrip("/") + "/"
post_id = redirect_url.split("/")[-2]
# --- Output JSON ---
print(json.dumps({
"status": "created",
"id": post_id,
"published": True
}, indent=2))
# -----------------------------
# UPDATE POST
# -----------------------------
def cmd_update(args):
session = get_session()
email, password, blog_url = load_config()
# Read new body content
with open(args.file, "r", encoding="utf-8") as f:
body_content = f.read().lstrip("\ufeff")
edit_url = f"{blog_url}/dashboard/posts/{args.id}/"
# Load edit page
r = session.get(edit_url)
soup = BeautifulSoup(r.text, "html.parser")
# Extract CSRF
csrf_input = soup.find("input", {"name": "csrfmiddlewaretoken"})
csrf = csrf_input["value"]
# Extract header_content EXACTLY like browser innerText
header_div = soup.find("div", {"id": "header_content"})
header_content = extract_header_content(header_div)
# Build payload
payload = {
"csrfmiddlewaretoken": csrf,
"header_content": header_content,
"body_content": body_content,
}
headers = {"Referer": edit_url}
# POST update
r = session.post(edit_url, data=payload, headers=headers)
if r.status_code != 200:
print(r.text)
raise RuntimeError(f"Update failed (status {r.status_code})")
print(json.dumps({"status": "updated", "id": args.id}, indent=2))
# -----------------------------
# DELETE POST
# -----------------------------
def cmd_delete(args):
session = get_session()
email, password, blog_url = load_config()
post_id = args.id
delete_url = f"{blog_url}/dashboard/posts/{post_id}/delete/"
# CSRF token comes from the cookie, not the page
if "csrftoken" not in session.cookies:
raise RuntimeError("No CSRF token in session cookies")
csrf = session.cookies["csrftoken"]
payload = {
"csrfmiddlewaretoken": csrf
}
# Must include Referer header or Django will reject it
headers = {
"Referer": f"{blog_url}/dashboard/posts/{post_id}/"
}
r = session.post(delete_url, data=payload, headers=headers, allow_redirects=False)
if r.status_code != 302:
print(r.text)
raise RuntimeError(f"Delete failed (status {r.status_code})")
print(json.dumps({
"status": "deleted",
"id": post_id
}, indent=2))
# -----------------------------
# PUBLISH POST
# -----------------------------
def cmd_publish(args):
session = get_session()
email, password, blog_url = load_config()
post_id = args.id
edit_url = f"{blog_url}/dashboard/posts/{post_id}/"
# CSRF from cookie
if "csrftoken" not in session.cookies:
raise RuntimeError("No CSRF token in session cookies")
csrf = session.cookies["csrftoken"]
payload = {
"csrfmiddlewaretoken": csrf,
"published": "true"
}
headers = {
"Referer": edit_url
}
r = session.post(edit_url, data=payload, headers=headers)
if r.status_code != 200:
print(r.text)
raise RuntimeError(f"Publish failed (status {r.status_code})")
print(json.dumps({
"status": "published",
"id": post_id
}, indent=2))
# def extract_clean_header(div):
# # Convert <br> to newlines
# for br in div.find_all("br"):
# br.replace_with("\n")
# # Convert <b>title:</b> to "title:"
# for b in div.find_all("b"):
# b.replace_with(b.get_text())
# # Get raw text
# text = div.get_text("\n")
# # Normalize whitespace
# lines = [line.strip() for line in text.split("\n") if line.strip()]
# # Rejoin into clean header block
# return "\n".join(lines)
def normalize_header_block(raw):
lines = []
key = None
for line in raw.splitlines():
stripped = line.strip()
if not stripped:
continue
# If line ends with ":" it's a key
if stripped.endswith(":"):
key = stripped[:-1] # remove colon
continue
# Otherwise it's a value
if key:
lines.append(f"{key}: {stripped}")
key = None
return "\n".join(lines)
def extract_header_content(header_div):
# Clone the div so we can modify it safely
div = header_div
# Replace <br> with newline
for br in div.find_all("br"):
br.replace_with("\n")
# Replace <b>...</b> with just the text inside
for b in div.find_all("b"):
b.replace_with(b.get_text())
# Replace <span>...</span> with its text
for span in div.find_all("span"):
span.replace_with(span.get_text())
# Now get the text exactly like innerText
text = div.get_text()
# Normalize whitespace like the browser does
lines = [line.strip() for line in text.split("\n") if line.strip()]
return "\n".join(lines)
# # -----------------------------
# # UNPUBLISH POST
# # -----------------------------
# def cmd_unpublish(args):
# session = get_session()
# email, password, blog_url = load_config()
# post_id = args.id
# edit_url = f"{blog_url}/dashboard/posts/{post_id}/"
# # 1) Load edit page
# r = session.get(edit_url)
# soup = BeautifulSoup(r.text, "html.parser")
# csrf_input = soup.find("input", {"name": "csrfmiddlewaretoken"})
# if not csrf_input:
# print(r.text)
# raise RuntimeError("Could not find CSRF token on edit page")
# csrf = csrf_input["value"]
# # Extract header_content from the hidden input
# header_input = soup.find("input", {"name": "header_content"})
# if not header_input:
# print(r.text)
# raise RuntimeError("Could not find hidden header_content input")
# header_content = header_input.get("value", "").strip()
# # Extract header_content from the editable DIV
# header_div = soup.find("div", {"id": "header_content"})
# if not header_div:
# print(r.text)
# raise RuntimeError("Could not find header_content DIV on edit page")
# raw_header = header_div.get_text("\n")
# header_content = normalize_header_block(raw_header)
# # Extract body_content
# body_textarea = soup.find("textarea", {"name": "body_content"})
# if not body_textarea:
# print(r.text)
# raise RuntimeError("Could not find body_content on edit page")
# body_content = body_textarea.text.lstrip("\ufeff")
# # 2) Build payload (publish = false)
# payload = {
# "csrfmiddlewaretoken": csrf,
# "publish": "false",
# "header_content": header_content,
# "body_content": body_content,
# }
# print(payload)
# headers = {
# "Referer": edit_url
# }
# # 3) POST back to edit URL
# r = session.post(edit_url, data=payload, headers=headers)
# if r.status_code != 200:
# print(r.text)
# raise RuntimeError(f"Unpublish failed (status {r.status_code})")
# print(json.dumps({
# "status": "unpublished",
# "id": post_id
# }, indent=2))
def cmd_load(args):
session = get_session()
email, password, blog_url = load_config()
edit_url = f"{blog_url}/dashboard/posts/{args.id}/"
r = session.get(edit_url)
soup = BeautifulSoup(r.text, "html.parser")
# Extract header
header_div = soup.find("div", {"id": "header_content"})
if not header_div:
raise RuntimeError("Could not find header_content DIV")
# Convert <br> to newlines
for br in header_div.find_all("br"):
br.replace_with("\n")
# Extract text
header_text = header_div.get_text("\n").strip()
# Extract body
body_textarea = soup.find("textarea", {"name": "body_content"})
if not body_textarea:
raise RuntimeError("Could not find body_content textarea")
body_text = body_textarea.text.lstrip("\ufeff")
print("\n=== HEADER ===\n")
print(header_text)
print("\n=== BODY ===\n")
print(body_text)
# -----------------------------
# MAIN
# -----------------------------
def main():
parser = argparse.ArgumentParser(
description=(
"BearBlog CLI (Free Plan)\n\n"
"Examples:\n"
" bearcli list\n"
" bearcli new \"My Post Title\" --file post.md\n"
" bearcli load abc123xyz\n"
" bearcli delete abc123xyz\n"
),
formatter_class=argparse.RawTextHelpFormatter
)
sub = parser.add_subparsers(dest="command", required=True)
# list
p_list = sub.add_parser(
"list",
help="List all posts in your Bear Blog dashboard."
)
p_list.set_defaults(func=cmd_list)
# new
p_new = sub.add_parser(
"new",
help="Create a new post from a markdown file."
)
p_new.add_argument("title", help="Title of the new post.")
p_new.add_argument("--file", required=True, help="Path to the markdown file.")
p_new.set_defaults(func=cmd_new)
# load
p_load = sub.add_parser(
"load",
help="Load a post by ID and print its header + body."
)
p_load.add_argument("id", help="Post ID to load.")
p_load.set_defaults(func=cmd_load)
# delete
p_delete = sub.add_parser(
"delete",
help="Delete a post by ID."
)
p_delete.add_argument("id", help="Post ID to delete.")
p_delete.set_defaults(func=cmd_delete)
# Disabled commands (kept for future use)
p_update = sub.add_parser("update", help="Update a post by ID.")
p_update.add_argument("id")
p_update.add_argument("--file", required=True)
p_update.set_defaults(func=cmd_update)
p_publish = sub.add_parser("publish", help="Publish a post by ID.")
p_publish.add_argument("id")
p_publish.set_defaults(func=cmd_publish)
# p_unpublish = sub.add_parser("unpublish", help="Unpublish a post (disabled).")
# p_unpublish.add_argument("id")
# p_unpublish.set_defaults(func=cmd_unpublish)
args = parser.parse_args()
args.func(args)
if __name__ == "__main__":
main()