-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate.py
More file actions
81 lines (67 loc) · 3.01 KB
/
Copy pathmigrate.py
File metadata and controls
81 lines (67 loc) · 3.01 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
"""Side-by-side migration example: kevinzg/facebook-scraper → socialapis.
The shape stays familiar — `FacebookScraper` is an exact alias of
`socialapis.Facebook`, so changing your import is the entire migration.
Run this:
1. Sign up free at https://socialapis.io/auth/signup
2. export SOCIALAPIS_TOKEN="<paste your token from the dashboard>"
3. pip install socialapis-sdk
4. python examples/migrate.py
"""
from __future__ import annotations
import os
# ---------------------------------------------------------------------------
# BEFORE — kevinzg/facebook-scraper (abandoned, breaks on every Meta change)
# ---------------------------------------------------------------------------
#
# from facebook_scraper import get_page_info, get_posts
#
# page = get_page_info("EngenSA")
# print(page["name"], page["likes"])
#
# for post in get_posts("EngenSA", pages=5):
# print(post["time"], post["text"][:80])
# ---------------------------------------------------------------------------
# AFTER — socialapis (hosted, typed, maintained)
# ---------------------------------------------------------------------------
from socialapis import FacebookScraper, InsufficientCreditsError, RateLimitError
def main() -> None:
token = os.environ.get("SOCIALAPIS_TOKEN")
if not token:
raise SystemExit(
"Set SOCIALAPIS_TOKEN — sign up free at "
"https://socialapis.io/auth/signup"
)
# FacebookScraper is an alias of socialapis.Facebook. Same class,
# same methods, identical behaviour — just a name that mirrors
# kevinzg's import surface for greppable migrations.
with FacebookScraper(api_token=token) as fb:
try:
page = fb.get_page_info("EngenSA")
except RateLimitError as exc:
raise SystemExit(
f"Rate-limited. Wait {exc.retry_after_seconds}s and retry."
) from exc
except InsufficientCreditsError:
raise SystemExit(
"Out of credits. Upgrade at https://socialapis.io/pricing"
) from None
# Same data kevinzg returned, but typed — page.title not page["name"].
# Field names match the real API exactly (see PageInfo in the SDK docs).
print(f"Page: {page.title}")
print(f" Category: {page.category}")
print(f" Likes: {page.likes_count:,}" if page.likes_count else " Likes: n/a")
print(
f" Followers: {page.followers_count:,}"
if page.followers_count
else " Followers: n/a"
)
print(f" Bio: {(page.bio or '')[:80]}")
# kevinzg's `for post in get_posts(...)` equivalent —
# paginate via cursors instead of `pages=N`.
result = fb.get_page_posts("EngenSA")
for post in result.get("posts", [])[:5]:
timestamp = post.get("time") or post.get("published_at", "?")
text = post.get("text") or post.get("message", "")
print(f" [{timestamp}] {text[:80]}")
if __name__ == "__main__":
main()