-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpush
executable file
·96 lines (81 loc) · 2.63 KB
/
gpush
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
#!/usr/bin/env python3.5
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
import googleapiclient
import argparse
import pyperclip
import pathlib
import sys
def parse_args():
parse = argparse.ArgumentParser(parents=[tools.argparser])
parse.add_argument('file', help='file to upload', type=str)
args = parse.parse_args()
return args
def setup_api(args):
SCOPES = 'https://www.googleapis.com/auth/drive'
store = file.Storage(
'{}/.gpush/credentials.json'.format(pathlib.Path.home()))
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets(
'{}/.gpush/client_secret.json'.format(pathlib.Path.home()), SCOPES)
creds = tools.run_flow(flow, store, args)
return build('drive', 'v3', http=creds.authorize(Http()))
def get_push_folder(service):
folder = service.files().list(
q="name='GPush'",
pageSize=100,
fields="files(id, mimeType)"
).execute()
for obj in folder.get('files'):
if obj.get('mimeType') == 'application/vnd.google-apps.folder':
return obj.get('id')
return service.files().create(
body={
'name': 'GPush',
'mimeType': 'application/vnd.google-apps.folder',
},
fields='id',
).execute().get('id')
def push_file(service, file, folder_id):
filepath = file
filename = filepath.split('/')[-1]
media = googleapiclient.http.MediaFileUpload(filepath)
file = service.files().create(
body={
'name': filename,
'originalFilename': filepath,
'parents': [folder_id],
},
media_body=media,
fields='id,webContentLink',
).execute()
batch = service.new_batch_http_request()
batch.add(service.permissions().create(
fileId=file.get('id'),
body={
'type': 'anyone',
'role': 'reader',
},
fields='',
))
batch.execute()
return file
def main():
pathlib.Path('{}/.gpush'.format(pathlib.Path.home())
).mkdir(parents=True, exist_ok=True)
args = parse_args()
try:
service = setup_api(args)
except Exception as e:
print(e, file=sys.stderr, end='\n\n')
print("Seems you did'nt configure the Google API. Please read the README.md file!", file=sys.stderr)
exit(1)
folder_id = get_push_folder(service)
file = push_file(service, args.file, folder_id)
print(file.get('webContentLink'))
pyperclip.copy(file.get('webContentLink'))
print('Link copied!')
if __name__ == '__main__':
main()