Skip to content

Commit 427800d

Browse files
committed
Tidied up the user stuff. Added docstrings and documentation
1 parent 0676c6a commit 427800d

File tree

5 files changed

+188
-2
lines changed

5 files changed

+188
-2
lines changed

README

Whitespace-only changes.

README.rst

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
Introduction
2+
=====================
3+
4+
pyzootool wraps the API of http://www.zootool.com. It's currently still very early in development, and not quite everything works.
5+
6+
What Works
7+
=====================
8+
9+
- Grabbing items by uid
10+
- Grabbing items by popularity
11+
- Grabbing items by username
12+
- Getting information on a user
13+
- Getting list of followers that a user has
14+
- Getting list of friends that a user has
15+
16+
What Doesn't Work
17+
=====================
18+
19+
- Authentication and anything that needs it
20+
- Adding items to zootool.com
21+
22+
Examples
23+
=====================
24+
25+
Here's a few examples of what you can do with this tool::
26+
27+
from pyzootool import controller
28+
zoocontrol = controller.ZooControl(apikey=YOUR_API_KEY)
29+
30+
## User information
31+
followers = zoocontrol.user.get_user_followers('username')
32+
friends = zoocontrol.user.get_user_friends('username')
33+
userinfo = zoocontrol.user.get_userinfo('username')
34+
35+
## Items
36+
popular_items = zoocontrol.item.get_popular('week')
37+
user_items = zoocontrol.item.get_items_by_user('username')
38+
item = zoocontrol.item.get_item('uid')

pyzootool/controller.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ def __init__(self, apikey, username=None, password=None):
1313
## TODO: Implement this
1414
self.auth = auth.ZooAuth(self.api_key, username, password)
1515
self.item = items.ZooItem(self.apikey, self.http)
16-
#self.user = users.ZooUser(self.apikey, self.http)
16+
self.user = users.ZooUser(self.apikey, self.http)

pyzootool/items.py

+53-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@
66
class ZooItemResult():
77

88
def __init__(self, json_data):
9+
"""
10+
Takes a SINGLE json object. Do *not* pass arrays of json data in.
11+
"""
912
self.parse_results(json_data)
1013

1114
def parse_results(self, json_data):
15+
"""
16+
Maps out the json data fields to variables
17+
"""
1218
self.uid = json_data['uid']
1319
self.title = json_data['title']
1420
self.url = json_data['url']
@@ -22,11 +28,25 @@ def parse_results(self, json_data):
2228
class ZooItem():
2329

2430
def __init__(self, apikey, http):
31+
"""
32+
Arguments:
33+
apikey - ZooTool apikey
34+
http - httplib2 http connection
35+
"""
2536
self.apikey = apikey
2637
self.http = http
2738

28-
2939
def get_item(self, item_id):
40+
"""
41+
Argument:
42+
item_id - unique identifier of item
43+
44+
Returns:
45+
result - ZooItemResult
46+
47+
Takes in a uid, calls to the ZooTool api to get the JSON objects.
48+
Returns a parsed json result in the form of a ZooItemResult object.
49+
"""
3050
values = {'uid': item_id, 'apikey': self.apikey}
3151
url = "%s/api/items/info/?%s" % (
3252
ROOT_URL, urllib.urlencode(values)
@@ -37,13 +57,45 @@ def get_item(self, item_id):
3757
return result
3858

3959
def get_popular(self, pop_type):
60+
"""
61+
Argument:
62+
pop_type - Valid options are 'week', 'year', 'all', 'today'
63+
64+
Returns:
65+
zoo_results - ZooItemResult
66+
67+
Takes in the popular sorting option. Returns an array of ZooItemResults
68+
"""
4069
values = {'type': pop_type, 'apikey': self.apikey }
4170
url = "%s/api/items/popular/?%s" % (
4271
ROOT_URL, urllib.urlencode(values)
4372
)
4473
resp, content = self.http.request(url)
4574
json_data = json.loads(content)
4675
zoo_results = []
76+
for item in json_data:
77+
result = ZooItemResult(item)
78+
zoo_results.append(result)
79+
return zoo_results
80+
81+
def get_items_by_user(self, username):
82+
"""
83+
Arguement:
84+
username - user we should grab items from
85+
86+
Returns:
87+
zoo_results - Array of ZooItemResults
88+
89+
Takes in a username, calls ZooTool API and receives JSON data. Kicks
90+
back an array of ZooItemResults
91+
"""
92+
values = {'username': username, 'apikey': self.apikey}
93+
url = "%s/api/users/items/?%s" % (
94+
ROOT_URL, urllib.urlencode(values)
95+
)
96+
resp, content = self.http.request(url)
97+
json_data = json.loads(content)
98+
zoo_results = []
4799
for item in json_data:
48100
result = ZooItemResult(item)
49101
zoo_results.append(result)

pyzootool/users.py

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import json
2+
import urllib
3+
4+
from pyzootool import ROOT_URL
5+
6+
class ZooUserResult():
7+
8+
def __init__(self, json_data):
9+
"""
10+
Takes a SINGLE json data object. Do not pass an array in
11+
"""
12+
self.parse_results(json_data)
13+
14+
def parse_results(self, json_data):
15+
"""
16+
Maps out the json data fields to variables
17+
"""
18+
print json_data
19+
self.username = json_data['username']
20+
self.website = json_data['website']
21+
self.avatar = json_data['avatar']
22+
self.profile = json_data['profile']
23+
24+
class ZooUser():
25+
"""
26+
TODO:
27+
This whole class
28+
"""
29+
30+
def __init__(self, apikey, http):
31+
"""
32+
Arguments:
33+
apikey - ZooTool apikey
34+
http - httplib2 http connection
35+
"""
36+
self.apikey = apikey
37+
self.http = http
38+
39+
def get_userinfo(self, username):
40+
"""
41+
Argument:
42+
username - name of user you wish to get info on
43+
44+
Returns:
45+
result - ZooUserResult
46+
47+
Takes in a username, calls ZooTool API and gets json data
48+
"""
49+
values = {'username': username, 'apikey': self.apikey}
50+
url = "%s/api/users/info/?%s" % (ROOT_URL, urllib.urlencode(values))
51+
resp, content = self.http.request(url)
52+
json_data = json.loads(content)
53+
result = ZooUserResult(json_data)
54+
55+
def get_user_friends(self, username):
56+
"""
57+
Argument:
58+
username - name of user you wish to get info on
59+
60+
Returns:
61+
zoo_results - Array of ZooUserResult
62+
63+
Takes in a username, calls ZooTool API and gets json data
64+
"""
65+
values = {'username': username, 'apikey': self.apikey}
66+
url = "%s/api/users/friends/?%s" % (ROOT_URL, urllib.urlencode(values))
67+
resp, content = self.http.request(url)
68+
json_data = json.loads(content)
69+
zoo_results = []
70+
for item in json_data:
71+
result = ZooUserResult(item)
72+
zoo_results.append(result)
73+
if zoo_results:
74+
return zoo_results
75+
else:
76+
raise AttributeError("No Results Found")
77+
78+
def get_user_followers(self, username):
79+
"""
80+
Argument:
81+
username - name of user you wish to get info on
82+
83+
Returns:
84+
zoo_results - Array of ZooUserResult
85+
86+
Takes in a username, calls ZooTool API and gets json data
87+
"""
88+
values = {'username': username, 'apikey': self.apikey}
89+
url = "%s/api/users/followers/?%s" % (ROOT_URL, urllib.urlencode(values))
90+
resp, content = self.http.request(url)
91+
json_data = json.loads(content)
92+
zoo_results = []
93+
for item in json_data:
94+
result = ZooUserResult(item)
95+
zoo_results.append(result)
96+
return zoo_results

0 commit comments

Comments
 (0)