Skip to content

Commit

Permalink
Implementation of the retrieval of one seen (one message), by giving …
Browse files Browse the repository at this point in the history
…its message-ID
  • Loading branch information
bortzmeyer committed May 26, 2012
1 parent f18d6ea commit 678025d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
13 changes: 8 additions & 5 deletions README
Expand Up @@ -38,18 +38,21 @@ Gentoo: TODO
*** Usage ***

Examples of use are the scripts seenthis-backup.py (to backup all your
seens), seenthis-post.py (to post a message) and seenthis-test-url.py
(to test if a given URL is alreday in SeenThis).
seens as ATOM feeds), seenthis-post.py (to post a message),
seenthis-get-msg.py (to retrieve an ATOM representation of a seen by
its Message-ID) and seenthis-test-url.py (to test if a given URL is
alreday in SeenThis).

Both need a file storing SeenThis authentication info. The file must
be named $HOME/.seenthis/auth and contain two lines, one for the user
name and one for the password.
All these scripts need a file storing SeenThis authentication
info. The file must be named $HOME/.seenthis/auth and contain two
lines, one for the user name and one for the password.

*** API ***

The SeenThis API is documented (in French only) in
<http://seenthis.net/fran%C3%A7ais/mentions/article/api> but some
functions are documented elsewhere such as
<http://seenthis.net/messages/14646> or
<http://seenthis.net/messages/70484>.

*** Author ***
Expand Down
37 changes: 31 additions & 6 deletions SeenThis.py
@@ -1,8 +1,6 @@
"""
Documentation of the SeenThis API:
Documentation of the SeenThis API: see the README
http://seenthis.net/fran%C3%A7ais/mentions/article/api
http://seenthis.net/messages/14646
"""

import os
Expand All @@ -26,6 +24,7 @@
authfile = "%s/.seenthis/auth" % os.environ['HOME']
create_endpoint = 'https://seenthis.net/api/messages'
retrieve_endpoint_tmpl = 'https://seenthis.net/api/people/%s/messages'
get_endpoint_tmpl = 'https://seenthis.net/api/messages/%i'
url_endpoint_tmpl = 'https://seenthis.net/api/url/%s'
mytemplate = """
<entry xmlns='http://www.w3.org/2005/Atom'
Expand All @@ -41,6 +40,9 @@
class InternalError(Exception):
pass

class NotFound(Exception):
pass

class CredentialsNotFound(InternalError):
pass

Expand Down Expand Up @@ -72,8 +74,28 @@ def _add_headers(self, r, post=False):
def get_message(self, msgid):
""" Returns a FeedParserPlus object (which inherits from
traditional FeedparserDict) representing one SeenThis message. """
raise InternalError("TODO: not yet implemented")

endpoint = get_endpoint_tmpl % int(msgid)
request = urllib2.Request(url=endpoint)
self._add_headers(request)
server = urllib2.urlopen(request)
data = server.read()
try:
atom_entry = FeedParserPlus.parse(data)
except:
import tempfile
(datafilefd, datafilename) = tempfile.mkstemp(suffix=".atom",
prefix="seenthis_", text=True)
datafile = os.fdopen(datafilefd, 'w')
datafile.write(data)
datafile.close()
raise InvalidResponse("ATOM parsing error of the answer. The data has been saved in %s" % datafilename)
# If the message-ID does not exist, SeenThis does not return a
# 404 and sends back an invalid XML file :-(
# http://seenthis.net/messages/70646 So, we hack.
if atom_entry.has_key("bozo_exception"):
raise NotFound("Message %i does not apparently exist" % int(msgid))
return atom_entry

def get(self, n=None):
"""
n is the number of messages to retrieve. When None, we just retrieve what
Expand Down Expand Up @@ -131,6 +153,9 @@ def get(self, n=None):
return result

def post(self, message):
# TODO: allows to use a message-ID as parameter and set
# thr:in-reply-to (the post should then go under an existing
# thread.
context = simpleTALES.Context(allowPythonPath=False)
context.addGlobal ("message", unicode(message, encoding=myencoding))
result = simpleTALUtils.FastStringOutput()
Expand All @@ -144,7 +169,7 @@ def post(self, message):
def url_exists(self, url):
""" Returns an dictionary. Field "found" is a boolean indicating if
the URL was found. Field "messages" is an array of message numbers
where the URL is found. You can then use the future TODO get_message()
where the URL is found. You can then use the get_message()
method to retrieve it. """
digester = hashlib.md5()
digester.update(url)
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Expand Up @@ -21,7 +21,8 @@
url='https://github.com/bortzmeyer/seenthis-python',
download_url='https://github.com/bortzmeyer/seenthis-python/tarball/master',
py_modules=['SeenThis', 'FeedParserPlus'],
scripts=['seenthis-backup.py', 'seenthis-post.py', 'seenthis-test-url.py'],
scripts=['seenthis-backup.py', 'seenthis-post.py', 'seenthis-test-url.py',
'seenthis-get-msg.py'],
data_files=[('/usr/local/doc/SeenThis', ['README',]),],
provides=['SeenThis',],
install_requires=['feedparser'] # TODO: even when simpletal
Expand Down

0 comments on commit 678025d

Please sign in to comment.