Browse Source

Change atom feed library for feeds

pull/159/head
squeaky otter 2 years ago
parent
commit
6a2c8105ed
Signed by: dashie GPG Key ID: C2D57B325840B755
  1. 4
      app.py
  2. 8
      attic/albums.py
  3. 107
      controllers/feeds.py
  4. 1
      requirements.txt
  5. 9
      transcoding_utils.py

4
app.py

@ -163,9 +163,7 @@ def create_app(config_filename="config.py", app_name=None, register_blueprints=T
config_oauth(app)
# Setup Flask-Security
security = Security( # noqa: F841
app, user_datastore
)
security = Security(app, user_datastore) # noqa: F841
@FlaskSecuritySignals.password_reset.connect_via(app)
@FlaskSecuritySignals.password_changed.connect_via(app)

8
attic/albums.py

@ -1,10 +1,8 @@
from flask import Blueprint, render_template, request, redirect, url_for, flash, Response, json
from flask_babelex import gettext
from flask_security import login_required, current_user
from flask import Blueprint, request, Response, json
from flask_security import current_user
from forms import AlbumForm
from models import db, User, Album, Sound
from utils.various import InvalidUsage, add_user_log
from utils.various import InvalidUsage
bp_albums = Blueprint("bp_albums", __name__)

107
controllers/feeds.py

@ -1,10 +1,34 @@
from werkzeug.contrib.atom import AtomFeed
from flask import Blueprint, request, url_for, abort, current_app, g
from models import User, Sound, Album
from feedgen.feed import FeedGenerator
import pytz
bp_feeds = Blueprint("bp_feeds", __name__)
def gen_feed(title, author, feed_url, url, subtitle, logo):
fg = FeedGenerator()
fg.load_extension("podcast")
# fg.podcast.itunes_category('Technology', 'Podcasting')
fg.id(feed_url)
fg.title(title)
fg.author(author)
fg.link(href=url)
fg.link(href=feed_url, rel="self")
fg.logo(logo)
fg.subtitle(subtitle)
fg.language("en")
fg.generator(
generator="reel2bits", uri=f"https://{current_app.config['AP_DOMAIN']}", version=g.cfg["REEL2BITS_VERSION"]
)
return fg
def utcdate(date):
return date.replace(tzinfo=pytz.utc)
@bp_feeds.route("/feeds/tracks/<int:user_id>", methods=["GET"])
@bp_feeds.route("/feeds/tracks/<int:user_id>.atom", methods=["GET"])
def tracks(user_id):
@ -18,31 +42,27 @@ def tracks(user_id):
feed_url = request.url
url = f"https://{current_app.config['AP_DOMAIN']}/{user.name}"
feed = AtomFeed(
f"{user.name} tracks",
feed_url=feed_url,
url=url,
subtitle=f"Tracks of {user.name}",
logo=None or f"https://{current_app.config['AP_DOMAIN']}/static/userpic_placeholder.png",
generator=("reel2bits", f"https://{current_app.config['AP_DOMAIN']}", g.cfg["REEL2BITS_VERSION"]),
author={"name": user.name, "uri": f"https://{current_app.config['AP_DOMAIN']}/{user.name}"},
)
author = {"name": user.name, "uri": f"https://{current_app.config['AP_DOMAIN']}/{user.name}"}
logo = None or f"https://{current_app.config['AP_DOMAIN']}/static/userpic_placeholder.png"
feed = gen_feed(f"{user.name} tracks", author, feed_url, url, f"Tracks of {user.name}", logo)
for track in q:
url_transcode = url_for("get_uploads_stuff", thing="sounds", stuff=track.path_sound(orig=False), _external=True)
feed.add(
title=track.title,
title_type="text",
content=track.description,
content_type="text",
url=f"https://{current_app.config['AP_DOMAIN']}/{user.name}/track/{track.slug}",
links=[{"href": url_transcode, "type": "audio/mpeg", "rel": "enclosure"}],
updated=track.updated,
author={"name": user.name, "uri": f"https://{current_app.config['AP_DOMAIN']}/{user.name}"},
published=track.uploaded,
rights=track.licence_info()["name"],
)
return feed.get_response()
url = f"https://{current_app.config['AP_DOMAIN']}/{user.name}/track/{track.slug}"
fe = feed.add_entry()
fe.id(url)
fe.title(track.title)
fe.link(href=url)
fe.enclosure(url_transcode, 0, "audio/mpeg")
fe.description(track.description)
fe.author({"name": user.name, "uri": f"https://{current_app.config['AP_DOMAIN']}/{user.name}"})
fe.rights(track.licence_info()["name"])
fe.pubdate(utcdate(track.uploaded))
fe.updated(utcdate(track.updated))
fe.content(track.description)
return feed.atom_str(pretty=True)
@bp_feeds.route("/feeds/album/<int:user_id>/<int:album_id>", methods=["GET"])
@ -57,29 +77,26 @@ def album(user_id, album_id):
feed_url = request.url
url = f"https://{current_app.config['AP_DOMAIN']}/{user.name}/album/{album.title}"
feed = AtomFeed(
f"{album.title} by {user.name}",
feed_url=feed_url,
url=url,
subtitle=f"Tracks for album '{album.title}' by {user.name}",
logo=None or f"https://{current_app.config['AP_DOMAIN']}/static/artwork_placeholder.png",
generator=("reel2bits", f"https://{current_app.config['AP_DOMAIN']}", g.cfg["REEL2BITS_VERSION"]),
author={"name": user.name, "uri": f"https://{current_app.config['AP_DOMAIN']}/{user.name}"},
author = {"name": user.name, "uri": f"https://{current_app.config['AP_DOMAIN']}/{user.name}"}
logo = None or f"https://{current_app.config['AP_DOMAIN']}/static/artwork_placeholder.png"
feed = gen_feed(
f"{album.title} by {user.name}", author, feed_url, url, f"Tracks for album '{album.title}' by {user.name}", logo
)
for track in album.sounds.filter(Sound.transcode_state == Sound.TRANSCODE_DONE):
url_transcode = url_for("get_uploads_stuff", thing="sounds", stuff=track.path_sound(orig=False), _external=True)
feed.add(
id=track.flake_id,
title=track.title,
title_type="text",
content=track.description,
content_type="text",
url=f"https://{current_app.config['AP_DOMAIN']}/{user.name}/track/{track.slug}",
links=[{"href": url_transcode, "type": "audio/mpeg", "rel": "enclosure"}],
updated=track.updated,
author={"name": user.name, "uri": f"https://{current_app.config['AP_DOMAIN']}/{user.name}"},
published=track.uploaded,
rights=track.licence_info()["name"],
)
return feed.get_response()
url = f"https://{current_app.config['AP_DOMAIN']}/{user.name}/track/{track.slug}"
fe = feed.add_entry()
fe.id(url)
fe.title(track.title)
fe.link(href=url)
fe.enclosure(url_transcode, 0, "audio/mpeg")
fe.description(track.description)
fe.author({"name": user.name, "uri": f"https://{current_app.config['AP_DOMAIN']}/{user.name}"})
fe.rights(track.licence_info()["name"])
fe.pubdate(utcdate(track.uploaded))
fe.updated(utcdate(track.updated))
fe.content(track.description)
return feed.atom_str(pretty=True)

1
requirements.txt

@ -25,5 +25,6 @@ flask-cors>=3.0.8
cachetools==3.1.1
flasgger==0.9.3
pymediainfo==4.0
feedgen==0.8.0
git+https://github.com/jwag956/flask-security#egg=Flask-Security-too
git+https://github.com/rhaamo/little-boxes@reel2bits#egg=little-boxes

9
transcoding_utils.py

@ -9,7 +9,14 @@ import mutagen
from pymediainfo import MediaInfo
from models import db, SoundInfo, Sound
from utils.various import get_waveform, create_png_waveform, duration_human, add_user_log, generate_audio_dat_file, add_log
from utils.various import (
get_waveform,
create_png_waveform,
duration_human,
add_user_log,
generate_audio_dat_file,
add_log,
)
from pydub import AudioSegment
from os.path import splitext
from flask import current_app

Loading…
Cancel
Save