-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgetposts.py
More file actions
354 lines (297 loc) · 15.7 KB
/
getposts.py
File metadata and controls
354 lines (297 loc) · 15.7 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
# getposts.py - Systematische Suche nach ALLEN Posts/Drafts/Schedules
import os
import requests
from datetime import datetime
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_all_posts():
"""Systematische Suche nach ALLEN Post-Arten mit verschiedenen Endpoints und Parametern"""
all_found_posts = {} # Dict um Duplikate zu vermeiden
print("=== SYSTEMATISCHE POST-SUCHE ===\n")
# Alle möglichen Endpoints mit verschiedenen Parametern
endpoints_to_try = [
# Basic endpoints
"/api/v1/drafts",
"/api/v1/posts",
"/api/v1/archive",
# Special: Individual drafts (to get postSchedules)
"INDIVIDUAL_DRAFTS",
# Mit verschiedenen Parametern
"/api/v1/posts?published=true",
"/api/v1/posts?published=false",
"/api/v1/posts?scheduled=true",
"/api/v1/posts?draft=true",
"/api/v1/posts?status=draft",
"/api/v1/posts?status=published",
"/api/v1/posts?status=scheduled",
"/api/v1/posts?all=true",
"/api/v1/posts?include_drafts=true",
# Andere mögliche Endpoints
"/api/v1/publication/posts",
"/api/v1/publication/drafts",
"/api/v1/publication/archive",
"/api/v1/user/posts",
"/api/v1/user/drafts",
"/api/v1/me/posts",
"/api/v1/dashboard/posts",
# Spezielle Schedule-Endpoints
"/api/v1/scheduled",
"/api/v1/queue",
"/api/v1/publication/scheduled",
]
current_time = datetime.now()
for endpoint in endpoints_to_try:
if endpoint == "INDIVIDUAL_DRAFTS":
print("--- TESTING INDIVIDUAL_DRAFTS ---")
# First get all draft IDs, then fetch each individually to get postSchedules
try:
drafts_response = session.get(f"{pub_url}/api/v1/drafts")
if drafts_response.status_code == 200:
drafts = drafts_response.json()
print(f"Found {len(drafts)} drafts to check individually")
for draft in drafts:
if 'id' in draft:
draft_id = draft['id']
individual_url = f"{pub_url}/api/v1/drafts/{draft_id}"
try:
individual_response = session.get(individual_url)
if individual_response.status_code == 200:
item = individual_response.json()
post_id = item['id']
# Sammle alle relevanten Daten
post_info = {
'id': post_id,
'endpoint': f"INDIVIDUAL_DRAFTS/{draft_id}",
'title': item.get('title') or item.get('draft_title', 'NO TITLE'),
'is_published': item.get('is_published'),
'post_date': item.get('post_date'),
'draft_updated_at': item.get('draft_updated_at'),
'updated_at': item.get('updated_at'),
'postSchedules': item.get('postSchedules', []),
'status': 'UNKNOWN'
}
# Bestimme Status basierend auf Daten
# FIRST: Check for postSchedules (das ist der echte Schedule!)
if post_info['postSchedules']:
try:
schedule = post_info['postSchedules'][0] # First schedule
trigger_at = schedule.get('trigger_at')
if trigger_at:
schedule_dt = datetime.fromisoformat(trigger_at.replace('Z', '+00:00'))
post_info['schedule_date'] = trigger_at
post_info['status'] = 'SCHEDULED'
except:
post_info['status'] = 'SCHEDULE_ERROR'
elif item.get('post_date'):
try:
post_dt = datetime.fromisoformat(item['post_date'].replace('Z', '+00:00'))
if item.get('is_published'):
if post_dt > current_time:
post_info['status'] = 'SCHEDULED'
else:
post_info['status'] = 'PUBLISHED'
else:
post_info['status'] = 'SCHEDULED'
except:
post_info['status'] = 'DATE_ERROR'
elif item.get('is_published'):
post_info['status'] = 'PUBLISHED'
else:
post_info['status'] = 'DRAFT'
all_found_posts[post_id] = post_info
print(f" -> {post_info['status']}: {post_info['title']}")
except Exception as e:
print(f"Error fetching individual draft {draft_id}: {e}")
except Exception as e:
print(f"Error getting drafts list: {e}")
print()
continue
url = f"{pub_url}{endpoint}"
print(f"--- TESTING {endpoint} ---")
try:
response = session.get(url)
print(f"Status: {response.status_code}")
if response.status_code == 200:
try:
data = response.json()
if isinstance(data, list):
print(f"Found {len(data)} items")
for item in data:
if isinstance(item, dict) and 'id' in item:
post_id = item['id']
# Sammle alle relevanten Daten
post_info = {
'id': post_id,
'endpoint': endpoint,
'title': item.get('title') or item.get('draft_title', 'NO TITLE'),
'is_published': item.get('is_published'),
'post_date': item.get('post_date'),
'draft_updated_at': item.get('draft_updated_at'),
'updated_at': item.get('updated_at'),
'postSchedules': item.get('postSchedules', []),
'status': 'UNKNOWN'
}
# Bestimme Status basierend auf Daten
# FIRST: Check for postSchedules (das ist der echte Schedule!)
if post_info['postSchedules']:
try:
schedule = post_info['postSchedules'][0] # First schedule
trigger_at = schedule.get('trigger_at')
if trigger_at:
schedule_dt = datetime.fromisoformat(trigger_at.replace('Z', '+00:00'))
post_info['schedule_date'] = trigger_at
post_info['status'] = 'SCHEDULED'
except:
post_info['status'] = 'SCHEDULE_ERROR'
elif item.get('post_date'):
try:
post_dt = datetime.fromisoformat(item['post_date'].replace('Z', '+00:00'))
if item.get('is_published'):
if post_dt > current_time:
post_info['status'] = 'SCHEDULED'
else:
post_info['status'] = 'PUBLISHED'
else:
post_info['status'] = 'SCHEDULED'
except:
post_info['status'] = 'DATE_ERROR'
elif item.get('is_published'):
post_info['status'] = 'PUBLISHED'
else:
post_info['status'] = 'DRAFT'
all_found_posts[post_id] = post_info
print(f" -> {post_info['status']}: {post_info['title']}")
elif isinstance(data, dict):
print(f"Dict response with keys: {list(data.keys())}")
if 'id' in data:
print(f"Single post found: {data.get('title', 'NO TITLE')}")
except Exception as e:
print(f"JSON parse error: {e}")
# Check if response contains useful text
if len(response.text) < 200:
print(f"Response text: {response.text}")
else:
print(f"HTTP Error: {response.status_code}")
if response.status_code == 403:
print(" -> Access denied")
elif response.status_code == 404:
print(" -> Endpoint not found")
except Exception as e:
print(f"Request error: {e}")
print()
return all_found_posts
def display_summary(all_posts):
"""Zeige schöne Zusammenfassung aller gefundenen Posts"""
print("=" * 80)
print("ZUSAMMENFASSUNG ALLER GEFUNDENEN POSTS")
print("=" * 80)
if not all_posts:
print("❌ KEINE POSTS GEFUNDEN!")
return
# Gruppiere nach Status
by_status = {}
for post in all_posts.values():
status = post['status']
if status not in by_status:
by_status[status] = []
by_status[status].append(post)
print(f"Insgesamt {len(all_posts)} eindeutige Posts gefunden")
print(f"Status-Verteilung: {dict([(k, len(v)) for k, v in by_status.items()])}")
print()
current_time = datetime.now()
for status, posts in by_status.items():
print(f"--- {status} ({len(posts)}) ---")
for post in sorted(posts, key=lambda x: x.get('post_date') or x.get('draft_updated_at') or ''):
print(f"ID: {post['id']}")
print(f" Title: {post['title']}")
print(f" Status: {post['status']}")
# Show scheduling info
if post.get('schedule_date'):
try:
schedule_dt = datetime.fromisoformat(post['schedule_date'].replace('Z', '+00:00'))
if schedule_dt > current_time:
print(f" SCHEDULED FOR: {schedule_dt.strftime('%Y-%m-%d %H:%M')} (in {(schedule_dt - current_time).total_seconds()/3600:.1f} hours)")
else:
print(f" Was scheduled for: {schedule_dt.strftime('%Y-%m-%d %H:%M')} (should be published now)")
except:
print(f" Schedule date: {post['schedule_date']} (parse error)")
elif post['post_date']:
try:
post_dt = datetime.fromisoformat(post['post_date'].replace('Z', '+00:00'))
if post_dt > current_time:
print(f" SCHEDULED FOR: {post_dt.strftime('%Y-%m-%d %H:%M')} (in {(post_dt - current_time).total_seconds()/3600:.1f} hours)")
else:
print(f" Published: {post_dt.strftime('%Y-%m-%d %H:%M')}")
except:
print(f" Post date: {post['post_date']} (parse error)")
# Show postSchedules info if available
if post.get('postSchedules'):
print(f" Schedule info: {post['postSchedules']}")
if post['draft_updated_at']:
print(f" Last updated: {post['draft_updated_at']}")
print(f" Found in: {post['endpoint']}")
print()
def find_missing_posts():
"""Versuche herauszufinden warum Posts fehlen könnten"""
print("=" * 80)
print("DIAGNOSE: WARUM FEHLEN POSTS?")
print("=" * 80)
# Test verschiedene Header-Kombinationen
test_headers = [
{"Accept": "application/json"},
{"Accept": "application/json", "X-Requested-With": "XMLHttpRequest"},
{"Accept": "*/*", "X-Requested-With": "XMLHttpRequest"},
{"Accept": "application/json", "Cache-Control": "no-cache"},
]
for i, headers in enumerate(test_headers):
print(f"\n--- TEST {i+1}: Headers {headers} ---")
test_session = requests.Session()
test_session.headers.update({
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
"Referer": pub_url,
})
test_session.headers.update(headers)
# Set cookies
for k, v in cookie_map.items():
if v:
test_session.cookies.set(k, v, domain=".substack.com")
try:
response = test_session.get(f"{pub_url}/api/v1/posts")
print(f"Posts endpoint: {response.status_code}")
if response.status_code == 200:
data = response.json()
print(f" Found {len(data)} posts")
response = test_session.get(f"{pub_url}/api/v1/drafts")
print(f"Drafts endpoint: {response.status_code}")
if response.status_code == 200:
data = response.json()
print(f" Found {len(data)} drafts")
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
print(f"Current time: {datetime.now()}")
print(f"Publication: {pub_url}\n")
# Hauptsuche
all_posts = get_all_posts()
# Zusammenfassung
display_summary(all_posts)
# Diagnose falls zu wenig gefunden
if len(all_posts) < 5: # Du hast gesagt 5 Posts total
find_missing_posts()