-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
executable file
·112 lines (82 loc) · 3.77 KB
/
test.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
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
#!/usr/bin/python3
from smugmugv2py import Connection, User, Node, Album, AlbumImage, SmugMugv2Exception
from sys import stdout, stdin
from os import linesep, path
from pprint import pprint
from test_setup import api_key, api_secret, token, secret
from datetime import datetime
from json import dumps
from requests import exceptions
def do_indent(indent):
for x in range(0, indent):
stdout.write(" ")
def print_album(node, indent):
album = Album.get_album(connection, node.album_uri)
stdout.write(", " + str(album.image_count) + " images")
images = album.get_images(connection)
for image in images:
do_indent(indent + 1)
print(image.filename + " - " + image.caption)
def print_node(node, indent):
do_indent(indent)
stdout.write("'" + node.name + "' (" + node.type + ") - " + node.privacy)
if node.type == "Album":
print_album(node, indent)
print()
children = node.get_children(connection)
for child in children:
print_node(child, indent + 1)
if not api_key or not api_secret:
raise Exception('API key and secret are required. see test_setup.py')
connection = Connection(api_key, api_secret, user_agent="Test user agent/2.4")
if not token or not secret:
auth_url = connection.get_auth_url(access="Full", permissions="Modify")
print("Visit the following URL and retrieve a verification code:%s%s" % (linesep, auth_url))
stdout.write('Enter the six-digit code: ')
stdout.flush()
verifier = stdin.readline().strip()
at, ats = connection.get_access_token(verifier)
print("Token: " + at)
print("Secret: " + ats)
token = at
secret = ats
connection.authorise_connection(token, secret)
try:
user = User.get_authorized_user(connection)
print("User: " + user.nickname + " (" + user.name + ")")
node = Node.get_node(connection, user.node)
# print_node(node, 0)
# new_node=Node(node.create_child_folder(connection, 'aaaatestnode2','aaaaatestnode2','Public'))
# print new_node.uri + " - " + new_node.name
new_node = None
for this_node in node.get_children(connection):
if this_node.url_name == "Testalbum":
new_node = this_node
if new_node is None:
# creating the child folder privately so people can run this test script 'safely'.
new_node = node.create_child_album(connection, 'testalbum', 'Testalbum', 'Private',
'A long description for the album')
print(new_node.uri + " - " + new_node.name + new_node.album_uri)
album = Album.get_album(connection, new_node.album_uri)
try:
pprint(connection.upload_image('adhawkins_github_avatar.jpg',
album.uri))
except exceptions.ConnectionError as e:
print("ConnectionError: " + str(e))
print("Deleting node after failed upload")
new_node.delete_node(connection)
# delete_node=Node(node.create_child_folder(connection, 'deletetest','Deletetest','Public'))
# print "Name: " + delete_node.name + ", url: " + delete_node.url_name
# pprint(delete_node.delete_node(connection))
# rename_node=Node(node.create_child_folder(connection, 'renametest','Renametest','Public'))
# print "Name: " + rename_node.name + ", url: " + rename_node.url_name
# rename = {
# "UrlName": "Renametestchange"
# }
# renamed_node=Node(rename_node.change_node(connection, rename))
# print "Renamed Name: " + renamed_node.name + ", url: " + renamed_node.url_name
# found_renamed_node=Node(SmugMugv2Utils.get_node(connection, rename_node.uri))
# print "Found Renamed Name: " + found_renamed_node.name + ", url: " + found_renamed_node.url_name
# pprint(connection.delete(renamed_node.uri))
except SmugMugv2Exception as e:
print("Error: " + str(e))