-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.py
More file actions
82 lines (68 loc) · 3.33 KB
/
Copy pathuser.py
File metadata and controls
82 lines (68 loc) · 3.33 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
"""GET /api/v1/reddit/user - one public account's recent submissions and comments.
export CHOCODATA_API_KEY="your_key"
python reddit_user_scraper_api_codes/user.py spez
python reddit_user_scraper_api_codes/user.py spez --kind submitted --limit 10
Prints the profile block and every item the feed carried. Get a key at chocodata.com.
"""
import argparse
import json
import os
import sys
import requests
ENDPOINT = "https://api.chocodata.com/api/v1/reddit/user"
KEY = os.environ.get("CHOCODATA_API_KEY", "")
def check(r: requests.Response) -> None:
"""Map the documented statuses onto something you can act on."""
if r.status_code == 200:
return
if r.status_code == 400:
sys.exit(f"400 invalid_params: {r.text[:300]}")
if r.status_code == 401:
sys.exit("401 INVALID_API_KEY: key missing or not recognised. "
"Get one at https://chocodata.com")
if r.status_code == 402:
sys.exit("402 INSUFFICIENT_CREDITS: balance exhausted. Top up at https://chocodata.com")
if r.status_code == 404:
sys.exit("404 item_not_found: no such account, or it is suspended or deleted. "
"Retrying will not help; check the username.")
if r.status_code == 429:
sys.exit("429 RATE_LIMITED: over your plan's concurrency. Back off and retry.")
if r.status_code == 502:
sys.exit("502 target_unreachable: Reddit refused this request. Retry after ~8s; "
"if it repeats, check the username.")
sys.exit(f"{r.status_code}: {r.text[:300]}")
def get_user(username: str, kind: str = "overview", limit: int = 25) -> dict:
if not KEY:
sys.exit("Set CHOCODATA_API_KEY first. Free key at https://chocodata.com")
r = requests.get(ENDPOINT, timeout=90,
params={"api_key": KEY, "username": username, "kind": kind, "limit": limit})
check(r)
return r.json()
def main() -> None:
ap = argparse.ArgumentParser(description="Scrape one public Reddit account's feed.")
ap.add_argument("username", nargs="?", default="spez",
help="account name, with or without the leading u/")
ap.add_argument("--kind", default="overview", choices=["overview", "submitted", "comments"])
ap.add_argument("--limit", type=int, default=25, help="1 to 25")
ap.add_argument("--json", action="store_true", help="print the raw response instead")
a = ap.parse_args()
data = get_user(a.username, a.kind, a.limit)
if a.json:
print(json.dumps(data, indent=2, ensure_ascii=False))
return
p = data["profile"]
print(f"u/{p['username']} {p['bio'] or '(no bio on this surface)'}")
print(f"{p['profile_url']} karma={p['total_karma']} created={p['created']}")
print(f"\n{data['total_results']} items ({a.kind}):\n")
for i, item in enumerate(data["items"], 1):
body = (item["body"] or "").replace("\n", " ")
print(f"{i:>2}. [{item['type']:<10}] r/{str(item['subreddit']):<20} "
f"{str(item['created'])[:10]} score={item['score']}")
print(f" {str(item['title'])[:88]}")
if body:
print(f" {body[:88]}")
kinds = {i["type"] for i in data["items"]}
print(f"\n{data['total_results']} items, types={sorted(kinds)}, "
f"source={data['_source']}, every score={data['items'][0]['score']}")
if __name__ == "__main__":
main()