dsh-zju-lecture-note 2.2.8 → 2.2.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,6 +5,9 @@ Formats timestamps, groups text by minutes, slices by time window,
5
5
  and prepares structured text for agent consumption.
6
6
  """
7
7
 
8
+ import datetime
9
+ import os
10
+ import re
8
11
  from typing import Any, Dict, List, Optional, Tuple
9
12
 
10
13
 
@@ -277,18 +280,213 @@ def format_quality_line(quality: Dict[str, Any]) -> str:
277
280
  return line
278
281
 
279
282
 
283
+ def _slide_seconds(slide: Dict[str, Any]) -> Optional[int]:
284
+ """Return the transcript-relative second for one slide, if available.
285
+
286
+ Use explicit ``is not None`` checks: page 1 is legitimately ``time=0`` and
287
+ must not be skipped by a truthiness test.
288
+ """
289
+ for key in ("time", "time_point", "sec"):
290
+ if key not in slide:
291
+ continue
292
+ value = slide.get(key)
293
+ if value is None:
294
+ continue
295
+ try:
296
+ return max(0, int(round(float(value))))
297
+ except (TypeError, ValueError):
298
+ continue
299
+ return None
300
+
301
+
302
+ def _minute_label(seconds: int) -> str:
303
+ """``HH:MM`` label aligned with the transcript's ``[HH:MM]`` lines."""
304
+ seconds = max(0, int(seconds))
305
+ return f"{seconds // 3600:02d}:{(seconds % 3600) // 60:02d}"
306
+
307
+
308
+ def _suggest_pages(pages: List[int], budget: int = 24) -> List[int]:
309
+ """Evenly spaced page fallback when the model has no key-point map."""
310
+ unique = sorted({int(page) for page in pages if page})
311
+ if not unique:
312
+ return []
313
+ if len(unique) <= budget:
314
+ return unique
315
+ if budget <= 1:
316
+ return [unique[0]]
317
+ step = (len(unique) - 1) / float(budget - 1)
318
+ picked = {unique[0], unique[-1]}
319
+ for index in range(1, budget - 1):
320
+ picked.add(unique[int(round(index * step))])
321
+ return sorted(picked)
322
+
323
+
324
+ def _capture_date(slides_list: List[Dict[str, Any]]) -> str:
325
+ values: List[int] = []
326
+ for slide in slides_list:
327
+ value = slide.get("capture_ms")
328
+ if value is None:
329
+ continue
330
+ try:
331
+ values.append(int(value))
332
+ except (TypeError, ValueError):
333
+ continue
334
+ if not values:
335
+ return ""
336
+ try:
337
+ moment = datetime.datetime.fromtimestamp(
338
+ min(values) / 1000.0,
339
+ tz=datetime.timezone(datetime.timedelta(hours=8)),
340
+ )
341
+ except (OverflowError, OSError, ValueError):
342
+ return ""
343
+ weekdays = ("周一", "周二", "周三", "周四", "周五", "周六", "周日")
344
+ return f"{moment:%Y-%m-%d}({weekdays[moment.weekday()]})"
345
+
346
+
347
+ def _format_compact_slides(
348
+ slides_list: List[Dict[str, Any]],
349
+ pdf_path: str = "",
350
+ download_attempted: bool = False,
351
+ ) -> str:
352
+ """A page-time index instead of 150 × two absolute-path lines.
353
+
354
+ The model only needs the directory, the file-name pattern, and the
355
+ page→time axis to pick the 15–25 pages worth reading. Repeating every
356
+ local path for a 150-page deck is pure context cost.
357
+ """
358
+ total = len(slides_list)
359
+ pages: List[int] = []
360
+ timed: List[Tuple[int, int]] = []
361
+ for idx, slide in enumerate(slides_list, start=1):
362
+ page = int(slide.get("page") or idx)
363
+ pages.append(page)
364
+ seconds = _slide_seconds(slide)
365
+ if seconds is not None:
366
+ timed.append((page, seconds))
367
+
368
+ span = ""
369
+ if timed:
370
+ span = f",时间轴 {_minute_label(timed[0][1])}–{_minute_label(timed[-1][1])}"
371
+ lines = [f"### 课堂投影 PPT 截图(共 {total} 页{span})"]
372
+
373
+ captured = _capture_date(slides_list)
374
+ if captured:
375
+ lines.append(f"拍摄日期: {captured}")
376
+
377
+ local = [
378
+ slide for slide in slides_list
379
+ if str(slide.get("local_path") or "").strip()
380
+ ]
381
+ if local:
382
+ first = local[0]
383
+ last = local[-1]
384
+ relative = str(first.get("workspace_rel") or "").strip()
385
+ if relative and "/" in relative:
386
+ directory = relative.rsplit("/", 1)[0]
387
+ elif relative:
388
+ directory = "."
389
+ else:
390
+ directory = os.path.dirname(str(first.get("local_path") or ""))
391
+ lines.append(f"截图目录: {directory}")
392
+ first_name = os.path.basename(str(first.get("local_path") or ""))
393
+ last_name = os.path.basename(str(last.get("local_path") or ""))
394
+ stem, extension = os.path.splitext(first_name)
395
+ pattern = re.sub(r"\d+$", "XXXX", stem) + extension
396
+ lines.append(f"文件命名: {first_name} … {last_name}(模式 {pattern})")
397
+ else:
398
+ urls = [
399
+ str(slide.get("image_url") or "").strip()
400
+ for slide in slides_list
401
+ if str(slide.get("image_url") or "").strip()
402
+ ]
403
+ if urls:
404
+ lines.append(f"截图地址示例: {urls[0]}")
405
+ if download_attempted:
406
+ lines.append(
407
+ "说明: 本次 download=true,但没有可用的本地截图;"
408
+ "请检查上方下载失败诊断,或重试一次下载。"
409
+ )
410
+ else:
411
+ lines.append(
412
+ "说明: 本次 download=false,未下载截图;"
413
+ "需要本地文件/PDF 时再按工具提示执行下载。"
414
+ )
415
+
416
+ if pdf_path:
417
+ lines.append(f"完整 PDF: {pdf_path}")
418
+
419
+ if timed:
420
+ shown = timed[:240]
421
+ axis = " ".join(
422
+ f"{page}={_minute_label(seconds)}" for page, seconds in shown
423
+ )
424
+ if len(timed) > len(shown):
425
+ axis += f" ...(仅列前 {len(shown)} 页,共 {len(timed)} 页)"
426
+ lines.append("页时间轴(相对第 1 页): " + axis)
427
+ else:
428
+ lines.append("页时间轴: 接口未提供可用时间戳,按页码顺序抽样。")
429
+
430
+ suggested = _suggest_pages(pages, budget=24)
431
+ if suggested:
432
+ lines.append(
433
+ "建议抽查(无明确关键页时): "
434
+ + ", ".join(str(page) for page in suggested)
435
+ )
436
+
437
+ texts: List[Tuple[int, str]] = []
438
+ for idx, slide in enumerate(slides_list, start=1):
439
+ page = int(slide.get("page") or idx)
440
+ value = str(slide.get("text") or slide.get("ocr_content") or "").strip()
441
+ if value:
442
+ texts.append((page, " ".join(value.split())))
443
+ if texts:
444
+ rendered = [f"{page}={value[:80]}" for page, value in texts[:20]]
445
+ lines.append("页面文字(接口返回时): " + " | ".join(rendered))
446
+ if len(texts) > 20:
447
+ lines.append(f"(另有 {len(texts) - 20} 页有文字,未展开)")
448
+
449
+ example = next(
450
+ (
451
+ slide for slide in slides_list
452
+ if str(slide.get("workspace_rel") or "").strip()
453
+ ),
454
+ None,
455
+ )
456
+ if example:
457
+ page = int(example.get("page") or 1)
458
+ relative = str(example.get("workspace_rel") or "").strip()
459
+ lines.append(
460
+ f"引用示例: ![第 {page} 页]({relative})"
461
+ "(讲义在子目录时按层级补 ../)"
462
+ )
463
+
464
+ return "\n".join(lines).rstrip() + "\n"
465
+
466
+
280
467
  def format_slides_as_markdown(
281
468
  slides_list: Optional[List[Dict[str, Any]]],
282
469
  pdf_path: str = "",
470
+ *,
471
+ compact: bool = False,
472
+ download_attempted: bool = False,
283
473
  ) -> str:
284
- """Format normalized slide records into a compact, truthful outline.
474
+ """Format normalized slide records into a truthful outline.
285
475
 
286
476
  ``search-ppt`` returns screenshots, not OCR text. A slide therefore shows
287
477
  whatever the API actually provided: text (rare), a remote image URL, and
288
478
  the local file when ``zju_fetch_slides(download=true)`` saved it.
479
+
480
+ ``compact=True`` is the model-facing index: one directory, the file-name
481
+ pattern, a page→time axis, and a small suggested sample. The legacy
482
+ per-page form is kept for the CLI and for callers that need every path.
289
483
  """
290
484
  if not slides_list:
291
485
  return ""
486
+ if compact:
487
+ return _format_compact_slides(
488
+ slides_list, pdf_path=pdf_path, download_attempted=download_attempted
489
+ )
292
490
 
293
491
  lines = ["### 课堂投影 PPT 截图"]
294
492
  if pdf_path:
@@ -296,8 +494,8 @@ def format_slides_as_markdown(
296
494
  lines.append("")
297
495
  for idx, slide in enumerate(slides_list, start=1):
298
496
  page = slide.get("page") or idx
299
- time_point = slide.get("time") or slide.get("time_point") or slide.get("sec")
300
- ts_str = f" [{format_timestamp(int(time_point))}]" if time_point is not None else ""
497
+ time_point = _slide_seconds(slide)
498
+ ts_str = f" [{format_timestamp(time_point)}]" if time_point is not None else ""
301
499
  title = slide.get("title") or f"第 {page} 页"
302
500
  lines.append(f"#### {title}{ts_str}")
303
501
 
@@ -9,8 +9,9 @@ inflated (FlateDecode), and no image is re-encoded.
9
9
 
10
10
  Security boundaries:
11
11
 
12
- * only ``https://*.zju.edu.cn`` image URLs are fetched;
13
- * plain ``http`` is upgraded to HTTPS for ZJU hosts and refused otherwise;
12
+ * image URLs must be HTTPS on ``*.zju.edu.cn`` or the official image CDN
13
+ allowlist (``chinamcloud`` / ``chinamcache``); everything else is refused;
14
+ * plain ``http`` is upgraded to HTTPS for those hosts;
14
15
  * the login Cookie is only sent to ZJU hosts (never to arbitrary URLs the
15
16
  API may return), and ``Authorization`` is never sent to image hosts;
16
17
  * image bytes are validated by magic number before anything is written.
@@ -30,8 +31,9 @@ from .client import (
30
31
  USER_AGENT,
31
32
  _fetch_bytes,
32
33
  extract_classroom_token,
34
+ is_allowed_image_host,
33
35
  is_zju_host,
34
- secure_zju_url,
36
+ secure_image_url,
35
37
  )
36
38
  from .paths import primary_workspace
37
39
  from .security import sanitize_cookie_header
@@ -52,14 +54,14 @@ def _record(diagnostics: Optional[List[str]], message: str) -> None:
52
54
 
53
55
 
54
56
  def _safe_image_url(url: str) -> Optional[str]:
55
- """Return the HTTPS ZJU URL to fetch, or ``None`` when it is unsafe."""
56
- value = secure_zju_url(url)
57
+ """Return the HTTPS ZJU/official-CDN URL to fetch, or ``None`` if unsafe."""
58
+ value = secure_image_url(url)
57
59
  parsed = urllib.parse.urlparse(value)
58
60
  if parsed.scheme != "https":
59
61
  return None
60
62
  if parsed.username or parsed.password:
61
63
  return None
62
- if not is_zju_host(parsed.hostname):
64
+ if not is_allowed_image_host(parsed.hostname):
63
65
  return None
64
66
  return value
65
67
 
@@ -87,7 +89,7 @@ def _download_one(
87
89
  return index, None, "该页没有截图地址"
88
90
  safe_url = _safe_image_url(url)
89
91
  if safe_url is None:
90
- return index, None, "截图地址不是 HTTPS 浙大主机,已拒绝下载"
92
+ return index, None, "截图地址不是 HTTPS 浙大主机或官方图片 CDN,已拒绝下载"
91
93
 
92
94
  host = (urllib.parse.urlparse(safe_url).hostname or "").lower()
93
95
  headers = {