format-history.sh 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/bin/bash
  2. if [ $# -eq 0 ]; then
  3. echo "Usage: $0 <export-file.json> [tag1] [tag2] ..." >&2
  4. exit 1
  5. fi
  6. export_file="$1"
  7. shift
  8. tags=("$@")
  9. if [ ! -f "$export_file" ]; then
  10. echo "Error: File '$export_file' not found" >&2
  11. exit 1
  12. fi
  13. if [ ${#tags[@]} -eq 0 ]; then
  14. jq -r '
  15. .playbackHistory | to_entries |
  16. map({
  17. url: .value.url,
  18. title: .value.title,
  19. lastInteraction: ([.value.history[].timestamp] | max)
  20. }) |
  21. sort_by(.lastInteraction) | reverse |
  22. .[] |
  23. "\((.lastInteraction / 1000 | strftime("%Y-%m-%d %H:%M:%S")))\t\(.url)\t\(.title)"
  24. ' "$export_file"
  25. else
  26. # Build jq filter for tag matching
  27. tag_filters=""
  28. for tag in "${tags[@]}"; do
  29. if [ -n "$tag_filters" ]; then
  30. tag_filters="$tag_filters and "
  31. fi
  32. tag_filters="${tag_filters}(.value.tags // [] | contains([\"$tag\"]))"
  33. done
  34. jq -r "
  35. .playbackHistory | to_entries |
  36. map(select($tag_filters)) |
  37. map({
  38. url: .value.url,
  39. title: .value.title,
  40. lastInteraction: ([.value.history[].timestamp] | max)
  41. }) |
  42. sort_by(.lastInteraction) | reverse |
  43. .[] |
  44. \"\((.lastInteraction / 1000 | strftime(\"%Y-%m-%d %H:%M:%S\")))\\t\(.url)\\t\(.title)\"
  45. " "$export_file"
  46. fi