Compare commits
10 Commits
ad0a0d07b2
...
1e5e4f436d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1e5e4f436d | ||
|
|
f03e44fd3c | ||
|
|
1ed54d6c15 | ||
|
|
cb0c4a512f | ||
|
|
518aa74dc4 | ||
|
|
31fce03012 | ||
|
|
1a9d6b2835 | ||
|
|
fab600fa07 | ||
|
|
539e1a916b | ||
|
|
9580a94e25 |
3
.gitignore
vendored
3
.gitignore
vendored
@ -14,3 +14,6 @@ audit_*.md
|
||||
|
||||
# Claude Code
|
||||
.claude/
|
||||
|
||||
# Debug output
|
||||
debug/
|
||||
|
||||
@ -33,3 +33,39 @@ def _is_login_page(page) -> bool:
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
def _get_window_id(page) -> int | None:
|
||||
try:
|
||||
client = page.context.new_cdp_session(page)
|
||||
info = client.send("Target.getTargetInfo")
|
||||
tid = info["targetInfo"]["targetId"]
|
||||
result = client.send("Browser.getWindowForTarget", {"targetId": tid})
|
||||
client.detach()
|
||||
return result["windowId"]
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
def minimize_window(page) -> None:
|
||||
wid = _get_window_id(page)
|
||||
if wid is None:
|
||||
return
|
||||
try:
|
||||
client = page.context.new_cdp_session(page)
|
||||
client.send("Browser.setWindowBounds", {"windowId": wid, "bounds": {"windowState": "minimized"}})
|
||||
client.detach()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def restore_window(page) -> None:
|
||||
wid = _get_window_id(page)
|
||||
if wid is None:
|
||||
return
|
||||
try:
|
||||
client = page.context.new_cdp_session(page)
|
||||
client.send("Browser.setWindowBounds", {"windowId": wid, "bounds": {"windowState": "normal"}})
|
||||
client.detach()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
@ -9,7 +9,7 @@ from pathlib import Path
|
||||
|
||||
from playwright.sync_api import sync_playwright
|
||||
|
||||
from teampulse.auth import create_context, ensure_logged_in
|
||||
from teampulse.auth import create_context, ensure_logged_in, restore_window
|
||||
from teampulse.memo import generate_memo, save_memo
|
||||
from teampulse.models import AuditEntry
|
||||
from teampulse.monitor import Monitor
|
||||
@ -57,17 +57,24 @@ def main() -> None:
|
||||
),
|
||||
)
|
||||
parser.add_argument(
|
||||
"--history",
|
||||
"-a", "--all",
|
||||
action="store_true",
|
||||
help="Bestehende Nachrichten auswerten (Standard: nur neue Nachrichten ab Scriptstart)",
|
||||
help="Alle Nachrichten auswerten inkl. bestehende (Standard: nur neue ab Scriptstart)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--no-minimize",
|
||||
action="store_true",
|
||||
help="Browser-Fenster nicht minimieren während der Aufzeichnung",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
include_history: bool = args.history
|
||||
include_history: bool = args.all
|
||||
minimize: bool = not args.no_minimize
|
||||
|
||||
print("─" * 60)
|
||||
print(" TeamPulse — Teams Chat Auswertung")
|
||||
print("─" * 60)
|
||||
print(f" Modus: {'bestehende + neue' if include_history else 'nur neue'} Nachrichten")
|
||||
print(f" Modus: {'alle' if include_history else 'nur neue'} Nachrichten")
|
||||
print(f" Browser: {'sichtbar' if not minimize else 'minimiert während Aufzeichnung'}")
|
||||
print(" Trigger: !start Name | !stop")
|
||||
print(" Beenden: Ctrl+C")
|
||||
print("─" * 60)
|
||||
@ -109,22 +116,15 @@ def main() -> None:
|
||||
resolver = Resolver(cache_path=CACHE_PATH, page=page)
|
||||
|
||||
while True:
|
||||
window = monitor.run_once(skip_existing=not include_history)
|
||||
window = monitor.run_once(skip_existing=not include_history, minimize=minimize)
|
||||
|
||||
# Only resolve entries that appear in the final memo
|
||||
# (moderator and presenter are excluded — no need to look up their email)
|
||||
excluded = {window.moderator, window.presenter}
|
||||
to_resolve = [e for e in window.entries if e.display_name not in excluded]
|
||||
|
||||
if to_resolve:
|
||||
import sys as _sys
|
||||
for i in range(3, 0, -1):
|
||||
_sys.stdout.write(f"\r Maus aus Browser-Fenster bewegen! Starte in {i}s...")
|
||||
_sys.stdout.flush()
|
||||
import time as _time; _time.sleep(1)
|
||||
_sys.stdout.write("\r\033[K")
|
||||
# Move Playwright mouse to top-left corner before hovering
|
||||
page.mouse.move(0, 0)
|
||||
if to_resolve and minimize:
|
||||
restore_window(page)
|
||||
|
||||
print(f"Löse {len(to_resolve)} E-Mail-Adresse(n) auf...")
|
||||
resolved_entries = []
|
||||
|
||||
@ -4,6 +4,7 @@ from datetime import datetime
|
||||
|
||||
from playwright.sync_api import Page
|
||||
|
||||
from teampulse.auth import minimize_window
|
||||
from teampulse.models import AuditEntry, AuditWindow, ChatMessage
|
||||
|
||||
_MSG_SELECTOR = "[data-tid='channel-pane-message'], [data-tid='chat-pane-message']"
|
||||
@ -151,7 +152,7 @@ class Monitor:
|
||||
))
|
||||
return new_messages
|
||||
|
||||
def run_once(self, skip_existing: bool = True) -> AuditWindow:
|
||||
def run_once(self, skip_existing: bool = True, minimize: bool = True) -> AuditWindow:
|
||||
import time
|
||||
|
||||
self.wait_for_chat()
|
||||
@ -230,6 +231,8 @@ class Monitor:
|
||||
start_time = datetime.now()
|
||||
collected = []
|
||||
print(f"▶ Zeitfenster gestartet — Vortragender: {presenter}")
|
||||
if minimize:
|
||||
minimize_window(self._page)
|
||||
|
||||
elif action == "stop":
|
||||
if start_time is None:
|
||||
|
||||
@ -4,17 +4,6 @@ from pathlib import Path
|
||||
|
||||
from playwright.sync_api import Page
|
||||
|
||||
from teampulse.monitor import _PROFILE_EMAIL_SELECTOR
|
||||
|
||||
# Selectors for the clickable sender element (avatar or name) that opens the profile card
|
||||
_AUTHOR_SELECTORS = [
|
||||
# Channel meeting: avatar next to reply-message-header name
|
||||
"[data-tid='reply-message-header-avatar']",
|
||||
"[data-tid='post-message-header-avatar']",
|
||||
# Meeting chat: author name element
|
||||
"[data-tid='message-author-name']",
|
||||
]
|
||||
|
||||
|
||||
class Resolver:
|
||||
def __init__(self, cache_path: Path, page: Page):
|
||||
@ -45,153 +34,132 @@ class Resolver:
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
def _hover_sender(self, display_name: str) -> tuple[str, dict]:
|
||||
"""Find the sender element and hover to open the profile card.
|
||||
Returns (strategy, bbox) or ('', {}) if not found."""
|
||||
def _click_avatar(self, display_name: str) -> str:
|
||||
"""Find the sender's avatar and click it to open the profile dialog.
|
||||
Returns strategy name or '' if not found."""
|
||||
|
||||
def _mouse_move_to(el) -> dict | None:
|
||||
"""Scroll element into view, get its bbox, move mouse there."""
|
||||
el.scroll_into_view_if_needed()
|
||||
bbox = el.bounding_box()
|
||||
if not bbox or bbox['width'] == 0:
|
||||
return None
|
||||
x = bbox['x'] + bbox['width'] / 2
|
||||
y = bbox['y'] + bbox['height'] / 2
|
||||
self._page.mouse.move(x, y)
|
||||
return bbox
|
||||
def _click_el(el) -> bool:
|
||||
try:
|
||||
el.scroll_into_view_if_needed()
|
||||
bbox = el.bounding_box()
|
||||
if not bbox or bbox['width'] == 0:
|
||||
return False
|
||||
el.click()
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
# Strategy 1: reply-message-header (channel meetings)
|
||||
# Strategy 1: reply-message-header (channel meetings) — click avatar
|
||||
for header in self._page.query_selector_all("[data-tid='reply-message-header']"):
|
||||
try:
|
||||
if display_name not in header.inner_text():
|
||||
continue
|
||||
avatar = header.query_selector("[data-tid='reply-message-header-avatar']")
|
||||
if avatar:
|
||||
bbox = _mouse_move_to(avatar)
|
||||
if bbox:
|
||||
return "reply-avatar", bbox
|
||||
if avatar and _click_el(avatar):
|
||||
return "reply-avatar"
|
||||
for span in header.query_selector_all("span"):
|
||||
if span.inner_text().strip() == display_name:
|
||||
bbox = _mouse_move_to(span)
|
||||
if bbox:
|
||||
return "reply-span", bbox
|
||||
if span.inner_text().strip() == display_name and _click_el(span):
|
||||
return "reply-span"
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
# Strategy 2: aria-label
|
||||
safe_name = display_name.replace("'", "\\'")
|
||||
for el in self._page.query_selector_all(f"[aria-label*='{safe_name}']"):
|
||||
try:
|
||||
if el.is_visible():
|
||||
bbox = _mouse_move_to(el)
|
||||
if bbox:
|
||||
return "aria-label", bbox
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
# Strategy 3: message-author-name — also try avatar to the left
|
||||
# Strategy 2: message-author-name — find avatar in parent tree and click
|
||||
for el in self._page.query_selector_all("[data-tid='message-author-name']"):
|
||||
try:
|
||||
if el.inner_text().strip() != display_name:
|
||||
continue
|
||||
# Look for avatar in parent tree (usually 3-4 levels up)
|
||||
avatar_bbox = self._page.evaluate("""(nameEl) => {
|
||||
# Find avatar div in parent tree via JS and click it
|
||||
clicked = self._page.evaluate("""(nameEl) => {
|
||||
let p = nameEl.parentElement;
|
||||
for (let i = 0; i < 6; i++) {
|
||||
if (!p) break;
|
||||
const av = p.querySelector('[data-tid*="avatar"], [class*="avatar"] img, [class*="Avatar"] img');
|
||||
const av = p.querySelector('[class*="ChatMessage__avatar"], [class*="avatar"]');
|
||||
if (av) {
|
||||
const r = av.getBoundingClientRect();
|
||||
if (r.width > 0) return {x: r.x + r.width/2, y: r.y + r.height/2, w: r.width, h: r.height};
|
||||
const inner = av.querySelector('img, [role="img"], button') || av;
|
||||
const r = inner.getBoundingClientRect();
|
||||
if (r.width > 0) { inner.click(); return true; }
|
||||
}
|
||||
p = p.parentElement;
|
||||
}
|
||||
return null;
|
||||
return false;
|
||||
}""", el)
|
||||
if avatar_bbox:
|
||||
self._page.mouse.move(avatar_bbox['x'], avatar_bbox['y'])
|
||||
return "nearby-avatar", avatar_bbox
|
||||
bbox = _mouse_move_to(el)
|
||||
if bbox:
|
||||
return "author-name", bbox
|
||||
if clicked:
|
||||
return "nearby-avatar"
|
||||
# Fallback: click the name span
|
||||
if _click_el(el):
|
||||
return "author-name"
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
return "", {}
|
||||
return ""
|
||||
|
||||
def _extract_email_from_profile(self, display_name: str) -> str | None:
|
||||
try:
|
||||
strategy, bbox = self._hover_sender(display_name)
|
||||
strategy = self._click_avatar(display_name)
|
||||
except Exception as e:
|
||||
print(f" Hover auf '{display_name}' fehlgeschlagen: {e}")
|
||||
print(f" Klick auf '{display_name}' fehlgeschlagen: {e}")
|
||||
return None
|
||||
|
||||
if not strategy:
|
||||
print(f" '{display_name}' nicht im sichtbaren Chat gefunden.")
|
||||
return None
|
||||
print(f" '{display_name}' via '{strategy}' bei {bbox} — warte auf Karte...")
|
||||
|
||||
# Screenshot right after hover for debugging
|
||||
try:
|
||||
from pathlib import Path as _Path
|
||||
debug_dir = _Path("debug"); debug_dir.mkdir(exist_ok=True)
|
||||
safe = display_name.replace(" ", "_")
|
||||
self._page.screenshot(path=str(debug_dir / f"{safe}_after_hover.png"))
|
||||
except Exception:
|
||||
pass
|
||||
print(f" '{display_name}' via '{strategy}' geklickt — warte auf Dialog...")
|
||||
|
||||
time.sleep(0.5) # Give Teams time to register the hover
|
||||
# Do NOT poll — Playwright's JS polling blocks Teams' async dialog loading.
|
||||
time.sleep(3)
|
||||
|
||||
try:
|
||||
# Stage 1: wait for card container to appear
|
||||
self._page.wait_for_selector(".lpc_ip_root_class", timeout=5_000)
|
||||
except Exception:
|
||||
print(f" Hover-Card für '{display_name}' erscheint nicht.")
|
||||
self._page.mouse.move(0, 0)
|
||||
return None
|
||||
email = self._page.evaluate(r"""() => {
|
||||
const card = document.querySelector('.lpc_ip_root_class lpc-card');
|
||||
if (!card) return '__NO_CARD__';
|
||||
|
||||
try:
|
||||
# Move mouse onto the card itself to keep it open while it loads
|
||||
card_el = self._page.query_selector(".lpc_ip_root_class")
|
||||
if card_el:
|
||||
card_el.hover()
|
||||
// 1. mailto link (most reliable — recurses into nested shadow DOMs)
|
||||
function findMailto(root) {
|
||||
const link = root.querySelector('a[href*="mailto:"]');
|
||||
if (link) return link.href.replace('mailto:', '').trim();
|
||||
for (const el of root.querySelectorAll('*')) {
|
||||
if (el.shadowRoot) {
|
||||
const found = findMailto(el.shadowRoot);
|
||||
if (found) return found;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
const linked = findMailto(card);
|
||||
if (linked) return linked;
|
||||
|
||||
# Stage 2: card content is inside a Shadow DOM (<lpc-card shadowrootmode="open">)
|
||||
# Wait for the mailto link to appear inside the shadow root
|
||||
self._page.wait_for_function(
|
||||
"""() => {
|
||||
const lpcCard = document.querySelector('.lpc_ip_root_class lpc-card');
|
||||
if (!lpcCard || !lpcCard.shadowRoot) return false;
|
||||
return !!lpcCard.shadowRoot.querySelector('a[href*="mailto:"]');
|
||||
}""",
|
||||
timeout=10_000,
|
||||
)
|
||||
email = self._page.evaluate("""() => {
|
||||
const lpcCard = document.querySelector('.lpc_ip_root_class lpc-card');
|
||||
if (!lpcCard || !lpcCard.shadowRoot) return null;
|
||||
const link = lpcCard.shadowRoot.querySelector('a[href*="mailto:"]');
|
||||
return link ? link.href.replace('mailto:', '').trim() : null;
|
||||
// 2. Extract email from text after common label patterns
|
||||
// Teams dialog text is concatenated without spaces, e.g.:
|
||||
// "SieKontaktinformationenE-Mailanja@example.com"
|
||||
const text = card.textContent;
|
||||
for (const label of ['E-Mail', 'E-mail', 'Email', 'email', 'mail', 'Mail']) {
|
||||
const idx = text.indexOf(label);
|
||||
if (idx !== -1) {
|
||||
const after = text.substring(idx + label.length);
|
||||
const m = after.match(/^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}/);
|
||||
if (m) return m[0];
|
||||
}
|
||||
}
|
||||
|
||||
return '__NO_EMAIL__' + text.trim().substring(0, 200);
|
||||
}""")
|
||||
self._page.mouse.move(0, 0)
|
||||
|
||||
if not email or email == '__NO_CARD__':
|
||||
print(f" Kein Profil-Dialog erschienen.")
|
||||
self._page.keyboard.press("Escape")
|
||||
return None
|
||||
|
||||
if email.startswith('__NO_EMAIL__'):
|
||||
print(f" Dialog offen, E-Mail nicht gefunden. Inhalt: {email[12:150]!r}")
|
||||
self._page.keyboard.press("Escape")
|
||||
return None
|
||||
|
||||
self._page.keyboard.press("Escape")
|
||||
time.sleep(0.3)
|
||||
return email or None
|
||||
except Exception:
|
||||
# Screenshot + shadow root text for diagnosis
|
||||
try:
|
||||
from pathlib import Path as _Path
|
||||
debug_dir = _Path("debug"); debug_dir.mkdir(exist_ok=True)
|
||||
safe = display_name.replace(" ", "_")
|
||||
self._page.screenshot(path=str(debug_dir / f"{safe}_card_state.png"))
|
||||
except Exception:
|
||||
pass
|
||||
card_text = self._page.evaluate("""() => {
|
||||
const lpcCard = document.querySelector('.lpc_ip_root_class lpc-card');
|
||||
if (!lpcCard) return '(kein lpc-card Element)';
|
||||
if (!lpcCard.shadowRoot) return '(kein Shadow Root)';
|
||||
return lpcCard.shadowRoot.textContent.trim().substring(0, 300);
|
||||
}""") or "(leer)"
|
||||
print(f" Karte für '{display_name}' offen, E-Mail nicht gefunden.")
|
||||
print(f" Shadow-Root-Inhalt: {card_text!r}")
|
||||
print(f" Screenshots in debug/{safe}_*.png")
|
||||
self._page.mouse.move(0, 0)
|
||||
return email
|
||||
|
||||
except Exception as e:
|
||||
print(f" Fehler beim Lesen des Profil-Dialogs: {e}")
|
||||
self._page.keyboard.press("Escape")
|
||||
return None
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user