See #170: API for OPML export

This commit is contained in:
Eliot Berriot 2020-03-19 09:43:46 +01:00
commit 37885ada0b
No known key found for this signature in database
GPG key ID: 6B501DFD73514E14
4 changed files with 101 additions and 5 deletions

View file

@ -792,7 +792,7 @@ class RssFeedItemSerializer(serializers.Serializer):
return upload
def rss_date(dt):
def rfc822_date(dt):
return dt.strftime("%a, %d %b %Y %H:%M:%S %z")
@ -814,7 +814,7 @@ def rss_serialize_item(upload):
"title": [{"value": upload.track.title}],
"itunes:title": [{"value": upload.track.title}],
"guid": [{"cdata_value": str(upload.uuid), "isPermalink": "false"}],
"pubDate": [{"value": rss_date(upload.creation_date)}],
"pubDate": [{"value": rfc822_date(upload.creation_date)}],
"itunes:duration": [{"value": rss_duration(upload.duration)}],
"itunes:explicit": [{"value": "no"}],
"itunes:episodeType": [{"value": "full"}],
@ -921,3 +921,22 @@ def rss_serialize_channel_full(channel, uploads):
channel_data = rss_serialize_channel(channel)
channel_data["item"] = [rss_serialize_item(upload) for upload in uploads]
return {"channel": channel_data}
# OPML stuff
def get_opml_outline(channel):
return {
"title": channel.artist.name,
"text": channel.artist.name,
"type": "rss",
"xmlUrl": channel.get_rss_url(),
"htmlUrl": channel.actor.url,
}
def get_opml(channels, date, title):
return {
"version": "2.0",
"head": [{"date": [{"value": rfc822_date(date)}], "title": [{"value": title}]}],
"body": [{"outline": [get_opml_outline(channel) for channel in channels]}],
}

View file

@ -8,6 +8,7 @@ from rest_framework import viewsets
from django import http
from django.db import transaction
from django.db.models import Count, Prefetch, Q
from django.utils import timezone
from funkwhale_api.common import locales
from funkwhale_api.common import permissions
@ -93,6 +94,20 @@ class ChannelViewSet(
def perform_create(self, serializer):
return serializer.save(attributed_to=self.request.user.actor)
def list(self, request, *args, **kwargs):
if self.request.GET.get("output") == "opml":
queryset = self.filter_queryset(self.get_queryset())[:500]
opml = serializers.get_opml(
channels=queryset,
date=timezone.now(),
title="Funkwhale channels OPML export",
)
xml_body = renderers.render_xml(renderers.dict_to_xml_tree("opml", opml))
return http.HttpResponse(xml_body, content_type="application/xml")
else:
return super().list(request, *args, **kwargs)
@decorators.action(
detail=True,
methods=["post"],