-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_gphotosuploader.py
executable file
·75 lines (58 loc) · 2.4 KB
/
main_gphotosuploader.py
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
#!/usr/bin/python
# coding=UTF8
import argparse
import os
import sys
from gphotosuploader import GoogleUploader
import utils
def main():
def scan(fup):
print "Scanning folder", options.folder, "..."
pc, npc, pcs, npcs = fup.scan_directory(options.folder)
print pc, "total pictures found. (" + utils.sizeof_fmt(pcs) + ")"
print npc, "other files found. (" + utils.sizeof_fmt(npcs) + ")"
parser = argparse.ArgumentParser(description='Upload to Google+ all JPEG pictures in the given folder recursively')
parser.add_argument('folder', help='The folder to search for pictures')
parser.add_argument('-u', dest="user_name", help='Google user name', default="")
parser.add_argument('-s', dest='scan_only', action="store_true", help="Scan folder but don't upload pictures")
parser.add_argument('-r', dest="small_size", action="store_true", help='Reduce image size before upload.')
parser.add_argument('-a', dest='auth_only', action="store_true", help="Authenticate to Google service")
options = parser.parse_args()
if not options.folder:
parser.print_help()
exit()
if options.scan_only:
scan(GoogleUploader("", ""))
exit()
config_file_name = "~/.hmsoft/gphotos.json"
api_keys = utils.get_api_keys_from_config(config_file_name)
api_key, api_secret = api_keys
if api_key is None or api_secret is None:
sys.stderr.write("Please add Google API access keys to config file: " + config_file_name + "\n")
exit()
gup = GoogleUploader(api_key, api_secret, options.user_name)
if options.auth_only:
if gup.authenticate():
print "User " + str(gup.user_name) + " authenticated successfully"
else:
print "Authentication failed."
exit()
print "Authenticating..."
if not gup.authenticate():
sys.stderr.write("Google authentication error\n")
exit()
print "Starting upload as user " + str(gup.user_name)
options.folder = unicode(options.folder, "UTF-8")
gup.original_size = not options.small_size
if os.path.isfile(options.folder):
gup.upload_file(options.folder)
print "Done."
else:
scan(gup)
t = gup.upload_directory(options.folder)
print "Done in " + utils.format_time(t)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print "Terminated by user."