Skip to content

Commit

Permalink
add microformats2.html_to_activities(), use it in MF2/AS demo
Browse files Browse the repository at this point in the history
for #31
  • Loading branch information
snarfed committed Sep 16, 2015
1 parent 03d97cc commit a58681f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
6 changes: 4 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,15 @@ def get(self):
url = util.get_required_param(self, 'url')
logging.info('Fetching %s', url)
resp = urllib2.urlopen(url, timeout=appengine_config.HTTP_TIMEOUT)
if url != resp.geturl():
logging.info('Redirected to %s', resp.geturl())
body = resp.read()

# decode data
if input == 'activitystreams':
activities = json.loads(body)
# elif input == 'html':
# activities = json.loads(body)
elif input == 'html':
activities = microformats2.html_to_activities(body, resp.geturl())
elif input == 'json-mf2':
activities = [microformats2.json_to_object(item)
for item in json.loads(body).get('items', [])]
Expand Down
16 changes: 15 additions & 1 deletion granary/microformats2.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import string
import xml.sax.saxutils

from mf2py import parser
from oauth_dropins.webutil import util
import source

Expand Down Expand Up @@ -272,6 +273,19 @@ def json_to_object(mf2):
return util.trim_nulls(obj)


def html_to_activities(html, url=None):
"""Converts a microformats2 HTML h-feed to ActivityStreams activities.
Args:
html: string HTML
url: optional string URL that HTML came from
Returns: list of ActivityStreams activity dicts
"""
parsed = parser.Parser(doc=html, url=None).to_dict()
return [{'object': json_to_object(item)} for item in parsed.get('items', [])]


def activities_to_html(activities):
"""Converts ActivityStreams activities to a microformats2 HTML h-feed.
Expand All @@ -291,7 +305,7 @@ def activities_to_html(activities):
%s
</body>
</html>
""" % '\n'.join(object_to_html(a.get('object') or a, a.get('context', {}))
""" % '\n'.join(object_to_html(a.get('object') or a, a.get('context', {}))
for a in activities)


Expand Down
28 changes: 20 additions & 8 deletions test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,19 @@
</body>
</html>
"""

ATOM = """\
ATOM_CONTENT = """\
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<br />
<br />
%s<br />
</div>
</content>
"""


Expand All @@ -97,10 +107,12 @@ def test_url_json_mf2_to_html(self):
self.assert_equals(200, resp.status_int)
self.assert_equals(HTML, resp.body)

# def test_url_html_to_atom(self):
# self.expect_urlopen('http://my/posts.json', json.dumps(ACTIVITIES))
# self.mox.ReplayAll()
# resp = app.application.get_response(
# '/url?url=http://my/posts.json&input=activitystreams&output=json-mf2')
# self.assert_equals(200, resp.status_int)
# self.assert_equals(MF2_JSON, json.loads(resp.body))
def test_url_html_to_atom(self):
self.expect_urlopen('http://my/posts.html', HTML)
self.mox.ReplayAll()

resp = app.application.get_response(
'/url?url=http://my/posts.html&input=html&output=atom')
self.assert_equals(200, resp.status_int)
self.assertIn(ATOM_CONTENT % 'foo bar', resp.body)
self.assertIn(ATOM_CONTENT % 'baz baj', resp.body)

0 comments on commit a58681f

Please sign in to comment.