File size: 4,068 Bytes
fe2a0f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import os
import logging
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from googleapiclient.discovery import build
from datetime import datetime

# Setup logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s',
    handlers=[
        logging.StreamHandler(),
        logging.FileHandler(f'drive_structure_{datetime.now().strftime("%Y%m%d_%H%M%S")}.log')
    ]
)

def authenticate_google():
    """Authenticate with Google Drive"""
    # Update scopes to match what's being returned
    SCOPES = [
        'https://www.googleapis.com/auth/drive.readonly',
        'https://www.googleapis.com/auth/drive.file',
        'https://www.googleapis.com/auth/drive.install',
        'https://www.googleapis.com/auth/userinfo.email',
        'https://www.googleapis.com/auth/userinfo.profile',
        'https://www.googleapis.com/auth/gmail.readonly',
        'openid'
    ]
    
    creds = None
    
    # Delete existing token to force new authentication
    if os.path.exists('token.json'):
        os.remove('token.json')
    
    # Client configuration
    CLIENT_CONFIG = {
        "installed": {
            "client_id": "483287191355-udtleajik8ko1o2n03fqmimuu47n3hba.apps.googleusercontent.com",
            "client_secret": "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",
            "auth_uri": "https://accounts.google.com/o/oauth2/auth",
            "token_uri": "https://oauth2.googleapis.com/token",
            "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
            "redirect_uris": ["http://localhost:8080/"]
        }
    }

    try:
        flow = InstalledAppFlow.from_client_config(
            CLIENT_CONFIG, 
            SCOPES
        )
        creds = flow.run_local_server(port=8080)
        
        # Save new credentials
        with open('token.json', 'w') as token:
            token.write(creds.to_json())
        
        return creds
        
    except Exception as e:
        logging.error(f"Authentication error: {str(e)}")
        raise

def list_drive_contents():
    """List all folders and files in Google Drive"""
    try:
        # Authenticate and build service
        creds = authenticate_google()
        service = build('drive', 'v3', credentials=creds)
        
        def get_files_in_folder(folder_id='root', prefix=''):
            """Recursively list contents of folders"""
            query = f"'{folder_id}' in parents and trashed=false"
            
            try:
                results = service.files().list(
                    q=query,
                    pageSize=1000,
                    fields="nextPageToken, files(id, name, mimeType)"
                ).execute()
                
                items = results.get('files', [])
                
                for item in items:
                    if item['mimeType'] == 'application/vnd.google-apps.folder':
                        # It's a folder
                        logging.info(f"{prefix}πŸ“ {item['name']}/")
                        # Recursively list contents of this folder
                        get_files_in_folder(item['id'], prefix + "  ")
                    else:
                        # It's a file
                        if item['name'].endswith('.csv'):
                            logging.info(f"{prefix}πŸ“„ {item['name']} (CSV)")
                        else:
                            logging.info(f"{prefix}πŸ“„ {item['name']}")
                            
            except Exception as e:
                logging.error(f"Error listing contents of folder: {str(e)}")
        
        # Start listing from root
        logging.info("\nGoogle Drive Structure:")
        logging.info("======================")
        get_files_in_folder()
        logging.info("======================")
        
    except Exception as e:
        logging.error(f"Error accessing Drive: {str(e)}")

if __name__ == "__main__":
    list_drive_contents()