export_browser_history.sh 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env bash
  2. # Helper script to export browser history and bookmarks to a format ArchiveBox can ingest.
  3. # Usage:
  4. # curl -O 'https://raw.githubusercontent.com/ArchiveBox/ArchiveBox/dev/bin/export_browser_history.sh'
  5. # bash export_browser_history.sh --chrome
  6. # bash export_browser_history.sh --firefox
  7. # bash export_browser_history.sh --safari
  8. # ls
  9. # chrome_history.json
  10. # firefox_history.json
  11. # firefox_bookmarks.json
  12. # safari_history.json
  13. OUTPUT_DIR="$(pwd)"
  14. if [[ "$1" == "--chrome" ]]; then
  15. # Google Chrome / Chromium
  16. if [[ -e "$2" ]]; then
  17. cp "$2" "$OUTPUT_DIR/chrome_history.db.tmp"
  18. else
  19. default=$(ls ~/Library/Application\ Support/Google/Chrome/Default/History)
  20. echo "Defaulting to history db: $default"
  21. echo "Optionally specify the path to a different sqlite history database as the 2nd argument."
  22. cp "$default" "$OUTPUT_DIR/chrome_history.db.tmp"
  23. fi
  24. sqlite3 "$OUTPUT_DIR/chrome_history.db.tmp" "SELECT \"[\" || group_concat(json_object('timestamp', last_visit_time, 'description', title, 'href', url)) || \"]\" FROM urls;" > "$OUTPUT_DIR/chrome_history.json"
  25. jq < "$(dirname "${2:-$default}")"/Bookmarks '.roots.other.children[] | {href: .url, description: .name, timestamp: .date_added}' > "$OUTPUT_DIR/chrome_bookmarks.json"
  26. rm "$OUTPUT_DIR"/chrome_history.db.*
  27. echo "Chrome history exported to:"
  28. echo " $OUTPUT_DIR/chrome_history.json"
  29. fi
  30. if [[ "$1" == "--firefox" ]]; then
  31. # Firefox
  32. if [[ -e "$2" ]]; then
  33. cp "$2" "$OUTPUT_DIR/firefox_history.db.tmp"
  34. else
  35. default=$(ls ~/Library/Application\ Support/Firefox/Profiles/*.default/places.sqlite)
  36. echo "Defaulting to history db: $default"
  37. echo "Optionally specify the path to a different sqlite history database as the 2nd argument."
  38. cp "$default" "$OUTPUT_DIR/firefox_history.db.tmp"
  39. fi
  40. sqlite3 "$OUTPUT_DIR/firefox_history.db.tmp" "SELECT \"[\" || group_concat(json_object('timestamp', last_visit_date, 'description', title, 'href', url)) || \"]\" FROM moz_places;" > "$OUTPUT_DIR/firefox_history.json"
  41. sqlite3 "$OUTPUT_DIR/firefox_history.db.tmp" "
  42. with recursive tags AS (
  43. select id, title, '' AS tags
  44. FROM moz_bookmarks
  45. where parent == 0
  46. UNION ALL
  47. select c.id, p.title, c.title || ',' || tags AS tags
  48. from moz_bookmarks AS c
  49. JOIN tags AS p
  50. ON c.parent = p.id
  51. )
  52. SELECT '[' || group_concat(json_object('timestamp', b.dateAdded, 'description', b.title, 'href', f.url, 'tags', tags.tags)) || ']'
  53. FROM moz_bookmarks AS b
  54. JOIN moz_places AS f ON f.id = b.fk
  55. JOIN tags ON tags.id = b.parent
  56. WHERE f.url LIKE '%://%';" > "$OUTPUT_DIR/firefox_bookmarks.json"
  57. rm "$OUTPUT_DIR"/firefox_history.db.*
  58. echo "Firefox history exported to:"
  59. echo " $OUTPUT_DIR/firefox_history.json"
  60. echo " $OUTPUT_DIR/firefox_bookmarks.json"
  61. fi
  62. if [[ "$1" == "--safari" ]]; then
  63. # Safari
  64. if [[ -e "$2" ]]; then
  65. cp "$2" "$OUTPUT_DIR/safari_history.db.tmp"
  66. else
  67. default="~/Library/Safari/History.db"
  68. echo "Defaulting to history db: $default"
  69. echo "Optionally specify the path to a different sqlite history database as the 2nd argument."
  70. cp "$default" "$OUTPUT_DIR/safari_history.db.tmp"
  71. fi
  72. sqlite3 "$OUTPUT_DIR/safari_history.db.tmp" "select url from history_items" > "$OUTPUT_DIR/safari_history.json"
  73. rm "$OUTPUT_DIR"/safari_history.db.*
  74. echo "Safari history exported to:"
  75. echo " $OUTPUT_DIR/safari_history.json"
  76. fi