-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
for #124
- Loading branch information
Showing
9 changed files
with
259 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
"""Convert between ActivityStreams and RSS 2.0. | ||
RSS 2.0 spec: http://www.rssboard.org/rss-specification | ||
""" | ||
from __future__ import absolute_import, unicode_literals | ||
from builtins import str | ||
from past.builtins import basestring | ||
|
||
import mimetypes | ||
|
||
from feedgen.feed import FeedGenerator | ||
import mf2util | ||
from oauth_dropins.webutil import util | ||
|
||
from . import microformats2 | ||
|
||
# allowed ActivityStreams objectTypes for media enclosures | ||
ENCLOSURE_TYPES = {'audio', 'video'} | ||
|
||
|
||
def from_activities(activities, actor=None, title=None, description=None, | ||
feed_url=None, home_page_url=None, image_url=None): | ||
"""Converts ActivityStreams activities to an RSS 2.0 feed. | ||
Args: | ||
activities: sequence of ActivityStreams activity dicts | ||
actor: ActivityStreams actor dict, the author of the feed | ||
title: string, the feed title | ||
description, the feed description | ||
home_page_url: string, the home page URL | ||
# feed_url: the URL of this RSS feed, if any | ||
image_url: the URL of an image representing this feed | ||
Returns: | ||
unicode string with RSS 2.0 XML | ||
""" | ||
try: | ||
iter(activities) | ||
except TypeError: | ||
raise TypeError('activities must be iterable') | ||
|
||
if isinstance(activities, (dict, basestring)): | ||
raise TypeError('activities may not be a dict or string') | ||
|
||
fg = FeedGenerator() | ||
fg.id(feed_url) | ||
fg.link(href=feed_url, rel='self') | ||
fg.link(href=home_page_url, rel='alternate') | ||
fg.title(title) | ||
fg.description(description) | ||
fg.generator('granary', uri='https://granary.io/') | ||
if image_url: | ||
fg.image(image_url) | ||
|
||
latest = None | ||
for activity in activities: | ||
obj = activity.get('object') or activity | ||
if obj.get('objectType') == 'person': | ||
continue | ||
|
||
item = fg.add_entry() | ||
url = obj.get('url') | ||
item.id(obj.get('id') or url) | ||
item.link(href=url) | ||
item.guid(url, permalink=True) | ||
|
||
item.title(obj.get('title') or obj.get('displayName')) | ||
content = microformats2.render_content( | ||
obj, include_location=True, render_attachments=False) or obj.get('summary') | ||
if content: | ||
item.content(content, type='CDATA') | ||
|
||
item.category([{'term': t.displayName} for t in obj.get('tags', []) | ||
if t.displayName and t.verb not in ('like', 'react', 'share')]) | ||
|
||
author = obj.get('author', {}) | ||
item.author({ | ||
'name': author.get('displayName') or author.get('username'), | ||
'uri': author.get('url'), | ||
}) | ||
|
||
for prop in 'published', 'updated': | ||
val = obj.get(prop) | ||
if val: | ||
dt = util.parse_iso8601(val) | ||
getattr(item, prop)(dt) | ||
if not latest or dt > latest: | ||
latest = dt | ||
|
||
enclosures = False | ||
for att in obj.get('attachments', []): | ||
stream = util.get_first(att, 'stream') or att | ||
if not stream: | ||
continue | ||
|
||
url = stream.get('url') | ||
mime = mimetypes.guess_type(url)[0] if url else None | ||
if (att.get('objectType') in ENCLOSURE_TYPES or | ||
mime and mime.split('/')[0] in ENCLOSURE_TYPES): | ||
enclosures = True | ||
item.enclosure(url=url, type=mime) # TODO: length (bytes) | ||
|
||
item.load_extension('podcast') | ||
duration = stream.get('duration') | ||
if duration: | ||
item.podcast.itunes_duration(duration) | ||
|
||
if enclosures: | ||
fg.load_extension('podcast') | ||
if actor: | ||
fg.podcast.itunes_author(actor.get('displayName') or actor.get('username')) | ||
fg.podcast.itunes_image(image_url) | ||
if description: | ||
fg.podcast.itunes_subtitle(description) | ||
fg.podcast.itunes_explicit('no') | ||
fg.podcast.itunes_block(False) | ||
|
||
if latest: | ||
fg.lastBuildDate(dt) | ||
|
||
return fg.rss_str(pretty=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
[{ | ||
"url": "http://podcast/post", | ||
"objectType": "article", | ||
"displayName": "i'm ready to speak", | ||
"content": "<p>some HTML</p>", | ||
"published": "2012-12-05T00:58:26+00:00", | ||
"attachments": [{ | ||
"stream": { | ||
"url": "http://a/podcast.mp3", | ||
"duration": 328 | ||
}, | ||
"objectType": "audio" | ||
}] | ||
}, | ||
{ | ||
"url": "http://vidjo/post", | ||
"objectType": "article", | ||
"displayName": "i'm ready to perform", | ||
"summary": "other thing", | ||
"updated": "2012-12-06T00:58:26+00:00", | ||
"attachments": [{ | ||
"stream": { | ||
"url": "http://a/vidjo.mov", | ||
"duration": 428 | ||
}, | ||
"objectType": "video" | ||
}], | ||
"stream": [{ | ||
"url": "http://a/vidjo.mov", | ||
"duration": 428 | ||
}] | ||
}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
{ | ||
"items": [{ | ||
"type": ["h-feed"], | ||
"lang": "en", | ||
"properties": { | ||
"name": ["Pawd Kaast"], | ||
"summary": ["a pawd kaast by meee"], | ||
"photo": ["https://cover/"], | ||
"author": [{ | ||
"type": ["h-card"], | ||
"properties": { | ||
"name": ["Meee"], | ||
"photo": ["https://photo/of/meee"], | ||
"url": ["https://meee.com"], | ||
"bio": [{ | ||
"html": "my <a>bio</a>", | ||
"value": "my bio" | ||
}] | ||
}, | ||
"value": "Meeeeee" | ||
}] | ||
}, | ||
"children": [{ | ||
"type": ["h-entry"], | ||
"properties": { | ||
"url": ["http://a/podcast"], | ||
"name": ["i'm ready to speak"], | ||
"audio": ["http://a/podcast.mp3"], | ||
"duration": ["328"], | ||
"size": ["7.77mb"], | ||
"summary": [{ | ||
"html": "something", | ||
"value": "something" | ||
}] | ||
} | ||
}, | ||
{ | ||
"type": ["h-entry"], | ||
"properties": { | ||
"name": ["i'm ready to perform"], | ||
"url": ["http://a/vidjo"], | ||
"video": ["http://a/vidjo.mov"], | ||
"duration": ["428"], | ||
"size": ["8.88mb"], | ||
"summary": [{ | ||
"html": "other thing", | ||
"value": "other thing" | ||
}] | ||
} | ||
}] | ||
}] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?xml version='1.0' encoding='UTF-8'?> | ||
<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0"> | ||
<channel> | ||
<title>Stuff</title> | ||
<link>http://site/</link> | ||
<description>some stuff by meee</description> | ||
<atom:link href="http://site/feed" rel="self"/> | ||
<docs>http://www.rssboard.org/rss-specification</docs> | ||
<generator>granary</generator> | ||
<image> | ||
<url>http://site/logo.png</url> | ||
<title>Stuff</title> | ||
<link>http://site/</link> | ||
</image> | ||
<lastBuildDate>Thu, 06 Dec 2012 00:58:26 +0000</lastBuildDate> | ||
<itunes:author>Martin Smith</itunes:author> | ||
<itunes:block>no</itunes:block> | ||
<itunes:image href="http://site/logo.png"/> | ||
<itunes:explicit>no</itunes:explicit> | ||
<itunes:subtitle>some stuff by meee</itunes:subtitle> | ||
|
||
<item> | ||
<title>i'm ready to perform</title> | ||
<link>http://vidjo/post</link> | ||
<description>other thing</description> | ||
<guid isPermaLink="true">http://vidjo/post</guid> | ||
<enclosure url="http://a/vidjo.mov" length="0" type="video/quicktime"/> | ||
<itunes:duration>428</itunes:duration> | ||
</item> | ||
|
||
<item> | ||
<title>i'm ready to speak</title> | ||
<link>http://podcast/post</link> | ||
<description><p>some HTML</p></description> | ||
<guid isPermaLink="true">http://podcast/post</guid> | ||
<enclosure url="http://a/podcast.mp3" length="0" type="audio/mpeg"/> | ||
<pubDate>Wed, 05 Dec 2012 00:58:26 +0000</pubDate> | ||
<itunes:duration>328</itunes:duration> | ||
</item> | ||
|
||
</channel> | ||
</rss> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters