diff --git a/localcache2.py b/localcache2.py index d2c85a8..7f333c7 100644 --- a/localcache2.py +++ b/localcache2.py @@ -669,8 +669,79 @@ def insert_usefulinfo_record(parsed): date_label = parsed.get('date') short_text = parsed.get('short') or '' summary_text = parsed.get('summary') or '' - tags = parsed.get('tags') or [] - events = parsed.get('events') or [] + # Normalize tags into a flat list of strings + def _norm_tags(t): + if t is None: + return [] + if isinstance(t, list): + out = [] + for x in t: + if isinstance(x, (list, tuple)): + out.extend([str(y) for y in x if y is not None]) + elif isinstance(x, dict): + # accept {name: "..."} + name = x.get('name') if 'name' in x else None + if name: + out.append(str(name)) + elif x is not None: + out.append(str(x)) + return out + if isinstance(t, str): + # try json first + try: + j = json.loads(t) + return _norm_tags(j) + except Exception: + pass + # split by comma + return [s.strip() for s in t.split(',') if s.strip()] + return [str(t)] + + # Normalize events into a list of dicts with expected keys + def _norm_events(ev): + def _one(e): + if not isinstance(e, dict): + return None + dt = e.get('dt') or e.get('datetime') or e.get('date') or e.get('when') + length = e.get('length') or e.get('duration') or e.get('dur') + title = e.get('title') or e.get('name') or e.get('summary') + desc = e.get('description') or e.get('details') or e.get('note') or e.get('body') + return {'dt': dt, 'length': length, 'title': title, 'description': desc} + + if ev is None: + return [] + if isinstance(ev, list): + out = [] + for x in ev: + if isinstance(x, dict): + y = _one(x) + if y: + out.append(y) + elif isinstance(x, str): + try: + j = json.loads(x) + if isinstance(j, dict): + y = _one(j) + if y: + out.append(y) + elif isinstance(j, list): + out.extend(_norm_events(j)) + except Exception: + pass + return out + if isinstance(ev, dict): + y = _one(ev) + return [y] if y else [] + if isinstance(ev, str): + try: + j = json.loads(ev) + return _norm_events(j) + except Exception: + return [] + return [] + + tags = _norm_tags(parsed.get('tags')) + events = _norm_events(parsed.get('events')) s_hash = _sha256((source or '') + "\n" + (date_label or '') + "\n" + short_text + "\n" + summary_text)