def add_item_elements(self, handler, item): super(ActivityStreamsFeed, self).add_item_elements(handler, item) handler.addQuickElement("activity: verb", item["verb"]) if "actor" in item: handler.startElement("author", {}) handler.addQuickElement("name", item["actor"].display_name) handler.addQuickElement("uri", get_tag_uri(item["actor"].get_absolute_url(), None)) handler.addQuickElement("id", item["actor"].get_absolute_url()) handler.addQuickElement("activity: object-type", "person") handler.addQuickElement("link", get_tag_uri(item["actor"].get_absolute_url(), None), {"type": "text/html"}) handler.endElement("author") if "object" in item: handler.startElement("activity: object", {}) handler.addQuickElement("id", item["object_id"]) handler.addQuickElement("title", item["object_title"]) handler.addQuickElement("published", rfc3339_date(item["object_timestamp"]).decode("utf-8")) handler.addQuickElement("link", item["object"].get_absolute_url(), {"type": "text/html"}) handler.addQuickElement("activity: object-type", item["object_object_type"]) handler.endElement("activity: object") if "target" in item: handler.startElement("activity: target", {}) handler.addQuickElement("id", item["target_id"]) handler.addQuickElement("title", item["target_title"]) handler.addQuickElement("activity: object-type", str(item["target_object_type"])) handler.endElement("activity: target")
def add_item_elements(self, handler, item): super(ActivityStreamsFeed, self).add_item_elements(handler, item) handler.addQuickElement("activity: verb", item['verb']) if 'actor' in item: handler.startElement('author', {}) handler.addQuickElement('name', item['actor'].display_name) handler.addQuickElement('uri', get_tag_uri( item['actor'].get_absolute_url(), None)) handler.addQuickElement('id', item['actor'].get_absolute_url()) handler.addQuickElement('activity: object-type', 'person') handler.addQuickElement('link', get_tag_uri( item['actor'].get_absolute_url(), None), {'type': 'text/html'}) handler.endElement('author') if 'object' in item: handler.startElement('activity: object', {}) handler.addQuickElement('id', item['object_id']) handler.addQuickElement('title', item['object_title']) handler.addQuickElement('published', rfc3339_date(item['object_timestamp']).decode('utf-8')) handler.addQuickElement('link', item['object'].get_absolute_url(), {'type': 'text/html'}) handler.addQuickElement('activity: object-type', item['object_object_type']) handler.endElement('activity: object') if 'target' in item: handler.startElement('activity: target', {}) handler.addQuickElement('id', item['target_id']) handler.addQuickElement('title', item['target_title']) handler.addQuickElement('activity: object-type', str(item['target_object_type'])) handler.endElement('activity: target')
def add_item_elements(self, handler, item): """Inject Activity Stream elements into an item""" handler.addQuickElement('published', item['pubdate'].isoformat()) item['pubdate'] = None # Author information. if item['author_name'] is not None: handler.startElement(u"author", {}) handler.addQuickElement(u"activity:object-type", 'http://activitystrea.ms/schema/1.0/person') handler.addQuickElement(u"name", item['author_name']) if item['author_email'] is not None: handler.addQuickElement(u"email", item['author_email']) if item['author_link'] is not None: handler.addQuickElement(u"uri", item['author_link']) handler.addQuickElement(u"id", get_tag_uri(item['author_link'], item['pubdate'])) handler.addQuickElement(u"link", u"", { 'type': 'text/html', 'rel':'alternate', 'href': item['author_link'] }) handler.addQuickElement(u"link", u"", { 'type': 'image/jpeg', 'rel':'photo', 'media:width': '64', 'media:height': '64', 'href': self.feed['request'].build_absolute_uri( avatar_url(item['obj'].claimed_by, 64)), }) avatar_href = avatar_url(item['obj'].claimed_by, 64) if avatar_href.startswith('/'): avatar_href = self.feed['request'].build_absolute_uri(avatar_href) handler.addQuickElement(u"link", u"", { 'type': 'image/jpeg', 'rel':'preview', 'media:width': '64', 'media:height': '64', 'href': avatar_href, }) handler.endElement(u"author") item['author_name'] = None handler.addQuickElement('activity:verb', item['activity']['verb']) a_object = item['activity']['object'] handler.startElement(u"activity:object", {}) handler.addQuickElement(u"activity:object-type", a_object['object-type']) handler.addQuickElement(u"title", a_object['name']) handler.addQuickElement(u"id", get_tag_uri(a_object['link'], item['pubdate'])) handler.addQuickElement(u"link", '', { 'href': a_object['link'], 'rel':'alternate', 'type':'text/html' }) handler.addQuickElement(u"link", u"", { 'type': 'image/jpeg', 'rel':'preview', 'media:width': a_object['preview']['width'], 'media:height': a_object['preview']['height'], 'href': a_object['preview']['href'], }) handler.endElement(u"activity:object") super(ActivityStreamAtomFeedGenerator, self).add_item_elements(handler, item)
def write(self, outfile, encoding): # Check for a callback param, validate it before use callback = self.feed['request'].GET.get('callback', None) if callback is not None: if not validate_jsonp.is_valid_jsonp_callback_value(callback): callback = None items_out = [] for item in self.items: avatar_href = avatar_url(item['obj'].claimed_by, 64) if avatar_href.startswith('/'): avatar_href = self.request.build_absolute_uri(avatar_href) a_object = item['activity']['object'] item_out = { 'postedTime': item['pubdate'], 'verb': item['activity']['verb'], 'actor': { 'objectType': 'http://activitystrea.ms/schema/1.0/person', 'id': get_tag_uri(item['author_link'], item['pubdate']), 'displayName': item['author_name'], 'permalinkUrl': item['author_link'], 'image': { 'url': avatar_href, 'width':'64', 'height':'64', } }, 'object': { 'objectType': a_object['object-type'], 'id': get_tag_uri(a_object['link'], item['pubdate']), 'displayName': a_object['name'], 'permalinkUrl': a_object['link'], 'image': { 'url': a_object['preview']['href'], 'width': a_object['preview']['width'], 'height': a_object['preview']['height'], } }, } items_out.append(item_out) data = { "items": items_out } if callback: outfile.write('%s(' % callback) outfile.write(json.dumps(data, default=self._encode_complex)) if callback: outfile.write(')')
def add_item_elements(self, handler, item): super(ActivityStreamsFeed, self).add_item_elements(handler, item) # <activity:verb>post</activity:verb> handler.addQuickElement(u"activity:verb", item['verb']) if 'actor' in item: # <author> handler.startElement('author', {}) # <name>Geraldine</name> handler.addQuickElement('name', item['actor'].display_name) # <uri>http://example.com/geraldine</uri> handler.addQuickElement('uri', get_tag_uri(item['actor'].get_absolute_url(), None)) # <id>tag:photopanic.example.com,2009:person/4859</id> handler.addQuickElement('id', item['actor'].get_absolute_url()) # <activity:object-type>person</activity:object-type> handler.addQuickElement('activity:object-type', 'person') # <link rel="alternate" type="text/html" href="http://example.com/geraldine" /> handler.addQuickElement('link', get_tag_uri(item['actor'].get_absolute_url(), None), {'type':'text/html'}) handler.endElement('author') # </author> if 'object' in item: # <activity:object> handler.startElement('activity:object', {}) # <id>tag:photopanic.example.com,2009:photo/4352</id> handler.addQuickElement('id', item['object_id']) # <title>My Cat</title> handler.addQuickElement('title', item['object_title']) # <published>2009-11-02T15:29:00Z</published> handler.addQuickElement( u"published", rfc3339_date(item['object_timestamp']).decode('utf-8')) # <link rel="alternate" type="text/html" href="http://example.com/geraldine/photos/4352" /> handler.addQuickElement('link', item['object'].get_absolute_url(), {'type':'text/html'}) # <activity:object-type>photo</activity:object-type> handler.addQuickElement('activity:object-type', item['object_object_type']) # </activity:object> handler.endElement('activity:object') if 'target' in item: # <activity:target> handler.startElement('activity:target', {}) # <id>tag:photopanic.example.com,2009:photo-album/2519</id> handler.addQuickElement('id', item['target_id']) # <title>My Pets</title> handler.addQuickElement('title', item['target_title']) # <link rel="alternate" type="text/html" href="/geraldine/albums/pets" /> # <activity:object-type>photo-album</activity:object-type> handler.addQuickElement('activity:object-type', str(item['target_object_type'])) # </activity:target> handler.endElement('activity:target')
def test_get_tag_uri(self): """ Test get_tag_uri() correctly generates TagURIs. """ self.assertEqual( feedgenerator.get_tag_uri('http://example.org/foo/bar#headline', datetime.date(2004, 10, 25)), u'tag:example.org,2004-10-25:/foo/bar/headline')
def add_item_elements(self, handler, item): handler.addQuickElement("title", item['title']) handler.addQuickElement( "updated", datetime.now().strftime("%Y-%m-%d %H:%M:%S") ) handler.addQuickElement( "link", "", { "href": item['link'], "type": self.link_type } ) if item['unique_id'] is not None: unique_id = item['unique_id'] else: unique_id = get_tag_uri(item['link'], item['pubdate']) handler.addQuickElement("id", unique_id) handler.addQuickElement( "content", item['description'], attrs={ "type": "text" } )
def test_get_tag_uri_with_port(self): """ Test that get_tag_uri() correctly generates TagURIs from URLs with port numbers. """ self.assertEqual( feedgenerator.get_tag_uri('http://www.example.org:8000/2008/11/14/django#headline', datetime.datetime(2008, 11, 14, 13, 37, 0)), u'tag:www.example.org,2008-11-14:/2008/11/14/django/headline')
def test_get_tag_uri_with_port(self): """ Test that get_tag_uri() correctly generates TagURIs from URLs with port numbers. """ self.assertEqual( feedgenerator.get_tag_uri('http://www.example.org:8000/2008/11/14/django#headline', datetime.datetime(2008, 11, 14, 13, 37, 0)), 'tag:www.example.org,2008-11-14:/2008/11/14/django/headline')
def item_extra_kwargs(self, obj): """ Returns an extra keyword arguments dictionary that is used with the `add_item` call of the feed generator. Add the 'content' field of the 'Entry' item, to be used by the custom feed generator. """ try: object_id = obj.action_object.get_absolute_url() except: object_id = '%s/%s' % (obj.action_object_content_type.model, obj.action_object.id) object_id = get_tag_uri(object_id, None) item = { 'content': obj.description, 'actor': obj.actor, 'verb': obj.verb_uri_prefix + obj.verb, # action object 'object_timestamp': obj.timestamp, 'object': obj.action_object, 'object_id': object_id, 'object_title': unicode(obj.action_object), 'object_object_type': obj.action_object_content_type.model, } if obj.target: try: target_id = obj.target.get_absolute_url() except Exception: target_id = '%s/%s' % (obj.target_content_type.model, obj.action_object.id) target_id = get_tag_uri(target_id, obj.timestamp) item['target'] = obj.target item['target_id'] = target_id item['target_title'] = unicode(obj.target) item['target_object_type'] = obj.target_content_type.name return item
def item_extra_kwargs(self, obj): """ Returns an extra keyword arguments dictionary that is used with the `add_item` call of the feed generator. Add the 'content' field of the 'Entry' item, to be used by the custom feed generator. """ try: object_id = obj.action_object.get_absolute_url() except: object_id = "%s/%s" % (obj.action_object_content_type.model, obj.action_object.id) object_id = get_tag_uri(object_id, None) item = { "content": obj.description, "actor": obj.actor, "verb": obj.verb_uri_prefix + obj.verb, # action object "object_timestamp": obj.timestamp, "object": obj.action_object, "object_id": object_id, "object_title": unicode(obj.action_object), "object_object_type": obj.action_object_content_type.model, } if obj.target: try: target_id = obj.target.get_absolute_url() except Exception: target_id = "%s/%s" % (obj.target_content_type.model, obj.action_object.id) target_id = get_tag_uri(target_id, obj.timestamp) item["target"] = obj.target item["target_id"] = target_id item["target_title"] = unicode(obj.target) item["target_object_type"] = obj.target_content_type.name return item
def add_item_elements(self, handler, item): handler.addQuickElement("title", item['title']) handler.addQuickElement("updated", datetime.now().strftime("%Y-%m-%d %H:%M:%S")) handler.addQuickElement("link", "", { "href": item['link'], "type": self.ac_link_type }) if item['unique_id'] is not None: unique_id = item['unique_id'] else: unique_id = get_tag_uri(item['link'], item['pubdate']) handler.addQuickElement("id", unique_id) handler.addQuickElement( "category", "", { "label": item['category_label'], "term": item['category_term'], "scheme": "http://www.fictionbook.org/index.php/" + "%D0%96%D0%B0%D0%BD%D1%80%D1%8B_FictionBook_2.1/" }) handler.addQuickElement( "link", "", { "type": "text/html", "href": item['abs_link'], "rel": "alternate", "title": item['title'] }) handler.addQuickElement( "link", "", { "type": "image/jpeg", "href": item['cover_link'], "rel": "http://opds-spec.org/image", }) handler.addQuickElement( "link", "", { "type": "image/jpeg", "href": item['cover_thumb_link'], "rel": "http://opds-spec.org/image/thumbnail", }) handler.addQuickElement( "link", "", { "type": "application/fb2+zip", "href": item['download_link'], "rel": "http://opds-spec.org/acquisition", "title": item['title'] })
def add_item_elements(self, handler, item): super(ActivityStreamsFeed, self).add_item_elements(handler, item) handler.addQuickElement("activity: verb", item['verb']) if 'actor' in item: handler.startElement('author', {}) handler.addQuickElement('name', item['actor'].display_name) handler.addQuickElement( 'uri', get_tag_uri(item['actor'].get_absolute_url(), None)) handler.addQuickElement('id', item['actor'].get_absolute_url()) handler.addQuickElement('activity: object-type', 'person') handler.addQuickElement( 'link', get_tag_uri(item['actor'].get_absolute_url(), None), {'type': 'text/html'}) handler.endElement('author') if 'object' in item: handler.startElement('activity: object', {}) handler.addQuickElement('id', item['object_id']) handler.addQuickElement('title', item['object_title']) handler.addQuickElement( 'published', rfc3339_date(item['object_timestamp']).decode('utf-8')) handler.addQuickElement('link', item['object'].get_absolute_url(), {'type': 'text/html'}) handler.addQuickElement('activity: object-type', item['object_object_type']) handler.endElement('activity: object') if 'target' in item: handler.startElement('activity: target', {}) handler.addQuickElement('id', item['target_id']) handler.addQuickElement('title', item['target_title']) handler.addQuickElement('activity: object-type', str(item['target_object_type'])) handler.endElement('activity: target')
def add_item_elements(self, handler, item): handler.addQuickElement("title", item['title']) handler.addQuickElement("updated", datetime.now().strftime("%Y-%m-%d %H:%M:%S")) handler.addQuickElement("link", "", { "href": item['link'], "type": self.link_type }) if item['unique_id'] is not None: unique_id = item['unique_id'] else: unique_id = get_tag_uri(item['link'], item['pubdate']) handler.addQuickElement("id", unique_id) handler.addQuickElement("content", item['description'], attrs={"type": "text"})
class ActivityStreamsObjectActivityFeed(AtomObjectActivityFeed): feed_type = ActivityStreamsFeed def feed_extra_kwargs(self, obj): """ Returns an extra keyword arguments dictionary that is used when initializing the feed generator. """ return {} def item_extra_kwargs(self, obj): """ Returns an extra keyword arguments dictionary that is used with the `add_item` call of the feed generator. Add the 'content' field of the 'Entry' item, to be used by the custom feed generator. """ # { # 'actor_content_type_id': 21L, # 'description': None, # 'timestamp': datetime.datetime(2011, 8, 18, 23, 16, 9), # '_state': '<django.db.models.base.ModelState object at 0x1057357d0>', # '_target_cache': None, # 'public': True, # '_actor_cache': '<Person: Steve>', # 'actor_object_id': 1L, # 'verb': u'updated', # 'verb_uri_prefix': 'http://activitystrea.ms/schema/1.0/', # 'target_object_id': None, # 'action_object_content_type_id': 32L, # 'target_content_type_id': None, # 'id': 64L, # 'action_object_object_id': 1L # } try: object_id = obj.action_object.get_absolute_url() except Exception, e: object_id = obj.action_object_content_type.model + "/" + str( obj.action_object.id) object_id = get_tag_uri(object_id, None) item = { 'content': obj.description, 'actor': obj.actor, 'verb': obj.verb_uri_prefix + obj.verb, # action object 'object_timestamp': obj.timestamp, 'object': obj.action_object, 'object_id': object_id, 'object_title': obj.action_object.__unicode__(), 'object_object_type': obj.action_object_content_type.model, } if obj.target: try: target_id = obj.target.get_absolute_url() except Exception, e: target_id = obj.target_content_type.model + "/" + str( obj.action_object.id) target_id = get_tag_uri(target_id, obj.timestamp) item['target'] = obj.target item['target_id'] = target_id item['target_title'] = obj.target.__unicode__() item['target_object_type'] = obj.target_content_type.name
def add_item_elements(self, handler, item): handler.addQuickElement("title", item['title']) handler.addQuickElement( "updated", datetime.now().strftime("%Y-%m-%d %H:%M:%S") ) handler.addQuickElement( "link", "", { "href": item['link'], "type": self.ac_link_type } ) if item['unique_id'] is not None: unique_id = item['unique_id'] else: unique_id = get_tag_uri(item['link'], item['pubdate']) handler.addQuickElement("id", unique_id) handler.addQuickElement( "category", "", { "label": item['category_label'], "term": item['category_term'], "scheme": "http://www.fictionbook.org/index.php/" + "%D0%96%D0%B0%D0%BD%D1%80%D1%8B_FictionBook_2.1/" } ) handler.addQuickElement( "link", "", { "type": "text/html", "href": item['abs_link'], "rel": "alternate", "title": item['title'] } ) handler.addQuickElement( "link", "", { "type": "image/jpeg", "href": item['cover_link'], "rel": "http://opds-spec.org/image", } ) handler.addQuickElement( "link", "", { "type": "image/jpeg", "href": item['cover_thumb_link'], "rel": "http://opds-spec.org/image/thumbnail", } ) handler.addQuickElement( "link", "", { "type": "application/fb2+zip", "href": item['download_link'], "rel": "http://opds-spec.org/acquisition", "title": item['title'] } )
def add_item_elements(self, handler, item): if 'parent_href' in item and 'parent_updated' in item: handler.addQuickElement('thr:in-reply-to', get_tag_uri(item['parent_href'], item['parent_updated'])) super(SalmonAtom1Feed, self).add_item_elements(handler, item)
def add_item_elements(self, handler, item): handler.addQuickElement("title", item['title']) if 'summary' in item and item['summary'] is not None: handler.addQuickElement('summary', item['summary'], {"type": "text"}) self.__add_link_element(handler, item['link'], '', self.__get_link_type(item['link_type'])) if item['pubdate'] is not None: handler.addQuickElement('published', rfc3339_date(item['pubdate'])) if item['updateddate'] is not None: handler.addQuickElement('updated', rfc3339_date(item['updateddate'])) # Author information. if item['author_name'] is not None: handler.startElement("author", {}) handler.addQuickElement("name", item['author_name']) if item['author_email'] is not None: handler.addQuickElement("email", item['author_email']) if item['author_link'] is not None: handler.addQuickElement("uri", item['author_link']) handler.endElement("author") # Unique ID. if item['unique_id'] is not None: unique_id = item['unique_id'] else: unique_id = get_tag_uri(item['link'], item['pubdate']) handler.addQuickElement("id", unique_id) # Summary. if item['description'] is not None: handler.addQuickElement("content", item['description'], {"type": "text"}) # Categories. for cat in item['categories']: handler.addQuickElement("category", "", {"term": cat}) # Rights. if item['item_copyright'] is not None: handler.addQuickElement("rights", item['item_copyright']) if 'download_links' in item and item['download_links'] is not None: for link in item['download_links']: self.__add_link_element(handler, link['href'], 'http://opds-spec.org/acquisition', link['type']) if 'authors' in item and item['authors'] is not None: for author in item['authors']: handler.startElement("author", {}) handler.addQuickElement("name", author['name']) if author['url'] is not None: handler.addQuickElement("uri", author['url']) handler.endElement("author") if 'image' in item and item[ 'image'] is not None and 'image_type' in item and item[ 'image_type'] is not None: self.__add_link_element(handler, item['image'], 'http://opds-spec.org/image', item['image_type']) if 'isbn' in item and item['isbn'] is not None: handler.addQuickElement('dc:identifier', 'urn:isbn:' + item['isbn']) if 'year' in item and item['year'] is not None: handler.addQuickElement('dc:issued', item['year']) if 'language' in item and item['language'] is not None: handler.addQuickElement('dc:language', item['language'])
def item_guid(self, item): return get_tag_uri( self._request.build_absolute_uri(item.get_absolute_url()), item.created_on)
def test_get_tag_uri(inp): try: feedgenerator.get_tag_uri(inp, datetime.date(2004, 10, 25)) except ValueError: # TODO: Is this a wanted exception? pass