SyncService keeps local decks.json and quiz_sessions.json files in sync with Google Drive AppData using a timestamp-based smart merge. It requires a valid GoogleAuthService instance with active credentials.
Constructor
SyncService(auth_service)
An authenticated
GoogleAuthService instance. The service uses it to obtain OAuth credentials and build the Drive API client on first use.Public methods
sync_decks() → Tuple[bool, str]
Synchronize the local decks.json with the copy stored in Google Drive AppData.
(True, "Đồng bộ Decks thành công") on success, or (False, error_message) on failure.Download remote file
Looks up
decks.json in the Drive AppData folder. Downloads and parses it if found.Merge by timestamp
For each deck that exists in both local and remote, compares the
updated_at field. The newer version wins. If timestamps are equal, the local copy is kept.Handle remote-only and local-only decks
Decks that exist only on Drive are pulled down locally. Decks that exist only locally are retained as-is.
Preserve local image paths
When a remote deck wins the merge, local
image_path values are re-injected into the remote deck’s cards by card_id. Drive does not store image files, only metadata.sync_quiz_sessions() → Tuple[bool, str]
Synchronize quiz_sessions.json with Google Drive AppData using the same timestamp-based merge strategy.
(True, "Đồng bộ Sessions thành công") on success, or (False, error_message) on failure.Unlike deck sync, quiz session merge does not need to preserve image paths. The newer
updated_at value for each deck_id key wins outright.perform_full_sync() → Tuple[bool, str]
Run sync_decks followed by sync_quiz_sessions in sequence. Returns immediately on the first failure.
(True, "Đồng bộ thành công") if both steps succeed. Returns the failure tuple from whichever step failed first.Error codes
| Condition | Returned message |
|---|---|
| No internet connection | "Không có kết nối mạng" |
| Drive API HTTP error | "Lỗi Google Drive: <status_code>" |
| Not authenticated | "Chưa đăng nhập Google Drive" |
| Other exception | "Lỗi: <exception_message>" |
8.8.8.8:53) with a 3-second timeout before any Drive API call is made.
Usage example
GeminiService
Extract flashcards from exam images using Gemini AI.
ExportService
Export decks to Quizlet-compatible text files.