Skip to content
This repository has been archived by the owner on Mar 20, 2019. It is now read-only.

Commit

Permalink
Use urlencode and start some error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
vrillusions committed Mar 5, 2011
1 parent 81bedf0 commit 94ba995
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions simplenote.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,25 @@ def __init__(self, email, password):
self.base_url = 'https://simple-note.appspot.com/api2/'
self.email = email
self.password = password
self.last_error = ''

def login(self):
# the login url is just api
url = 'https://simple-note.appspot.com/api/login'
data = 'email=' + self.email + '&password=' + self.password
data = base64.b64encode(data)
fh = urllib2.urlopen(url, data)
self.authtok = fh.read()
query = {'email': self.email, 'password': self.password}
data = base64.b64encode(urllib.urlencode(query))
try:
fh = urllib2.urlopen(url, data)
self.authtok = fh.read()
except urllib2.HTTPError, e:
# Received a non 2xx status code
self.last_error = 'http error: ' + str(e.code)
print e.readlines()
return None
except urllib2.URLError, e:
# Non http error, like network issue
self.last_error = 'url error:', e.reason
return None
fh.close()

def note(self):
Expand All @@ -49,12 +60,13 @@ def delete(self):

def index(self, length=100):
url = self.base_url + 'index'
data = 'length=' + str(length) + '&auth=' + self.authtok + '&email=' + self.email
query = {'length': length, 'auth': self.authtok, 'email': self.email}
data = urllib.urlencode(query)
# this is a get request so everything is sent in url
fh = urllib2.urlopen(url + '?' + data)
index = fh.read()
fh.close()
return(index)
return index


def main():
Expand All @@ -71,6 +83,9 @@ def main():

sn = Simplenote(email, password)
sn.login()
if sn.last_error != '':
print 'ERROR:', sn.last_error
sys.exit(1)
print sn.index(1)


Expand Down

0 comments on commit 94ba995

Please sign in to comment.