forked from parthanium/YoPy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yopy.py
93 lines (77 loc) · 2.95 KB
/
yopy.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
#!/usr/bin/env python
"""
A simple Yo! API for python.
As of now, you can:
1.Get the number of subscribers
2.Send a Yo! to all subscribers
3.Send a Yo! to a specific user
YoPy requires a Yo! API access token. You can get one by registering at http://dev.justyo.co/
Parth Dhar
2014
GAE integration by David H Goldberg 2014
"""
from google.appengine.api import urlfetch
import json
class Yo:
def __init__(self, token):
self.token = token
def number(self):
"""
Function to GET the the number of subscribers of the API user account.
Returns:
int: number of subscribers if request is successful.
Raises:
Exception: if request is unsuccessful.
"""
number_url = "https://api.justyo.co/subscribers_count/?api_token=" + self.token
number = urlfetch.fetch(number_url)
if number.status_code == 200:
inObj = json.loads(number.content)
return inObj['result']
else:
raise Exception(number.status_code)
def yo_all(self, link=''):
"""
Function to send a Yo to all subscribers of the API user account.
Args:
link (str, optional): link to send with Yo. Default is neglected.
Returns:
bool: True if request is successful.
Raises:
Exception: if request is unsuccessful.
"""
yoall_data = {"api_token": self.token, 'link':link}
yoall_url = "https://api.justyo.co/yoall/"
yoall = urlfetch.fetch(url=yoall_url,
payload=json.dumps(yoall_data),
method=urlfetch.POST,
headers={'content-type': 'application/json'})
if yoall.status_code == 201: # not sure why 201 and not 200...
return True
else:
raise Exception(yoall.status_code)
def yo_user(self, username, **kwargs):
"""
Function to send a Yo to a specific username.
Args:
username (str): the Yo usernname.
**kwargs: keyword should be either ''link'' or ''location''.
Location should be of the form ''lat,lon'' where lat and lon are decimal degrees.
Returns:
bool: True if request is successful.
Raises:
Exception: if request is unsuccessful.
"""
username = username.upper()
youser_data = {"api_token": self.token, "username": username}
for kw in kwargs:
youser_data.update( { kw:kwargs[kw] } )
youser_url = "https://api.justyo.co/yo/"
youser = urlfetch.fetch(url=youser_url,
payload=json.dumps(youser_data),
method=urlfetch.POST,
headers={'content-type': 'application/json'})
if youser.status_code == 200:
return True
else:
raise Exception(youser.status_code)