-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_socialblade_csv.py
More file actions
69 lines (56 loc) · 1.91 KB
/
Copy pathexport_socialblade_csv.py
File metadata and controls
69 lines (56 loc) · 1.91 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
"""Flatten saved Social Blade Dataset JSON into a useful CSV export."""
from __future__ import annotations
import csv
import json
import sys
from pathlib import Path
from typing import Any
FIELDS = [
"recordType",
"status",
"platform",
"creatorId",
"username",
"displayName",
"primaryAudience",
"primaryAudienceMetric",
"mediaCount",
"engagementRate",
"averageLikes",
"averageComments",
"globalRank",
"audienceRank",
"viewsRank",
"countryRank",
"categoryRank",
"earningsStatus",
"responseTimeMs",
"httpRequests",
"scrapedAt",
]
def _read_items(path: Path) -> list[dict[str, Any]]:
value = json.loads(path.read_text(encoding="utf-8"))
if isinstance(value, dict):
return [value]
if not isinstance(value, list) or not all(isinstance(item, dict) for item in value):
raise ValueError("Input JSON must contain one object or an array of objects.")
return value
def _row(item: dict[str, Any]) -> dict[str, Any]:
row = {field: item.get(field) for field in FIELDS}
row["audience"] = json.dumps(item.get("audience"), ensure_ascii=False)
row["content"] = json.dumps(item.get("content"), ensure_ascii=False)
row["dataQuality"] = json.dumps(item.get("dataQuality"), ensure_ascii=False)
row["error"] = json.dumps(item.get("error"), ensure_ascii=False)
return row
def main() -> None:
if len(sys.argv) != 3:
raise SystemExit("Usage: python export_socialblade_csv.py INPUT.json OUTPUT.csv")
input_path, output_path = map(Path, sys.argv[1:])
rows = [_row(item) for item in _read_items(input_path)]
fieldnames = FIELDS + ["audience", "content", "dataQuality", "error"]
with output_path.open("w", newline="", encoding="utf-8") as handle:
writer = csv.DictWriter(handle, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(rows)
if __name__ == "__main__":
main()