Skip to content

Commit

Permalink
mf2: add nickname and (custom) numeric-id properties
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed Jul 22, 2017
1 parent 363be66 commit 4fbc399
Show file tree
Hide file tree
Showing 24 changed files with 87 additions and 30 deletions.
2 changes: 1 addition & 1 deletion granary/facebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -1199,7 +1199,7 @@ def post_to_object(self, post, type=None):
place_id = place.get('id')
obj['location'] = {
'displayName': place.get('name'),
'id': place_id,
'id': self.tag_uri(place_id),
'url': self.object_url(place_id),
}
location = place.get('location', None)
Expand Down
2 changes: 1 addition & 1 deletion granary/instagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ def media_to_object(self, media):
if 'location' in media:
media_loc = media.get('location', {})
object['location'] = {
'id': media_loc.get('id'),
'id': self.tag_uri(media_loc.get('id')),
'displayName': media_loc.get('name'),
'latitude': media_loc.get('point', {}).get('latitude'),
'longitude': media_loc.get('point', {}).get('longitude'),
Expand Down
23 changes: 16 additions & 7 deletions granary/microformats2.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@
""")
HCARD = string.Template("""\
<span class="$types">
$ids
$linked_name
$photo
$nicknames
$photos
</span>
""")
IN_REPLY_TO = string.Template(' <a class="u-in-reply-to" href="$url"></a>')
Expand Down Expand Up @@ -171,8 +173,10 @@ def object_to_json(obj, trim_nulls=True, entry_class='h-entry',
[entry_class] if isinstance(entry_class, basestring)
else list(entry_class)),
'properties': {
'uid': [obj.get('id', '')],
'uid': [obj.get('id') or ''],
'numeric-id': [obj.get('numeric_id') or ''],
'name': [name],
'nickname': [obj.get('username') or ''],
'summary': [summary],
'url': (list(object_urls(obj) or object_urls(primary)) +
obj.get('upstreamDuplicates', [])),
Expand Down Expand Up @@ -602,13 +606,18 @@ def hcard_to_html(hcard, parent_props=None):
if not prop:
return ''

prop.setdefault('uid', '')
photo = prop.get('photo')
return HCARD.substitute(
prop,
types=' '.join(util.uniquify(parent_props + hcard.get('type', []))),
photo=img(photo, 'u-photo', '') if photo else '',
linked_name=maybe_linked_name(props))
ids='\n'.join(['<data class="p-uid" value="%s" />' % uid
for uid in props.get('uid', []) if uid] +
['<data class="p-numeric-id" value="%s" />' % nid
for nid in props.get('numeric-id', []) if nid]),
linked_name=maybe_linked_name(props),
nicknames='\n'.join('<span class="p-nickname">%s</span>' % nick
for nick in props.get('nickname', []) if nick),
photos='\n'.join(img(photo, 'u-photo', '')
for photo in props.get('photo', []) if photo),
)


def render_content(obj, include_location=True, synthesize_content=True):
Expand Down
1 change: 1 addition & 0 deletions granary/test/test_atom.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def test_render_share_no_content(self):
out = atom.activities_to_atom([activity], {})
self.assert_multiline_in("""
Shared <a href="#">a post</a> by <span class="h-card">
<span class="p-name">Mr. Foo</span>
</span>
Expand Down
3 changes: 2 additions & 1 deletion granary/test/test_facebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ def tag_uri(name):
'to': [{'objectType':'group', 'alias':'@public'}],
'location': {
'displayName': 'Lake Merced',
'id': '113785468632283',
'id': tag_uri('113785468632283'),
'url': 'https://www.facebook.com/113785468632283',
'latitude': 37.728193717481,
'longitude': -122.49336423595,
Expand Down Expand Up @@ -945,6 +945,7 @@ def tag_uri(name):
<span class="summary">my link caption</span>
</p>
<p> <span class="p-location h-card">
<data class="p-uid" value="tag:facebook.com:113785468632283" />
<a class="p-name u-url" href="https://www.facebook.com/113785468632283">Lake Merced</a>
</span>
Expand Down
5 changes: 3 additions & 2 deletions granary/test/test_instagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def tag_uri(name):
'image': {'url': 'http://attach/image/big'},
'to': [{'objectType':'group', 'alias':'@public'}],
'location': {
'id': '520640',
'id': tag_uri('520640'),
'displayName': 'Le Truc',
'latitude': 37.3,
'longitude': -122.5,
Expand Down Expand Up @@ -403,6 +403,7 @@ def tag_uri(name):
</a>
</p>
<p> <span class="p-location h-card">
<data class="p-uid" value="tag:instagram.com:520640" />
<a class="p-name u-url" href="https://instagram.com/explore/locations/520640/">Le Truc</a>
</span>
Expand Down Expand Up @@ -900,7 +901,7 @@ def tag_uri(name):
'image': {'url': 'https://scontent-sjc2-1.cdninstagram.com/hphotos-xfp1/t51.2885-15/e35/12545499_1662965520652470_1466520818_n.jpg'},
'to': [{'objectType':'group', 'alias':'@public'}],
'location': {
'id': '345924646',
'id': tag_uri('345924646'),
'displayName': 'RCA Studio B',
'url': 'https://instagram.com/explore/locations/345924646/',
},
Expand Down
11 changes: 4 additions & 7 deletions granary/test/test_microformats2.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def test_render_content_omits_tags_without_urls(self):
}))

def test_render_content_location(self):
self.assert_equals("""\
self.assert_multiline_equals("""\
foo
<p> <span class="p-location h-card">
<a class="p-name u-url" href="http://my/place">My place</a>
Expand All @@ -233,7 +233,7 @@ def test_render_content_location(self):
'displayName': 'My place',
'url': 'http://my/place',
}
}))
}), ignore_blanks=True)

def test_render_content_synthesize_content(self):
for verb, phrase in ('like', 'likes'), ('share', 'shared'):
Expand Down Expand Up @@ -287,17 +287,14 @@ def test_render_content_unicode_high_code_points(self):
}]}))

def test_escape_html_attribute_values(self):
self.assert_equals("""\
self.assert_multiline_equals("""\
<article class="h-entry">
<span class="p-uid"></span>
<span class="p-author h-card">
<span class="p-name">a " b ' c</span>
<img class="u-photo" src="img" alt="" />
</span>
<div class="e-content p-name">
<p>
<img class="thumbnail" src="img" alt="d &amp; e" />
<span class="name">d & e</span>
Expand All @@ -307,7 +304,7 @@ def test_escape_html_attribute_values(self):
</article>""", microformats2.object_to_html({
'author': {'image': {'url': 'img'}, 'displayName': 'a " b \' c'},
'attachments': [{'image': {'url': 'img'}, 'displayName': 'd & e'}],
}))
}), ignore_blanks=True)

def test_mention_and_hashtag(self):
self.assert_equals("""
Expand Down
12 changes: 8 additions & 4 deletions granary/test/test_testdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,14 @@ def read_json(filename):


def create_test_function(fn, original, expected):
"""Create a simple test function that asserts
fn(original) == expected"""
return lambda self: self.assert_equals(expected, fn(original))

"""Create a simple test function that asserts fn(original) == expected"""
def test(self):
got = fn(original)
if isinstance(got, basestring) and isinstance(expected, basestring):
return self.assert_multiline_equals(expected, got, ignore_blanks=True)
else:
return self.assert_equals(expected, got)
return test

# TODO: use a handler with an HTTPS request so that URL schemes are converted
# self.handler.request = webapp2.Request.blank('/', base_url='https://foo')
Expand Down
3 changes: 2 additions & 1 deletion granary/test/test_twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def tag_uri(name):
'image': {'url': 'http://p.twimg.com/picture1'},
'location': {
'displayName': 'Carcassonne, Aude',
'id': '31cb9e7ed29dbe52',
'id': tag_uri('31cb9e7ed29dbe52'),
'url': 'https://maps.google.com/maps?q=32.4004416,-98.9852672',
},
'to': [{'objectType': 'group', 'alias': '@public'}],
Expand Down Expand Up @@ -649,6 +649,7 @@ def tag_uri(name):
</a>
</p>
<p> <span class="p-location h-card">
<data class="p-uid" value="tag:twitter.com:31cb9e7ed29dbe52" />
<a class="p-name u-url" href="https://maps.google.com/maps?q=32.4004416,-98.9852672">Carcassonne, Aude</a>
</span>
Expand Down
1 change: 1 addition & 0 deletions granary/test/testdata/actor.mf2.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<span class="h-card">
<data class="p-uid" value="tag:example.com,2011:martin" />
<a class="p-name u-url" href="http://example.com/martin">Martin Smith</a>
<img class="u-photo" src="http://example.com/martin/image" alt="" />
</span>
1 change: 1 addition & 0 deletions granary/test/testdata/actor_multiple_urls.mf2.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<span class="h-card">
<data class="p-uid" value="tag:example.com,2011:martin" />
<a class="p-name u-url" href="http://example.com/martin">Martin Smith</a>
<a class="u-url" href="http://se.co/nd"></a>
<a class="u-url" href="http://third/"></a>
Expand Down
14 changes: 14 additions & 0 deletions granary/test/testdata/actor_with_nickname_numeric_id.as.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"urls": [
{"value": "http://example.com/martin"},
{"value": "https://example.com/4321"}
],
"objectType" : "person",
"id": "tag:example.com,2011:martin",
"numeric_id": "4321",
"image": [{
"url": "http://example.com/martin/image"
}],
"displayName": "Martin Smith",
"username": "martin"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<span class="h-card">
<data class="p-uid" value="tag:example.com,2011:martin" />
<data class="p-numeric-id" value="4321" />
<a class="p-name u-url" href="http://example.com/martin">Martin Smith</a>
<a class="u-url" href="https://example.com/4321"></a>
<span class="p-nickname">martin</span>
<img class="u-photo" src="http://example.com/martin/image" alt="" />
</span>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"type": ["h-card"],
"properties": {
"uid": ["tag:example.com,2011:martin"],
"numeric-id": ["4321"],
"name": ["Martin Smith"],
"nickname": ["martin"],
"url": [
"http://example.com/martin",
"https://example.com/4321"
],
"photo": ["http://example.com/martin/image"]
}
}
2 changes: 1 addition & 1 deletion granary/test/testdata/article_with_likes.as.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"verb": "like",
"object": {"url": "http://example.com/abc"},
"author": {
"id": "100004",
"numeric_id": "100004",
"displayName": "Alice",
"url": "http://example.com/alice"
}
Expand Down
1 change: 1 addition & 0 deletions granary/test/testdata/article_with_likes.mf2.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<span class="p-uid"></span>

<span class="p-author h-card">
<data class="p-numeric-id" value="100004" />
<a class="p-name u-url" href="http://example.com/alice">Alice</a>

</span>
Expand Down
2 changes: 1 addition & 1 deletion granary/test/testdata/article_with_likes.mf2.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"author": [{
"type": ["h-card"],
"properties": {
"uid": ["100004"],
"numeric-id": ["100004"],
"name": ["Alice"],
"url": ["http://example.com/alice"]
}
Expand Down
2 changes: 1 addition & 1 deletion granary/test/testdata/article_with_reposts.as.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"objectType": "share",
"object": {"url": "http://example.com/abc"},
"author": {
"id": "100004",
"numeric_id": "100004",
"displayName": "Alice",
"url": "http://example.com/alice"
}
Expand Down
1 change: 1 addition & 0 deletions granary/test/testdata/article_with_reposts.mf2.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<span class="p-uid"></span>

<span class="p-author h-card">
<data class="p-numeric-id" value="100004" />
<a class="p-name u-url" href="http://example.com/alice">Alice</a>

</span>
Expand Down
2 changes: 1 addition & 1 deletion granary/test/testdata/article_with_reposts.mf2.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"author": [{
"type": ["h-card"],
"properties": {
"uid": ["100004"],
"numeric-id": ["100004"],
"name": ["Alice"],
"url": ["http://example.com/alice"]
}
Expand Down
1 change: 1 addition & 0 deletions granary/test/testdata/comment.mf2.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
</div>

<span class="p-location h-card">
<data class="p-uid" value="31cb9e7ed29dbe52" />
<a class="p-name u-url" href="https://maps.google.com/maps?q=32.4004416,-98.9852672">Carcassonne, Aude</a>

</span>
Expand Down
2 changes: 2 additions & 0 deletions granary/test/testdata/note.mf2.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<time class="dt-published" datetime="2012-02-22T20:26:41">2012-02-22T20:26:41</time>
<time class="dt-updated" datetime="2013-10-25T10:31:30+00:00">2013-10-25T10:31:30+00:00</time>
<span class="p-author h-card">
<data class="p-uid" value="tag:example.com,2001:ryan" />
<span class="p-name">Ryan Barrett</span>
<img class="u-photo" src="http://example.com/ryan/image" alt="" />
</span>
Expand All @@ -19,6 +20,7 @@
<img class="u-photo" src="http://example.com/blog-post-123/image" alt="attachment" />

<span class="p-location h-card">
<data class="p-uid" value="31cb9e7ed29dbe52" />
<a class="p-name u-url" href="https://maps.google.com/maps?q=32.4004416,-98.9852672">Carcassonne, Aude</a>

</span>
Expand Down
2 changes: 1 addition & 1 deletion granary/test/testdata/repost.mf2.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"url": ["http://example.com/this/repost"],
"published": ["2012-12-05T00:58:26+00:00"],
"content": [{
"html": "Shared <a href=\"http://example.com/original/post\">a post</a> by <span class=\"h-card\">\n <a class=\"p-name u-url\" href=\"http://example.com/bob\">Bob</a>\n \n </span>\nThe original post"
"html": "Shared <a href=\"http://example.com/original/post\">a post</a> by <span class=\"h-card\">\n \n<a class=\"p-name u-url\" href=\"http://example.com/bob\">Bob</a>\n \n </span>\nThe original post"
}],
"repost-of": [{
"type": ["h-cite"],
Expand Down
2 changes: 1 addition & 1 deletion granary/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,7 @@ def tweet_to_object(self, tweet):
if place:
obj['location'] = {
'displayName': place.get('full_name'),
'id': place.get('id'),
'id': self.tag_uri(place.get('id')),
}

# place['url'] is a JSON API url, not useful for end users. get the
Expand Down

0 comments on commit 4fbc399

Please sign in to comment.