-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdraft_publish.py
More file actions
305 lines (246 loc) · 10.2 KB
/
draft_publish.py
File metadata and controls
305 lines (246 loc) · 10.2 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
# draft_publish.py - Publish Substack drafts
import os
import json
import requests
from dotenv import load_dotenv
load_dotenv()
# Setup session
session = requests.Session()
session.headers.update({
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
"Referer": os.getenv("PUBLICATION_URL"),
"Content-Type": "application/json"
})
# Set cookies
cookie_map = {
"sid": os.getenv("SID"),
"substack.lli": os.getenv("SUBSTACK_LLI"),
"substack.sid": os.getenv("SUBSTACK_SID")
}
for k, v in cookie_map.items():
if v:
session.cookies.set(k, v, domain=".substack.com")
pub_url = os.getenv("PUBLICATION_URL")
def get_drafts():
"""Get all drafts"""
response = session.get(f"{pub_url}/api/v1/drafts")
if response.status_code == 200:
drafts = response.json()
print(f"Found {len(drafts)} drafts")
return drafts
else:
print(f"Error getting drafts: {response.text}")
return []
def get_unpublished_drafts():
"""Get only unpublished drafts for API"""
response = session.get(f"{pub_url}/api/v1/drafts")
if response.status_code == 200:
drafts = response.json()
# Filter only unpublished drafts
unpublished = [draft for draft in drafts if not draft.get('is_published', False)]
return unpublished
else:
return None
def publish_draft(draft_id, send_email=True, audience="everyone"):
"""Publish a draft immediately"""
print(f"Publishing draft {draft_id}...")
print(f"Send email: {send_email}")
print(f"Audience: {audience}")
# Publish the draft
publish_data = {
"should_send_email": send_email,
"audience": audience # "everyone" or "paid"
}
response = session.post(f"{pub_url}/api/v1/drafts/{draft_id}/publish", json=publish_data)
if response.status_code == 200:
result = response.json()
print(f"SUCCESS! Draft {draft_id} published!")
# Get the published post URL if available
post_url = None
if 'slug' in result:
post_url = f"{pub_url}/p/{result['slug']}"
print(f"Post URL: {post_url}")
# Return API-compatible format
return {
"success": True,
"post_id": result.get('id'),
"post_url": post_url,
"message": f"Draft {draft_id} published successfully",
"raw_result": result
}
else:
print(f"FAILED to publish: {response.text}")
return {
"success": False,
"message": f"Failed to publish draft {draft_id}: {response.text}",
"error_code": response.status_code
}
def publish_draft_paid_only(draft_id, send_email=True):
"""Publish a draft for paid subscribers only"""
return publish_draft(draft_id, send_email=send_email, audience="paid")
def get_published_posts():
"""Get published posts"""
response = session.get(f"{pub_url}/api/v1/posts")
if response.status_code == 200:
posts = response.json()
print(f"Found {len(posts)} published posts")
return posts
else:
print(f"Error getting posts: {response.text}")
return []
def list_drafts():
"""List all drafts with details"""
drafts = get_drafts()
print("\n=== AVAILABLE DRAFTS ===")
if not drafts:
print("No drafts found")
return []
for draft in drafts:
print(f"Draft ID: {draft['id']}")
print(f"Title: {draft.get('draft_title', 'Untitled')}")
print(f"Subtitle: {draft.get('draft_subtitle', 'No subtitle')}")
print(f"Created: {draft.get('draft_created_at', 'Unknown')}")
print(f"Updated: {draft.get('draft_updated_at', 'Unknown')}")
# Show if scheduled
if draft.get('post_date'):
print(f"Scheduled for: {draft['post_date']}")
# Show content preview
draft_body = draft.get('draft_body', '{}')
try:
content = json.loads(draft_body)
if content.get('content') and len(content['content']) > 0:
first_paragraph = content['content'][0]
if first_paragraph.get('content') and len(first_paragraph['content']) > 0:
first_text = first_paragraph['content'][0]
if first_text.get('text'):
preview = first_text['text'][:100]
print(f"Preview: {preview}...")
except:
print("Preview: [Content not readable]")
print("-" * 50)
return drafts
def unpublish_post(post_id):
"""Unpublish a post (make it a draft again)"""
print(f"Unpublishing post {post_id}...")
response = session.post(f"{pub_url}/api/v1/posts/{post_id}/unpublish", json={})
if response.status_code == 200:
result = response.json()
print(f"SUCCESS! Post {post_id} unpublished (now a draft)")
return result
else:
print(f"FAILED to unpublish: {response.text}")
return None
def list_published_posts():
"""List published posts"""
posts = get_published_posts()
print("\n=== PUBLISHED POSTS ===")
if not posts:
print("No published posts found")
return []
for post in posts[:10]: # Show first 10
print(f"Post ID: {post['id']}")
print(f"Title: {post.get('title', 'Untitled')}")
print(f"Slug: {post.get('slug', 'no-slug')}")
print(f"Published: {post.get('post_date', 'Unknown')}")
if post.get('slug'):
post_url = f"{pub_url}/p/{post['slug']}"
print(f"URL: {post_url}")
print("-" * 40)
return posts
if __name__ == "__main__":
print("=== SUBSTACK DRAFT PUBLISHING ===")
# Get all drafts
drafts = get_drafts()
if not drafts:
print("\nNo drafts found!")
create_new = input("Do you want to create a new draft? (y/n): ").lower().strip()
if create_new == 'y':
print("\nCalling draft_create.py...")
import subprocess
import sys
# Call draft_create.py
result = subprocess.run([sys.executable, 'draft_create.py'],
cwd=os.path.dirname(os.path.abspath(__file__)))
if result.returncode == 0:
print("\nDraft created! Please run this script again to publish it.")
else:
print("\nFailed to create draft.")
exit()
# Show available drafts
print(f"\n=== AVAILABLE DRAFTS ({len(drafts)}) ===")
unpublished_drafts = []
for i, draft in enumerate(drafts):
if not draft.get('is_published', False):
unpublished_drafts.append(draft)
print(f"{len(unpublished_drafts)}. ID: {draft['id']}")
print(f" Title: {draft.get('draft_title', 'Untitled')}")
# Show content preview
draft_body = draft.get('draft_body', '{}')
try:
content = json.loads(draft_body)
if content.get('content') and len(content['content']) > 0:
first_paragraph = content['content'][0]
if first_paragraph.get('content') and len(first_paragraph['content']) > 0:
first_text = first_paragraph['content'][0]
if first_text.get('text'):
preview = first_text['text'][:80]
print(f" Preview: {preview}...")
except:
print(" Preview: [Content not readable]")
print()
if not unpublished_drafts:
print("No unpublished drafts found!")
create_new = input("Do you want to create a new draft? (y/n): ").lower().strip()
if create_new == 'y':
print("\nCalling draft_create.py...")
import subprocess
import sys
result = subprocess.run([sys.executable, 'draft_create.py'],
cwd=os.path.dirname(os.path.abspath(__file__)))
if result.returncode == 0:
print("\nDraft created! Please run this script again to publish it.")
exit()
# Ask user to select a draft
while True:
try:
choice = input(f"Select draft to publish (1-{len(unpublished_drafts)}) or 'q' to quit: ").strip()
if choice.lower() == 'q':
print("Goodbye!")
exit()
choice_num = int(choice) - 1
if 0 <= choice_num < len(unpublished_drafts):
selected_draft = unpublished_drafts[choice_num]
break
else:
print(f"Invalid choice. Please enter 1-{len(unpublished_drafts)} or 'q'")
except ValueError:
print("Invalid input. Please enter a number or 'q'")
# Show selected draft details
print(f"\n=== SELECTED DRAFT ===")
print(f"ID: {selected_draft['id']}")
print(f"Title: {selected_draft.get('draft_title', 'Untitled')}")
# Ask for publishing options
print(f"\n=== PUBLISHING OPTIONS ===")
# Email option
send_email = input("Send email to subscribers? (y/n, default: y): ").lower().strip()
send_email = send_email != 'n'
# Audience option
audience_choice = input("Audience (1=everyone, 2=paid only, default: 1): ").strip()
audience = "paid" if audience_choice == "2" else "everyone"
# Confirm
print(f"\n=== CONFIRM PUBLISHING ===")
print(f"Draft: {selected_draft.get('draft_title', 'Untitled')}")
print(f"Send email: {send_email}")
print(f"Audience: {audience}")
confirm = input("\nPublish now? (y/n): ").lower().strip()
if confirm == 'y':
result = publish_draft(selected_draft['id'], send_email=send_email, audience=audience)
if result:
print(f"\nSUCCESS! Draft published!")
if 'slug' in result:
print(f"Post URL: {pub_url}/p/{result['slug']}")
else:
print(f"\nFAILED to publish draft.")
else:
print("Publishing cancelled.")
print("\nPublishing complete!")