Skip to content

Instantly share code, notes, and snippets.

@claytron
Last active September 10, 2024 13:56
Show Gist options
  • Save claytron/a651add2593705e23cd0018f8aa5a3d5 to your computer and use it in GitHub Desktop.
Save claytron/a651add2593705e23cd0018f8aa5a3d5 to your computer and use it in GitHub Desktop.
Trakt VIP backup sync to Letterboxd

Use the backup files from Trakt VIP settings to sync to Letterboxd.

Use the help to see what options are available.

$ ./letterboxd.sh -h
#!/usr/bin/env bash
set -euo pipefail
usage() {
cat << EOF
usage: $0 -b base_path
This script will convert the Trakt backup files into a CSV file that
can be imported into Letterboxd.
The default mode is to create an import file for the watched movies.
https://letterboxd.com/import/
You can alternatively create an import file for the watchlist by using
the -w option.
OPTIONS:
-b The base path to the Trakt backup files
-w Create an import file for the watchlist
-d Turn on debugging output
-h Show this message
EOF
}
# Initialize options
base_path=
watchlist=
while getopts "hdwb:" OPTION
do
case $OPTION in
b)
base_path=$OPTARG
;;
w)
watchlist=1
;;
d)
set -x
;;
h)
usage
exit 0
;;
?)
usage
exit 1
;;
esac
done
if [ -z "$base_path" ]; then
usage
echo
echo Error: base path is required
exit 1
fi
function letterboxd_import() {
read -d '' -r letterboxd_query <<'EOF' || true
# Combine the ratings and watched data by the Trakt ID
JOIN(
($ratings[] | INDEX(.movie.ids.trakt | tostring));
.[];
.movie.ids.trakt | tostring;
add
)
| JOIN(
($comments[] | INDEX(.movie.ids.trakt | tostring));
.;
.movie.ids.trakt | tostring;
add
)
# Remove the time information from the watched date
| .last_watched_at |= sub( "T.*"; "")
# Create our CSV output
| .last_watched_at as $WatchedDate | .movie.ids.imdb as $imdbID | .rating as $Rating10 | .movie.title as $Title | .movie.year as $Year | .comment.comment as $Review | .last_updated_at as $LastUpdated
| [$imdbID, $Title, $Year, $WatchedDate, $LastUpdated, $Rating10, $Review]
| @csv
EOF
# Create the CSV header
echo "imdbID,Title,Year,WatchedDate,LastUpdated,Rating10,Review" > letterboxd_import.csv
# Combine the ratings and watched movies JSON files and generate the CSV
jq -r --slurpfile ratings "${base_path}/ratings/ratings-movies.json" --slurpfile comments "${base_path}/comments/comments-movies.json" "$letterboxd_query" "${base_path}/watched/watched-movies.json" >> letterboxd_import.csv
echo "letterboxd_import.csv created"
}
function letterboxd_watchlist() {
read -d '' -r letterboxd_query <<'EOF' || true
.[]
| select(.type == "movie")
# Create our CSV output
| .movie.ids.imdb as $imdbID | .movie.title as $Title | .movie.year as $Year | .notes as $Review | .listed_at as $ListedAt
| [$imdbID, $Title, $Year, $Review, $ListedAt]
| @csv
EOF
# Create the CSV header
echo "imdbID,Title,Year,Review,ListedAt" > letterboxd_watchlist.csv
# Generate the CSV
jq -r "$letterboxd_query" "${base_path}/lists/watchlist.json" >> letterboxd_watchlist.csv
echo "letterboxd_watchlist.csv created"
}
if [ -n "$watchlist" ]; then
letterboxd_watchlist
else
letterboxd_import
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment