import csv,datetime,hashlib,json,pathlib,re,sys
manifest_file,output=map(pathlib.Path,sys.argv[1:3])
manifest=json.loads(manifest_file.read_text(encoding="utf-8"))
snapshots=manifest.get("snapshots");selected=manifest.get("selected",[])
if not isinstance(snapshots,list) or not snapshots or not isinstance(selected,list) or len(selected)>3:raise ValueError("Provide saved snapshots and at most three selected posts")
rows=[];posts={};sources=[];communities=set();requests=set();stats={"ad_elements":0,"unusable_elements":0,"duplicate_posts":0}
for snapshot in snapshots:
params=snapshot["params"]
if not isinstance(params,dict) or set(params)-{"subreddit_name","sort","language","need_format","after"}:raise ValueError("Unsupported feed parameter")
community=params.get("subreddit_name")
if not isinstance(community,str) or not re.fullmatch(r"[A-Za-z0-9_]{2,21}",community) or community.lower() in communities:raise ValueError("One saved first page per distinct community")
if params.get("sort")!="NEW" or params.get("need_format") is not False or params.get("language")!="en-US" or params.get("after","")!="":raise ValueError("Use this workflow's raw NEW first-page scope")
communities.add(community.lower())
stamp=snapshot["collected_at"];dt=datetime.datetime.fromisoformat(stamp.replace("Z","+00:00"))
if dt.tzinfo is None:raise ValueError("Collection time needs timezone")
source=pathlib.Path(snapshot["file"])
if not source.is_absolute():source=manifest_file.parent/source
raw=source.read_bytes();response=json.loads(raw)
if not isinstance(response,dict) or response.get("status")!="succeeded" or response.get("action")!="reddit.app.fetch_subreddit_feed" or response.get("error") is not None:raise ValueError("Expected successful raw subreddit feed")
rid=response.get("request_id")
if not isinstance(rid,str) or not rid.strip() or rid in requests:raise ValueError("Distinct real request IDs required")
requests.add(rid)
elements=response["data"]["subredditV3"]["elements"];edges=elements.get("edges")
if not isinstance(edges,list):raise ValueError("Missing feed edges is not an empty page")
digest=hashlib.sha256(raw).hexdigest();saved="raw-"+str(len(sources)+1)+".json"
sources.append({"saved_file":saved,"source_file":str(source.resolve()),"params":params,"collected_at":stamp,"request_id":rid,"sha256":digest,"bytes":raw})
page_posts={}
for position,edge in enumerate(edges,1):
if not isinstance(edge,dict):raise ValueError("Invalid feed edge")
node=edge.get("node")
if node is None:stats["unusable_elements"]+=1;continue
if not isinstance(node,dict):raise ValueError("Unusable non-null feed node")
if node.get("adPayload") is not None:stats["ad_elements"]+=1;continue
ident=node.get("id")
if not isinstance(ident,str) or not re.fullmatch(r"t3_[a-z0-9]{1,17}",ident):stats["unusable_elements"]+=1;continue
cells=node.get("cells")
if not isinstance(cells,list):raise ValueError("Post fields require cells array")
values={}
for cell in cells:
if not isinstance(cell,dict):raise ValueError("Invalid post cell")
for field in ("title","authorName","createdAt","score","commentCount"):
value=cell.get(field)
if value is None:continue
if field in ("score","commentCount"):
if type(value) is not int or abs(value)>9007199254740991 or (field=="commentCount" and value<0):raise ValueError("Invalid or unsafe post count")
elif not isinstance(value,str):raise ValueError("Invalid post text")
if field in values and values[field]!=value:raise ValueError("Conflicting values across cells")
values[field]=value
row={"post_id":ident,"source_community":community,"response_position":position,"title":values.get("title"),"author":values.get("authorName"),"created_at":values.get("createdAt"),"score":values.get("score"),"comment_count":values.get("commentCount"),"post_link":"https://www.reddit.com/comments/"+ident[3:]+"/","request_id":rid,"collected_at":stamp,"source_sha256":digest}
projection={k:row[k] for k in ("title","author","created_at","score","comment_count")}
if ident in page_posts and page_posts[ident]!=projection:raise ValueError("Conflicting duplicate post in one page")
page_posts[ident]=projection
if ident in posts:
stats["duplicate_posts"]+=1
continue
posts[ident]=row;rows.append(row)
plans=[];chosen=set()
for selection in selected:
ident=selection.get("post_id");reason=selection.get("reason")
if ident not in posts or ident in chosen or not isinstance(reason,str) or not reason.strip():raise ValueError("Select a real unique exported post with a review reason")
chosen.add(ident);row=posts[ident]
plans.append({"action":"reddit.app.fetch_post_comments","params":{"post_id":ident,"sort_type":"NEW","need_format":False,"after":"","language":"en-US"},"reason":reason,"source_request_id":row["request_id"],"source_sha256":row["source_sha256"],"executed":False})
output.mkdir()
for item in sources:(output/item["saved_file"]).write_bytes(item["bytes"])
(output/"sources.json").write_text(json.dumps([{k:v for k,v in item.items() if k!="bytes"} for item in sources],ensure_ascii=False,indent=2),encoding="utf-8")
(output/"selected-comment-requests.json").write_text(json.dumps(plans,ensure_ascii=False,indent=2),encoding="utf-8")
columns=["post_id","source_community","response_position","title","author","created_at","score","comment_count","post_link","request_id","collected_at","source_sha256"]
with (output/"posts.csv").open("x",encoding="utf-8-sig",newline="") as handle:
writer=csv.DictWriter(handle,fieldnames=columns);writer.writeheader()
for row in rows:writer.writerow({k:("'"+v if isinstance(v,str) and (v.lstrip().startswith(("=","+","-","@")) or v.startswith(("\t","\r","\n"))) else v) for k,v in row.items()})
(output/"coverage.json").write_text(json.dumps({"saved_communities":len(communities),"exported_posts":len(rows),**stats,"selected_comment_calls":len(plans),"executed_calls":0,"scope":"Saved NEW first pages only; no time-window ranking or full-community coverage"},indent=2),encoding="utf-8")
print(str(output))