|
| 1 | +from google.auth.transport.requests import Request |
| 2 | +from google.oauth2.credentials import Credentials |
| 3 | +from google_auth_oauthlib.flow import InstalledAppFlow |
| 4 | +from googleapiclient.discovery import build |
| 5 | +from googleapiclient.errors import HttpError |
| 6 | +from time import sleep |
| 7 | +from tqdm import tqdm |
| 8 | + |
| 9 | +# Function to authorize and get credentials using OAuth2 flow |
| 10 | +def authorize_google_tasks(api_name, api_version, scopes, token_file): |
| 11 | + flow = InstalledAppFlow.from_client_secrets_file('credentials.json', scopes=scopes) |
| 12 | + creds = flow.run_local_server(port=0) |
| 13 | + |
| 14 | + # Save the credentials for the next run |
| 15 | + with open(token_file, 'w') as token: |
| 16 | + token.write(creds.to_json()) |
| 17 | + |
| 18 | + return creds |
| 19 | + |
| 20 | +# Function to get or create a task list |
| 21 | +def get_or_create_tasklist(service, tasklist_title): |
| 22 | + try: |
| 23 | + # Try to get the task list |
| 24 | + tasklist = service.tasklists().list().execute() |
| 25 | + tasklist_id = next((tl['id'] for tl in tasklist.get('items', []) if tl['title'] == tasklist_title), None) |
| 26 | + |
| 27 | + if tasklist_id: |
| 28 | + return tasklist_id |
| 29 | + except HttpError as e: |
| 30 | + if e.resp.status != 404: |
| 31 | + raise |
| 32 | + |
| 33 | + # If the task list doesn't exist, create it |
| 34 | + new_tasklist = service.tasklists().insert(body={'title': tasklist_title}).execute() |
| 35 | + return new_tasklist['id'] |
| 36 | + |
| 37 | +# Function to get tasks from a specific task list (including completed tasks) with pagination |
| 38 | +def get_tasks(service, tasklist_id, max_results=100): |
| 39 | + all_tasks = [] |
| 40 | + next_page_token = None |
| 41 | + |
| 42 | + while True: |
| 43 | + # Retrieve tasks for the current page |
| 44 | + page_tasks = service.tasks().list( |
| 45 | + tasklist=tasklist_id, |
| 46 | + showCompleted=True, |
| 47 | + showDeleted=True, |
| 48 | + showHidden=True, |
| 49 | + maxResults=max_results, |
| 50 | + pageToken=next_page_token |
| 51 | + ).execute() |
| 52 | + |
| 53 | + # Add tasks from the current page to the overall list |
| 54 | + all_tasks.extend(page_tasks.get('items', [])) |
| 55 | + |
| 56 | + # Check if there are more pages |
| 57 | + next_page_token = page_tasks.get('nextPageToken') |
| 58 | + if not next_page_token: |
| 59 | + break |
| 60 | + |
| 61 | + return all_tasks |
| 62 | + |
| 63 | +# Function to create tasks in a specific task list with retry |
| 64 | +def create_tasks_with_retry(service, tasklist_id, tasks, max_retries=3): |
| 65 | + for task in tqdm(tasks, desc=f"Importing tasks to {tasklist_id}"): |
| 66 | + for retry_count in range(max_retries): |
| 67 | + try: |
| 68 | + # Check for recurring tasks and handle them correctly |
| 69 | + if 'recurrence' in task: |
| 70 | + # Clear the 'completed' field for recurring tasks |
| 71 | + task.pop('completed', None) |
| 72 | + # Insert the recurring task without specifying a due date/time |
| 73 | + service.tasks().insert(tasklist=tasklist_id, body=task).execute() |
| 74 | + else: |
| 75 | + # Insert non-recurring tasks |
| 76 | + service.tasks().insert(tasklist=tasklist_id, body=task).execute() |
| 77 | + |
| 78 | + # Break out of the retry loop if successful |
| 79 | + break |
| 80 | + except HttpError as e: |
| 81 | + if e.resp.status == 403 and 'quotaExceeded' in str(e): |
| 82 | + # Quota exceeded, wait for some time and then retry |
| 83 | + print(f"Quota Exceeded. Retrying in 60 seconds (Attempt {retry_count + 1}/{max_retries})") |
| 84 | + sleep(60) |
| 85 | + else: |
| 86 | + # For other errors, raise the exception |
| 87 | + raise |
| 88 | + |
| 89 | +# Function to create tasks in a specific task list |
| 90 | +def create_tasks(service, tasklist_id, tasks): |
| 91 | + create_tasks_with_retry(service, tasklist_id, tasks) |
| 92 | + |
| 93 | +def main(): |
| 94 | + # Set the API information |
| 95 | + api_name = 'tasks' |
| 96 | + api_version = 'v1' |
| 97 | + scopes = ['https://www.googleapis.com/auth/tasks'] |
| 98 | + token_file_account1 = 'token_account1.json' # Replace with your token file for account 1 |
| 99 | + token_file_account2 = 'token_account2.json' # Replace with your token file for account 2 |
| 100 | + |
| 101 | + # Authorize and get credentials for the source Google Tasks account |
| 102 | + creds_account1 = authorize_google_tasks(api_name, api_version, scopes, token_file_account1) |
| 103 | + service_account1 = build(api_name, api_version, credentials=creds_account1) |
| 104 | + |
| 105 | + # Authorize and get credentials for the destination Google Tasks account |
| 106 | + creds_account2 = authorize_google_tasks(api_name, api_version, scopes, token_file_account2) |
| 107 | + service_account2 = build(api_name, api_version, credentials=creds_account2) |
| 108 | + |
| 109 | + # Get task lists from the source account |
| 110 | + tasklists_account1 = service_account1.tasklists().list().execute().get('items', []) |
| 111 | + print(f"Found {len(tasklists_account1)} task lists in the source account.") |
| 112 | + |
| 113 | + # Iterate through each task list and get tasks |
| 114 | + for tasklist_account1 in tasklists_account1: |
| 115 | + tasklist_title = tasklist_account1['title'] |
| 116 | + tasklist_id_account2 = get_or_create_tasklist(service_account2, tasklist_title) |
| 117 | + |
| 118 | + tasks_account1 = get_tasks(service_account1, tasklist_account1['id']) |
| 119 | + print(f"\nWorking on task list: {tasklist_title}") |
| 120 | + print(f"Found {len(tasks_account1)} tasks in the source account.") |
| 121 | + |
| 122 | + # Create tasks in the destination account |
| 123 | + create_tasks(service_account2, tasklist_id_account2, tasks_account1) |
| 124 | + |
| 125 | + print("\nTasks exported and imported successfully.") |
| 126 | + |
| 127 | +if __name__ == '__main__': |
| 128 | + main() |
0 commit comments