|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +This is a Go application that synchronizes anime and manga lists between AniList and MyAnimeList accounts. The application uses OAuth2 authentication to access both services and supports bidirectional synchronization: |
| 8 | +- AniList to MyAnimeList (default) |
| 9 | +- MyAnimeList to AniList (with `-reverse-direction` flag) |
| 10 | + |
| 11 | +## Architecture |
| 12 | + |
| 13 | +The application follows a modular architecture with the following key components: |
| 14 | + |
| 15 | +- **Main entry point**: `main.go` - handles CLI flags and application initialization |
| 16 | +- **Configuration**: `config.go` - manages YAML configuration and environment variables |
| 17 | +- **Application core**: `app.go` - coordinates OAuth clients and sync operations |
| 18 | +- **API clients**: `anilist.go` and `myanimelist.go` - handle API interactions with respective services |
| 19 | +- **Media types**: `anime.go` and `manga.go` - define data structures and transformations |
| 20 | +- **Sync logic**: `updater.go` - handles the synchronization process between services |
| 21 | +- **OAuth handling**: `oauth.go` - manages OAuth2 authentication flows |
| 22 | +- **Statistics**: `statistics.go` - tracks sync operations and results |
| 23 | +- **Strategies**: `strategies.go` - implements different sync strategies and entry matching logic |
| 24 | + |
| 25 | +### Core Sync Pattern |
| 26 | + |
| 27 | +The synchronization follows a generic pattern defined in `updater.go`: |
| 28 | +- **Source/Target Interface**: Uses `Source` and `Target` interfaces for type-safe operations |
| 29 | +- **Updater struct**: Contains function pointers for getting targets by ID/name and updating them |
| 30 | +- **Comparison logic**: Implements progress comparison and diff generation between platforms |
| 31 | +- **Ignore list**: Supports title-based filtering for entries that don't exist on target platform |
| 32 | +- **Force sync**: Optional flag to bypass progress comparison and sync all entries |
| 33 | +- **Strategy chain**: Uses configurable strategies for finding and matching entries between platforms |
| 34 | + |
| 35 | +The `App` struct instantiates separate `Updater` instances for anime and manga in both directions (normal and reverse), each with their own function implementations for API operations. |
| 36 | + |
| 37 | +## Common Development Commands |
| 38 | + |
| 39 | +### Building |
| 40 | +```bash |
| 41 | +# Build the application |
| 42 | +make build |
| 43 | + |
| 44 | +# Build using go directly |
| 45 | +go build -o anilist-mal-sync |
| 46 | + |
| 47 | +# Build for Docker (used in multi-stage build) |
| 48 | +CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -mod=vendor -ldflags="-w -s" -o main |
| 49 | +``` |
| 50 | + |
| 51 | +Note: The Makefile builds from `./cmd/main.go` but the actual main file is in the root directory as `main.go`. |
| 52 | + |
| 53 | +### Testing |
| 54 | +```bash |
| 55 | +# Run all tests |
| 56 | +make test |
| 57 | + |
| 58 | +# Run tests with verbose output |
| 59 | +go test ./... -v |
| 60 | + |
| 61 | +# Test files: |
| 62 | +# - anime_test.go: Tests for score normalization/denormalization (anime) |
| 63 | +# - manga_test.go: Tests for score normalization/denormalization (manga) |
| 64 | +``` |
| 65 | + |
| 66 | +### Code Formatting |
| 67 | +```bash |
| 68 | +# Format code with gofumpt |
| 69 | +make fmt |
| 70 | + |
| 71 | +# This ensures code follows gofumpt standards required by CI |
| 72 | +``` |
| 73 | + |
| 74 | +### Linting |
| 75 | +```bash |
| 76 | +# Run linter using Docker (recommended) |
| 77 | +make lint |
| 78 | + |
| 79 | +# Run linter directly (if golangci-lint is installed locally) |
| 80 | +golangci-lint run --new |
| 81 | + |
| 82 | +# This uses golangci-lint v1.64.6 in Docker container |
| 83 | +# Note: dupl linter is disabled for test files to allow identical test structures |
| 84 | +``` |
| 85 | + |
| 86 | +### Running |
| 87 | +```bash |
| 88 | +# Run with default config |
| 89 | +go run . |
| 90 | + |
| 91 | +# Run with custom config file |
| 92 | +go run . -c myconfig.yaml |
| 93 | + |
| 94 | +# Run with dry run mode (no actual updates) |
| 95 | +go run . -d |
| 96 | + |
| 97 | +# Run with verbose logging |
| 98 | +go run . -verbose |
| 99 | + |
| 100 | +# Sync manga instead of anime |
| 101 | +go run . -manga |
| 102 | + |
| 103 | +# Sync both anime and manga |
| 104 | +go run . -all |
| 105 | + |
| 106 | +# Force sync all entries |
| 107 | +go run . -f |
| 108 | + |
| 109 | +# Reverse sync direction (MyAnimeList to AniList) |
| 110 | +go run . -reverse-direction |
| 111 | +``` |
| 112 | + |
| 113 | +### Cleanup |
| 114 | +```bash |
| 115 | +# Clean build artifacts and test cache |
| 116 | +make clean |
| 117 | +``` |
| 118 | + |
| 119 | +### Docker Development |
| 120 | +```bash |
| 121 | +# Build Docker image |
| 122 | +docker build -t anilist-mal-sync . |
| 123 | + |
| 124 | +# Run with pre-built image from GitHub Container Registry |
| 125 | +docker pull ghcr.io/bigspawn/anilist-mal-sync:latest |
| 126 | + |
| 127 | +# Run with Docker (requires config and token volumes) |
| 128 | +docker run -p 18080:18080 \ |
| 129 | + -v /path/to/config.yaml:/etc/anilist-mal-sync/config.yaml \ |
| 130 | + -v /path/to/tokens:/home/appuser/.config/anilist-mal-sync \ |
| 131 | + ghcr.io/bigspawn/anilist-mal-sync:latest |
| 132 | +``` |
| 133 | + |
| 134 | +## Configuration |
| 135 | + |
| 136 | +The application uses a YAML configuration file (`config.yaml`) with the following structure: |
| 137 | +- OAuth settings (port, redirect URI) |
| 138 | +- AniList client credentials and settings |
| 139 | +- MyAnimeList client credentials and settings |
| 140 | +- Token file path for persistent authentication |
| 141 | + |
| 142 | +Environment variables can override sensitive values: |
| 143 | +- `PORT` - OAuth server port |
| 144 | +- `CLIENT_SECRET_ANILIST` - AniList client secret |
| 145 | +- `CLIENT_SECRET_MYANIMELIST` - MyAnimeList client secret |
| 146 | + |
| 147 | +## Key Dependencies |
| 148 | + |
| 149 | +- `golang.org/x/oauth2` - OAuth2 client implementation |
| 150 | +- `gopkg.in/yaml.v2` - YAML configuration parsing |
| 151 | +- `github.com/nstratos/go-myanimelist` - MyAnimeList API client |
| 152 | +- `github.com/rl404/verniy` - AniList API client |
| 153 | +- `github.com/cenkalti/backoff/v4` - Exponential backoff for API retry logic |
| 154 | + |
| 155 | +### Important: Dependency Management |
| 156 | +**Always run `go mod vendor` after `go get` to keep the vendor directory in sync with go.mod changes.** |
| 157 | + |
| 158 | +## Authentication Flow |
| 159 | + |
| 160 | +The application implements OAuth2 authentication for both services: |
| 161 | +1. Starts a local server on port 18080 (configurable) |
| 162 | +2. Opens browser to service authorization URL |
| 163 | +3. Handles callback and exchanges code for access token |
| 164 | +4. Stores tokens in `~/.config/anilist-mal-sync/token.json` |
| 165 | + |
| 166 | +## Sync Process |
| 167 | + |
| 168 | +1. Fetches user's anime/manga list from AniList |
| 169 | +2. Fetches user's anime/manga list from MyAnimeList |
| 170 | +3. Normalizes AniList scores to 0-10 format (see Score Normalization below) |
| 171 | +4. Compares entries and identifies differences |
| 172 | +5. Updates MyAnimeList entries to match AniList status |
| 173 | +6. In reverse sync, denormalizes scores back to user's AniList format |
| 174 | +7. Provides statistics on sync operations |
| 175 | + |
| 176 | +### Score Normalization |
| 177 | + |
| 178 | +The application handles score format differences between AniList and MyAnimeList: |
| 179 | + |
| 180 | +**AniList Score Formats**: |
| 181 | +- `POINT_100` (0-100) - e.g., 85/100 |
| 182 | +- `POINT_10_DECIMAL` (0-10.0) - e.g., 8.5/10 |
| 183 | +- `POINT_10` (0-10) - e.g., 8/10 |
| 184 | +- `POINT_5` (0-5) - e.g., 4/5 |
| 185 | +- `POINT_3` (1-3) - e.g., 2/3 |
| 186 | + |
| 187 | +**MyAnimeList Score Format**: |
| 188 | +- Integer 0-10 only |
| 189 | + |
| 190 | +**Implementation**: |
| 191 | +- Scores are stored internally as `int` in normalized 0-10 format |
| 192 | +- When reading from AniList: scores are normalized to 0-10 |
| 193 | +- When writing to AniList: scores are denormalized back to user's format |
| 194 | +- When reading/writing MAL: no conversion needed (already 0-10) |
| 195 | +- Functions: `normalizeScoreForMAL()` and `denormalizeScoreForAniList()` in `anime.go` and `manga.go` |
| 196 | +- User's score format is retrieved once at startup via `GetUserScoreFormat()` in `anilist.go` |
| 197 | + |
| 198 | +This architecture prevents MAL API errors when AniList scores exceed 10 (e.g., `400 invalid score bad_request`) |
| 199 | + |
| 200 | +## Docker Support |
| 201 | + |
| 202 | +The application includes Docker support with: |
| 203 | +- Multi-stage build using Alpine base image |
| 204 | +- Non-root user execution (appuser with UID 10001) |
| 205 | +- Volume mounts for configuration and token storage |
| 206 | +- Port exposure for OAuth callback (18080) |
| 207 | +- Vendored dependencies for faster builds |
| 208 | + |
| 209 | +## Development Notes |
| 210 | + |
| 211 | +### Rate Limiting |
| 212 | +Both AniList and MyAnimeList have rate limits. The application may appear to freeze due to API timeouts. If this occurs, stop the application and wait before retrying. |
| 213 | + |
| 214 | +### Entry Matching |
| 215 | +The sync process uses two methods to match entries: |
| 216 | +1. **By ID**: When AniList entry has a MAL ID reference |
| 217 | +2. **By Title**: When no ID is available, searches MAL by title and matches by media type |
| 218 | + |
| 219 | +### Debug Output |
| 220 | +Use the `-verbose` flag to enable detailed logging of the sync process, including API calls and comparison results. |
| 221 | + |
| 222 | +### Ignore List |
| 223 | +Hard-coded ignore lists in `app.go` skip entries that don't exist on the target platform (e.g., "Scott Pilgrim Takes Off" is not available on MAL). |
0 commit comments