1
- from pydrive .auth import GoogleAuth
2
- from pydrive .drive import GoogleDrive
3
- import httplib2
4
- import pickle
5
-
6
- gauth = GoogleAuth ()
7
- gauth .settings = {
8
- 'client_config_backend' : 'settings' ,
9
- 'client_config_file' :'file' ,
10
- 'save_credentials' : 'true' ,
11
- 'save_credentials_backend' : 'file' ,
12
- 'save_credentials_file' : 'Credentials.txt' ,
13
- 'get_refresh_token' : True ,
14
- 'oauth_scope' : ['https://www.googleapis.com/auth/drive' ],
15
- 'client_config' : {
16
- 'client_id' :pickle .load (open ('client_id' , 'rb' )),
17
- 'auth_uri' :'https://accounts.google.com/o/oauth2/auth' ,
18
- 'token_uri' :'https://accounts.google.com/o/oauth2/token' ,
19
- 'client_secret' :pickle .load (open ('client_secret' , 'rb' )),
20
- 'redirect_uri' : 'http://localhost' ,
21
- 'revoke_uri' : 'https://accounts.google.com/o/oauth2/revoke'
22
- }
23
- }
24
-
25
- gauth .LoadClientConfigSettings ()
26
- gauth .LoadCredentials ()
27
- gauth .LocalWebserverAuth ()
1
+ from __future__ import print_function
2
+
3
+ from pydrive .auth import GoogleAuth
4
+ from pydrive .drive import GoogleDrive
5
+ import httplib2
6
+ import zipfile
7
+ import sys
8
+ import os
9
+ import time
10
+ import hashlib
11
+
12
+ gauth = GoogleAuth ()
13
+ drive = GoogleDrive (gauth )
14
+ GDZipFile = drive .CreateFile ()
15
+ oldHash = None
16
+
17
+ def authenticate ():
18
+ gauth .settings = {
19
+ 'client_config_backend' : 'settings' ,
20
+ 'client_config_file' :'file' ,
21
+ 'save_credentials' : 'true' ,
22
+ 'save_credentials_backend' : 'file' ,
23
+ 'save_credentials_file' : 'Credentials.txt' ,
24
+ 'get_refresh_token' : True ,
25
+ 'oauth_scope' : ['https://www.googleapis.com/auth/drive' ],
26
+ 'client_config' : {
27
+ # 'client_id':pickle.load(open('client_id', 'rb'))
28
+ 'client_id' :'1069969218802-lms0do78nc6hsqkp88ltaesf9ve0dn1p.apps.googleusercontent.com' ,
29
+ 'auth_uri' :'https://accounts.google.com/o/oauth2/auth' ,
30
+ 'token_uri' :'https://accounts.google.com/o/oauth2/token' ,
31
+ # 'client_secret':pickle.load(open('client_secret', 'rb')),
32
+ 'client_secret' :'Nws7EkTv6SI5j2D8fJMNzRpp' ,
33
+ 'redirect_uri' : 'http://localhost' ,
34
+ 'revoke_uri' : 'https://accounts.google.com/o/oauth2/revoke'
35
+ }
36
+ }
37
+
38
+ gauth .LoadClientConfigSettings ()
39
+ # gauth.LoadCredentials()
40
+ gauth .http = httplib2 .Http (ca_certs = 'cacert.pem' )
41
+ gauth .LocalWebserverAuth ()
42
+
43
+ def zip_folder (folder_path , output_path ):
44
+ parent_folder = os .path .dirname (folder_path )
45
+ # Retrieve the paths of the folder contents.
46
+ contents = os .walk (folder_path )
47
+ try :
48
+ zip_file = zipfile .ZipFile (output_path , 'w' , zipfile .ZIP_DEFLATED )
49
+ for root , folders , files in contents :
50
+ # Include all subfolders, including empty ones.
51
+ for folder_name in folders :
52
+ absolute_path = os .path .join (root , folder_name )
53
+ relative_path = absolute_path .replace (parent_folder + '\\ ' ,'' )
54
+ # print "Adding '%s' to archive." % absolute_path
55
+ zip_file .write (absolute_path , relative_path )
56
+ for file_name in files :
57
+ absolute_path = os .path .join (root , file_name )
58
+ relative_path = absolute_path .replace (parent_folder + '\\ ' ,'' )
59
+ # print "Adding '%s' to archive." % absolute_path
60
+ zip_file .write (absolute_path , relative_path )
61
+ print ("Archive '{0}' created successfully." .format (output_path ))
62
+ except IOError , message :
63
+ print (message )
64
+ sys .exit (1 )
65
+ except OSError , message :
66
+ print (message )
67
+ sys .exit (1 )
68
+ except zipfile .BadZipfile , message :
69
+ print (message )
70
+ sys .exit (1 )
71
+ finally :
72
+ zip_file .close ()
73
+
74
+
75
+ def upload (info ):
76
+ fileName = info ['projectName' ] + ".zip"
77
+ zip_folder (info ['dirName' ] , info ['projectName' ] + ".zip" )
78
+ global GDZipFile
79
+ GDZipFile ['mimeType' ] = 'application/zip'
80
+ GDZipFile ['title' ] = info ['projectName' ] + time .strftime ("_%x_%X" ) + ".zip"
81
+ GDZipFile ['description' ] = info ['description' ]
82
+ GDZipFile .SetContentFile (info ['projectName' ] + ".zip" )
83
+
84
+ if changed (fileName ):
85
+ print ("Uploading..." )
86
+ GDZipFile .Upload ()
87
+ print ("File Uploaded\n " )
88
+ else :
89
+ return
90
+
91
+ def changed (fileName ):
92
+ zipFile = open (fileName , 'rb' )
93
+ buff = zipFile .read (128 )
94
+ sha = hashlib .sha256 ()
95
+ while len (buff ) > 0 :
96
+ sha .update (buff )
97
+ buff = zipFile .read (128 )
98
+ newHash = sha .hexdigest ()
99
+ # print newHash
100
+ # print oldHash
101
+ global oldHash
102
+ if newHash == str (oldHash ):
103
+ print ( "No changes made, not uploading\n " )
104
+ # print newHash
105
+ oldHash = newHash
106
+ return False
107
+ else :
108
+ oldHash = newHash
109
+ return True
110
+
111
+
112
+ # raw_input("File Uploaded, press any key to exit")
113
+
114
+ if __name__ == '__main__' :
115
+ print ( "Welcome to Auto Upload to Drive program!" )
116
+ print ("First we'd like to collect some information about the project" )
117
+
118
+ zip_info = {
119
+ 'projectName' : raw_input ("\t What is the name of the project?: " ),
120
+ 'dirName' : raw_input ("\t Where is the location?: " ).strip ("\" " ) ,
121
+ 'description' : raw_input ("\t Please enter something about your project, for searching in GDrive\n \t (What it does...)[Optional]: " ),
122
+ 'interval' : int (raw_input ("\t Please enter Upload interval, in seconds (choose reasonably)\n \t Eg:300: " ))
123
+ }
124
+ authenticate ()
125
+ try :
126
+ for i in range (1 ,100 * 60 * 35 ):
127
+ print ( "File #{}" .format (i ))
128
+ upload (zip_info )
129
+ print ( "Waiting for next turn." )
130
+ time .sleep (zip_info ['interval' ])
131
+ except KeyboardInterrupt , messgae :
132
+ print ("Exit requested, Exiting now" )
133
+ sys .exit (0 )
0 commit comments