import csv,datetime,decimal,hashlib,json,pathlib,re,sys
source,manifest,target=map(pathlib.Path,sys.argv[1:4])
request=json.loads(manifest.read_text(encoding="utf-8"));params=request.get("params")
if request.get("action")!="xiaohongshu.pgy.get_keyword_daily" or not isinstance(params,dict) or set(params)!={"search_word"}:raise ValueError("Record the exact daily keyword request")
keyword=params["search_word"]
if not isinstance(keyword,str) or not keyword.strip() or not 1<=len(keyword)<=100:raise ValueError("One nonempty keyword required")
stamp=request["collected_at"]
if datetime.datetime.fromisoformat(stamp.replace("Z","+00:00")).tzinfo is None:raise ValueError("Timezone required")
raw=source.read_bytes();r=json.loads(raw)
if r.get("status")!="succeeded" or r.get("action")!=request["action"]:raise ValueError("Expected successful keyword response")
rid=r.get("request_id")
if not isinstance(rid,str) or not rid.strip():raise ValueError("request_id required")
envelope=r.get("data")
if not isinstance(envelope,dict) or envelope.get("success") is not True:raise ValueError("PGY business failure")
data=envelope.get("data")
if not isinstance(data,dict) or not isinstance(data.get("dailyData"),list):raise ValueError("Expected non-null dailyData array")
def day(value):
if type(value) is int:value=str(value)
if not isinstance(value,str):raise ValueError("Unsupported dateKey")
if re.fullmatch(r"[0-9]{8}",value):return datetime.datetime.strptime(value,"%Y%m%d").date()
if re.fullmatch(r"[0-9]{4}-[0-9]{2}-[0-9]{2}",value):return datetime.date.fromisoformat(value)
raise ValueError("Helper accepts YYYYMMDD or YYYY-MM-DD date labels only")
def metric(value,integer=False):
if value is None:return None
if type(value) not in (str,int,float):raise ValueError("Unsupported numeric value")
text=str(value)
if not re.fullmatch(r"[0-9]+(?:\.[0-9]+)?",text):raise ValueError("Unsupported formatted number")
number=decimal.Decimal(text)
if integer and number!=number.to_integral_value():raise ValueError("Count must be whole")
return number
points={}
for item in data["dailyData"]:
if not isinstance(item,dict):raise ValueError("Invalid daily point")
date=day(item.get("dateKey"))
if date in points:raise ValueError("Duplicate day; inspect before merging")
points[date]={"date":date.isoformat(),"dateKey":item["dateKey"],
"searchScore":metric(item.get("searchScore")),"noteNum":metric(item.get("noteNum"),True),
"userNum":metric(item.get("userNum"),True)}
summary={"keyword":keyword,"request_id":rid,"collected_at":stamp,"source_sha256":hashlib.sha256(raw).hexdigest(),
"returned_days":len(points),"missing_dates":[],"comparison_state":"insufficient_data",
"comparison":"Two adjacent seven-day averages ending at the latest returned date label; not an official period-over-period metric",
"date_scope":"Returned date labels; no timezone conversion or guaranteed three-month coverage"}
if points:
first,last=min(points),max(points);summary.update({"first_date":first.isoformat(),"last_date":last.isoformat()})
if (last-first).days>3660:raise ValueError("Unexpected date span; inspect source")
summary["missing_dates"]=[(first+datetime.timedelta(days=i)).isoformat() for i in range((last-first).days+1) if first+datetime.timedelta(days=i) not in points]
dates=[last-datetime.timedelta(days=i) for i in range(14)]
if all(date in points and points[date]["searchScore"] is not None for date in dates):
current=sum(points[date]["searchScore"] for date in dates[:7])/7
previous=sum(points[date]["searchScore"] for date in dates[7:])/7
summary.update({"recent_7d_mean":str(current),"previous_7d_mean":str(previous),"mean_change":str(current-previous),
"relative_change":None if previous==0 else str((current-previous)/previous),
"comparison_state":"zero_baseline" if previous==0 else "computed"})
def cell(value):
if value is None:return ""
text=str(value)
return "'"+text if text.lstrip().startswith(("=","+","-","@")) or text.startswith(("\t","\r","\n")) else text
target.mkdir()
with (target/"daily.csv").open("x",encoding="utf-8-sig",newline="") as output:
writer=csv.DictWriter(output,fieldnames=["keyword","date","dateKey","searchScore","noteNum","userNum","request_id","collected_at"]);writer.writeheader()
for date in sorted(points):writer.writerow({key:cell(value) for key,value in {"keyword":keyword,**points[date],"request_id":rid,"collected_at":stamp}.items()})
(target/"raw-response.json").write_bytes(raw)
(target/"summary.json").write_text(json.dumps(summary,ensure_ascii=False,indent=2),encoding="utf-8")
print(str(target/"daily.csv"))