-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathupload_facebook.py
More file actions
443 lines (370 loc) Β· 14.5 KB
/
upload_facebook.py
File metadata and controls
443 lines (370 loc) Β· 14.5 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
#!/usr/bin/env python3
"""
Upload photos or reels to the Facebook Page via Graph API.
Supports single-photo, multi-photo posts, and Reels on the ModernHackers FB Page.
Uses the same credentials file as upload_instagram.py.
Usage:
python upload_facebook.py # all photos from config as multi-photo post
python upload_facebook.py --photo path/img.jpg # single photo
python upload_facebook.py --photo # latest photo from config
python upload_facebook.py --all # all photos from config directory
python upload_facebook.py --video path/reel.mp4 # upload as Facebook Reel
python upload_facebook.py --caption "My post" # custom caption
Credentials file: instagram_credentials.json
Config: project_config.json (facebook / project section)
"""
import argparse
import json
import os
import sys
import time
import urllib.error
import urllib.parse
import urllib.request
from pathlib import Path
# Graph API version
API_VERSION = "v21.0"
API_BASE = f"https://graph.facebook.com/{API_VERSION}"
# Retry settings
MAX_RETRIES = 3
RETRIABLE_HTTP_CODES = {500, 502, 503, 504}
def load_credentials(creds_file):
"""Load Meta API credentials."""
creds_path = Path(creds_file)
if not creds_path.exists():
print(f"ERROR: Credentials file not found: {creds_file}")
print("See INSTAGRAM_SETUP.md for setup instructions.")
sys.exit(1)
with open(creds_path) as f:
creds = json.load(f)
required = ["page_id", "page_access_token"]
missing = [k for k in required if not creds.get(k)]
if missing:
print(f"ERROR: Missing fields in {creds_file}: {', '.join(missing)}")
sys.exit(1)
return creds
def load_config(config_path):
"""Load project configuration."""
if Path(config_path).exists():
with open(config_path) as f:
return json.load(f)
return {}
def api_request(url, data=None, method="GET", headers=None, timeout=60, raw_data=None):
"""Make a Graph API request with retry logic."""
for attempt in range(MAX_RETRIES + 1):
try:
if raw_data is not None:
body = raw_data
elif data is not None:
body = urllib.parse.urlencode(data).encode()
else:
body = None
req = urllib.request.Request(url, data=body, method=method)
if headers:
for k, v in headers.items():
req.add_header(k, v)
resp = urllib.request.urlopen(req, timeout=timeout)
return json.loads(resp.read())
except urllib.error.HTTPError as e:
err_body = e.read().decode()
if e.code in RETRIABLE_HTTP_CODES and attempt < MAX_RETRIES:
wait = 2 ** (attempt + 1)
print(f" Retriable HTTP {e.code}, retrying in {wait}s...")
time.sleep(wait)
continue
try:
err_json = json.loads(err_body)
msg = err_json.get("error", {}).get("error_user_msg") or err_json.get("error", {}).get("message", err_body)
except json.JSONDecodeError:
msg = err_body
raise RuntimeError(f"HTTP {e.code}: {msg}")
except urllib.error.URLError as e:
if attempt < MAX_RETRIES:
wait = 2 ** (attempt + 1)
print(f" Network error: {e.reason}, retrying in {wait}s...")
time.sleep(wait)
continue
raise RuntimeError(f"Network error: {e.reason}")
raise RuntimeError("Max retries exceeded")
def upload_photo(page_id, token, photo_path, published=False, message=None):
"""
Upload a photo to the Facebook Page.
Returns the Facebook Photo ID.
"""
boundary = f"----PythonBoundary{os.getpid()}"
with open(photo_path, "rb") as f:
photo_data = f.read()
fields = [
("published", str(published).lower()),
("access_token", token),
]
if message and published:
fields.append(("message", message))
body = b""
for key, val in fields:
body += f"--{boundary}\r\nContent-Disposition: form-data; name=\"{key}\"\r\n\r\n{val}\r\n".encode()
filename = Path(photo_path).name
body += f"--{boundary}\r\nContent-Disposition: form-data; name=\"source\"; filename=\"{filename}\"\r\nContent-Type: image/jpeg\r\n\r\n".encode()
body += photo_data
body += f"\r\n--{boundary}--\r\n".encode()
result = api_request(
f"{API_BASE}/{page_id}/photos",
raw_data=body,
method="POST",
headers={"Content-Type": f"multipart/form-data; boundary={boundary}"},
timeout=120,
)
return result["id"]
def publish_multi_photo_post(page_id, token, fb_photo_ids, message):
"""
Publish a Facebook Page post with multiple photos.
Uses /{page_id}/feed with attached_media referencing
previously uploaded (unpublished) photo IDs.
Returns the post ID.
"""
data = {
"message": message,
"access_token": token,
}
for i, photo_id in enumerate(fb_photo_ids):
data[f"attached_media[{i}]"] = json.dumps({"media_fbid": photo_id})
result = api_request(f"{API_BASE}/{page_id}/feed", data=data, method="POST", timeout=30)
return result["id"]
def upload_video_reel(page_id, token, video_path, description=""):
"""
Upload a video as a Reel to the Facebook Page.
Uses /{page_id}/videos with published=true.
Returns (video_id, permalink).
"""
boundary = f"----PythonBoundary{os.getpid()}"
with open(video_path, "rb") as f:
video_data = f.read()
fields = [
("published", "true"),
("access_token", token),
]
if description:
fields.append(("description", description))
body = b""
for key, val in fields:
body += f"--{boundary}\r\nContent-Disposition: form-data; name=\"{key}\"\r\n\r\n{val}\r\n".encode()
filename = Path(video_path).name
body += f"--{boundary}\r\nContent-Disposition: form-data; name=\"source\"; filename=\"{filename}\"\r\nContent-Type: video/mp4\r\n\r\n".encode()
body += video_data
body += f"\r\n--{boundary}--\r\n".encode()
result = api_request(
f"{API_BASE}/{page_id}/videos",
raw_data=body,
method="POST",
headers={"Content-Type": f"multipart/form-data; boundary={boundary}"},
timeout=300,
)
video_id = result["id"]
# Try to get permalink
try:
url = f"{API_BASE}/{video_id}?fields=permalink_url&access_token={urllib.parse.quote(token)}"
info = api_request(url)
permalink = info.get("permalink_url", "")
except Exception:
permalink = ""
return video_id, permalink
def find_photos_from_config(config):
"""Find photo files from the config paths.photos directory."""
photos_dir = config.get("paths", {}).get("photos", "")
if not photos_dir:
return []
photos_path = Path(photos_dir)
if not photos_path.is_dir():
return []
exts = {".jpg", ".jpeg", ".png"}
return sorted(
[p for p in photos_path.iterdir() if p.is_file() and p.suffix.lower() in exts],
key=lambda p: p.name,
)
def build_caption(config, args_caption=None):
"""Build caption from config and/or CLI args.
Reads from project section first, facebook section can override."""
project_cfg = config.get("project", {})
fb_cfg = config.get("facebook", {})
if args_caption:
caption = args_caption
else:
caption = fb_cfg.get("default_caption") or project_cfg.get("title", "")
hashtags = fb_cfg.get("hashtags") or project_cfg.get("hashtags", [])
if hashtags:
existing_tags = {t.strip().lower() for t in caption.split() if t.startswith("#")}
new_tags = [f"#{t}" if not t.startswith("#") else t for t in hashtags]
new_tags = [t for t in new_tags if t.lower() not in existing_tags]
if new_tags:
caption = caption.rstrip() + "\n\n" + " ".join(new_tags)
return caption.strip()
def main():
"""Main entry point."""
parser = argparse.ArgumentParser(
description="Upload photos or reels to Facebook Page via Graph API"
)
parser.add_argument(
"--photo",
type=str,
nargs="?",
const="__FROM_CONFIG__",
help="Photo file to upload (JPG/PNG). If no path given, picks latest from config",
)
parser.add_argument(
"--video",
type=str,
help="Video file to upload as Facebook Reel (MP4)",
)
parser.add_argument(
"--all",
action="store_true",
help="Upload all photos from config paths.photos directory",
)
parser.add_argument(
"--caption",
type=str,
help="Post caption (default: from config)",
)
parser.add_argument(
"--config",
type=str,
default="project_config.json",
help="Project config file (default: project_config.json)",
)
parser.add_argument(
"--credentials",
type=str,
default="instagram_credentials.json",
help="Credentials file (default: instagram_credentials.json)",
)
args = parser.parse_args()
config = load_config(args.config)
creds = load_credentials(args.credentials)
page_id = creds["page_id"]
token = creds["page_access_token"]
page_name = creds.get("page_name", page_id)
# --- Video / Reel mode ---
if args.video:
video_path = Path(args.video)
if not video_path.exists():
print(f"ERROR: Video file not found: {video_path}")
sys.exit(1)
caption = build_caption(config, args.caption)
size_mb = video_path.stat().st_size / (1024 * 1024)
print("=" * 72)
print("π Facebook Page Reel Upload")
print("=" * 72)
print(f"Page: {page_name}")
print(f"Video: {video_path.name}")
print(f"Size: {size_mb:.1f} MB")
if caption:
display = caption[:80] + ("..." if len(caption) > 80 else "")
print(f"Caption: {display}")
print("=" * 72 + "\n")
try:
print(f" Uploading {video_path.name} ({size_mb:.1f} MB)...")
video_id, permalink = upload_video_reel(page_id, token, str(video_path), caption)
print(f" β Video ID: {video_id}")
print("\n" + "=" * 72)
print("β
Facebook Reel Published")
print("=" * 72)
print(f"Video ID: {video_id}")
if permalink:
print(f"URL: https://www.facebook.com{permalink}")
else:
print(f"URL: https://www.facebook.com/{page_id}/videos/{video_id}")
print("=" * 72)
except RuntimeError as e:
print(f"\nβ Upload failed: {e}")
sys.exit(1)
except Exception as e:
print(f"\nβ Unexpected error: {e}")
import traceback
traceback.print_exc()
sys.exit(1)
return
# --- Photo mode ---
# Determine photos to upload
photo_paths = []
if args.photo and args.photo != "__FROM_CONFIG__":
p = Path(args.photo)
if not p.exists():
print(f"ERROR: File not found: {p}")
sys.exit(1)
photo_paths = [p]
else:
available = find_photos_from_config(config)
if not available:
photos_dir = config.get("paths", {}).get("photos", "(not set)")
print(f"ERROR: No photos found in: {photos_dir}")
print("Set paths.photos in project_config.json or use --photo <file>")
sys.exit(1)
if args.all or args.photo is None:
photo_paths = available
else:
# --photo with no arg: pick latest
photo_paths = [available[-1]]
print(f"Found {len(available)} photos in: {config['paths']['photos']}")
for p in available:
size = p.stat().st_size / (1024 * 1024)
marker = " β" if p in photo_paths else ""
print(f" {p.name} ({size:.1f} MB){marker}")
print()
caption = build_caption(config, args.caption)
total_mb = sum(p.stat().st_size for p in photo_paths) / (1024 * 1024)
print("=" * 72)
print("π Facebook Page Upload")
print("=" * 72)
print(f"Page: {page_name}")
print(f"Photos: {len(photo_paths)}")
print(f"Total size: {total_mb:.1f} MB")
if caption:
display = caption[:80] + ("..." if len(caption) > 80 else "")
print(f"Caption: {display}")
print("=" * 72 + "\n")
try:
if len(photo_paths) == 1:
# Single photo: publish directly
photo_path = photo_paths[0]
name = photo_path.name
print(f" Uploading {name}...")
photo_id = upload_photo(page_id, token, str(photo_path), published=True, message=caption)
post_url = f"https://www.facebook.com/photo/?fbid={photo_id}"
print(f" β Photo ID: {photo_id}")
print("\n" + "=" * 72)
print("β
Facebook Photo Published")
print("=" * 72)
print(f"Photo ID: {photo_id}")
print(f"URL: {post_url}")
print("=" * 72)
else:
# Multi-photo: upload all as unpublished, then create feed post
fb_photo_ids = []
for i, photo_path in enumerate(photo_paths, 1):
name = photo_path.name
size_mb = photo_path.stat().st_size / (1024 * 1024)
print(f" [{i}/{len(photo_paths)}] {name} ({size_mb:.1f} MB)")
fb_id = upload_photo(page_id, token, str(photo_path), published=False)
fb_photo_ids.append(fb_id)
print(f" β Photo ID: {fb_id}")
print(f"\n Publishing multi-photo post ({len(fb_photo_ids)} photos)...")
post_id = publish_multi_photo_post(page_id, token, fb_photo_ids, caption)
post_url = f"https://www.facebook.com/{post_id}"
print(f" β Post ID: {post_id}")
print("\n" + "=" * 72)
print("β
Facebook Multi-Photo Post Published")
print("=" * 72)
print(f"Post ID: {post_id}")
print(f"Photos: {len(fb_photo_ids)}")
print(f"URL: {post_url}")
print("=" * 72)
except RuntimeError as e:
print(f"\nβ Upload failed: {e}")
sys.exit(1)
except Exception as e:
print(f"\nβ Unexpected error: {e}")
import traceback
traceback.print_exc()
sys.exit(1)
if __name__ == "__main__":
main()