Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Optional token initialization added to Pinboard constructor.

  • Loading branch information...
commit d44ee299f2dd305d79322bb95ccf99c132fb8a58 1 parent 0f1226b
@dreikanter dreikanter authored
Showing with 20 additions and 17 deletions.
  1. +20 −17 pinboard.py
View
37 pinboard.py
@@ -131,24 +131,31 @@ class PinboardAccount(UserDict):
# Time of last request so that the one second limit can be enforced.
__lastrequest = None
+ # Pinboard API token
+ # (see http://blog.pinboard.in/2012/07/api_authentication_tokens/)
+ __token = None
+
# Special methods
- def __init__(self, username, password):
+ def __init__(self, username=None, password=None, token=None):
UserDict.__init__(self)
# Authenticate the URL opener so that it can access Pinboard
if _debug:
sys.stderr.write("Initialising Pinboard Account object.\n")
- auth_handler = urllib2.HTTPBasicAuthHandler()
- auth_handler.add_password("API", "https://api.pinboard.in/", \
- username, password)
- opener = urllib2.build_opener(auth_handler)
+
+ if token:
+ self.__token = token
+ opener = urllib2.build_opener()
+ else:
+ auth_handler = urllib2.HTTPBasicAuthHandler()
+ auth_handler.add_password("API", "https://api.pinboard.in/", \
+ username, password)
+ opener = urllib2.build_opener(auth_handler)
+
opener.addheaders = [("User-agent", USER_AGENT), ('Accept-encoding', 'gzip')]
urllib2.install_opener(opener)
if _debug:
sys.stderr.write("URL opener with HTTP authenticiation installed globally.\n")
-
-
- if _debug:
sys.stderr.write("Time of last update loaded into class dictionary.\n")
def __getitem__(self, key):
@@ -186,7 +193,7 @@ def __request(self, url):
self.__lastrequest = time.time()
if _debug:
sys.stderr.write("Opening %s.\n" % url)
-
+
try:
## for pinboard a gzip request is made
raw_xml = urllib2.urlopen(url)
@@ -195,10 +202,10 @@ def __request(self, url):
compressedstream = StringIO.StringIO(compresseddata)
gzipper = gzip.GzipFile(fileobj=compressedstream)
xml = gzipper.read()
-
+
except urllib2.URLError, e:
raise e
-
+
self["headers"] = {}
for header in raw_xml.headers.headers:
(name, value) = header.split(": ")
@@ -209,10 +216,7 @@ def __request(self, url):
if _debug:
sys.stderr.write("%s opened successfully.\n" % url)
return minidom.parseString(xml)
-
-
-
-
+
def posts(self, tag="", date="", todt="", fromdt="", count=0):
"""Return pinboard.in bookmarks as a list of dictionaries.
@@ -319,7 +323,7 @@ def suggest(self, url):
popular = [t.firstChild.data for t in tags.getElementsByTagName('popular')]
recommended = [t.firstChild.data for t in tags.getElementsByTagName('recommended')]
-
+
return {'popular': popular, 'recommended': recommended}
def tags(self):
@@ -397,7 +401,6 @@ def dates(self, tag=""):
self["dates"] = dates
return dates
-
# Methods to modify pinboard.in content
def add(self, url, description, extended="", tags=(), date="", toread="no", replace="no", shared="yes"):
Please sign in to comment.
Something went wrong with that request. Please try again.