cli commands

This commit is contained in:
cellarspoon 2021-12-15 12:23:37 +01:00
parent 30dbc6212f
commit b385833cbe
No known key found for this signature in database
GPG Key ID: 03789458B3D0C410
6 changed files with 154 additions and 190 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
test
__pycache__

View File

@ -1,8 +1,3 @@
#!/bin/python3
# lumbung.space calendar feed generator
# © 2021 roel roscam abbing gplv3 etc
import os
import re
import shutil
@ -187,6 +182,7 @@ def update_event_post(post_dir, event):
print("Event current: ", event.name, "({})".format(event.uid))
def main():
for event in list(cal.events):
post_dir = os.path.join(output_dir, event.uid)

View File

@ -1,8 +1,3 @@
#!/bin/python3
# lumbung.space rss feed aggregator
# © 2021 roel roscam abbing gplv3 etc
import os
import shutil
import time
@ -100,6 +95,9 @@ def create_post(post_dir, entry):
parsed_content = parse_posts(post_dir, post_content)
template_dir = os.path.join(Path(__file__).parent.resolve(), "templates")
env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_dir))
template = env.get_template("feed.md")
with open(os.path.join(post_dir, "index.html"), "w") as f: # n.b. .html
post = template.render(frontmatter=frontmatter, content=parsed_content)
f.write(post)
@ -140,7 +138,6 @@ def parse_posts(post_dir, post_content):
"""
soup = BeautifulSoup(post_content, "html.parser")
allowed_iframe_sources = ["youtube.com", "vimeo.com", "tv.lumbung.space"]
media = []
for img in soup(["img", "object"]):
local_image = grab_media(post_dir, img["src"])
@ -184,6 +181,7 @@ def grab_feed(feed_url):
return False
def main():
feed_urls = open("feeds_list.txt", "r").read().splitlines()
start = time.time()
@ -191,17 +189,11 @@ start = time.time()
if not os.path.exists("etags"):
os.mkdir("etags")
template_dir = os.path.join(Path(__file__).parent.resolve(), "templates")
env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_dir))
output_dir = os.environ.get("OUTPUT_DIR")
if not os.path.exists(output_dir):
os.makedirs(output_dir)
template = env.get_template("feed.md")
# add iframe to the allowlist of feedparser's sanitizer,
# this is now handled in parse_post()
feedparser.sanitizer._HTMLSanitizer.acceptable_elements |= {"iframe"}
@ -247,7 +239,6 @@ for feed_url in feed_urls:
print("deleted", post)
shutil.rmtree(os.path.join(feed_dir, slugify(post)))
end = time.time()
print(end - start)

View File

@ -1,9 +1,3 @@
# lumbung.space hashtag publishing bot
# © 2021 roel roscam abbing agplv3
# Makes Hugo posts out of hashtag feeds on Mastodon.
# Requires an account on the Mastodon instance configured.
# Currently does not do any thread recreation and only handles images
import os
import shutil
from pathlib import Path
@ -12,23 +6,11 @@ import jinja2
import requests
from mastodon import Mastodon
# Which instance to login to
instance = "https://social.lumbung.space"
# n.b. if it is the first time you use this script
# You need to register the app:
# https://mastodonpy.readthedocs.io/en/stable/#module-mastodon
# Login credentials for bot account
email = ""
password = ""
# Which hashtags to publish
hashtags = ["jalansesama"]
# your Hugo content directory
output_dir = os.environ.get("OUTPUT_DIR", "path/to/hugo/content")
def login_mastodon_bot():
mastodon = Mastodon(
@ -82,6 +64,14 @@ def create_post(post_directory, post_metadata):
if not os.path.exists(post_directory):
os.mkdir(post_directory)
template_dir = os.path.join(Path(__file__).parent.resolve(), "templates")
env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_dir))
env.filters["localize_media_url"] = localize_media_url
env.filters["filter_mastodon_urls"] = filter_mastodon_urls
template = env.get_template("hashtag.md")
with open(os.path.join(post_directory, "index.html"), "w") as f:
post = template.render(post_metadata=post_metadata)
f.write(post)
@ -106,24 +96,13 @@ def filter_mastodon_urls(content):
return content
def main():
mastodon = login_mastodon_bot()
output_dir = output_dir
template_dir = os.path.join(Path(__file__).parent.resolve(), "templates")
env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_dir))
env.filters["localize_media_url"] = localize_media_url
env.filters["filter_mastodon_urls"] = filter_mastodon_urls
template = env.get_template("hashtag.md")
output_dir = os.environ.get("OUTPUT_DIR")
if not os.path.exists(output_dir):
os.mkdir(output_dir)
for hashtag in hashtags:
hashtag_dir = os.path.join(output_dir, hashtag)
@ -158,5 +137,7 @@ for hashtag in hashtags:
) # create list of posts which have not been returned in the feed
for post in existing_posts:
print("deleted", post) # rm posts that exist but are no longer returned in feed
print(
"deleted", post
) # rm posts that exist but are no longer returned in feed
shutil.rmtree(os.path.join(hashtag_dir, post))

View File

@ -1,8 +1,3 @@
#!/bin/python3
# lumbung.space video feed generator
# c 2021 roel roscam abbing gpvl3 etc
import ast
import datetime
import json
@ -15,6 +10,9 @@ import jinja2
import peertube
import requests
host = "https://tv.lumbung.space"
configuration = peertube.Configuration(host=host + "/api/v1")
client = peertube.ApiClient(configuration)
# jinja filters & config
def duration(n):
@ -35,29 +33,10 @@ def linebreaks(text):
return br.sub(r"<br />\n", text)
template_dir = os.path.join(Path(__file__).parent.resolve(), "templates")
env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_dir))
env.filters["duration"] = duration
env.filters["linebreaks"] = linebreaks
host = "https://tv.lumbung.space"
configuration = peertube.Configuration(host=host + "/api/v1")
client = peertube.ApiClient(configuration)
v = peertube.VideoApi(client)
response = v.videos_get(count=100, filter="local", tags_one_of="publish")
videos = response.to_dict()
videos = videos["data"]
def create_post(post_directory, video_metadata):
def create_post(post_directory, video_metadata, host):
global client # lazy
if not os.path.exists(post_dir):
if not os.path.exists(post_directory):
os.mkdir(post_directory)
preview_image = video_metadata["preview_path"].split("/")[-1]
@ -77,6 +56,12 @@ def create_post(post_directory, video_metadata):
long_description = ast.literal_eval(api_response)
video_metadata["description"] = long_description["description"]
template_dir = os.path.join(Path(__file__).parent.resolve(), "templates")
env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_dir))
env.filters["duration"] = duration
env.filters["linebreaks"] = linebreaks
template = env.get_template("video.md")
with open(os.path.join(post_directory, "index.md"), "w") as f:
post = template.render(v=video_metadata, host=host, preview_image=preview_image)
f.write(post)
@ -86,7 +71,7 @@ def create_post(post_directory, video_metadata):
f.write(timestamp.format("X"))
def update_post(post_directory, video_metadata):
def update_post(post_directory, video_metadata, host):
if os.path.exists(post_directory):
if os.path.exists(os.path.join(post_directory, ".timestamp")):
old_timestamp = open(os.path.join(post_directory, ".timestamp")).read()
@ -101,7 +86,7 @@ def update_post(post_directory, video_metadata):
video_metadata["name"],
"({})".format(video_metadata["uuid"]),
)
create_post(post_dir, video_metadata)
create_post(post_directory, video_metadata, host)
else:
print(
"Video current: ",
@ -110,18 +95,22 @@ def update_post(post_directory, video_metadata):
)
else:
# compat for when there is no timestamp yet..
create_post(post_dir, video_metadata)
create_post(post_directory, video_metadata, host)
output_dir = os.environ.get(
"OUTPUT_DIR", "/home/r/Programming/lumbung.space/lumbung.space-web/content/video"
)
def main():
v = peertube.VideoApi(client)
response = v.videos_get(count=100, filter="local", tags_one_of="publish")
videos = response.to_dict()
videos = videos["data"]
output_dir = os.environ.get("OUTPUT_DIR")
if not os.path.exists(output_dir):
os.mkdir(output_dir)
template = env.get_template("video.md")
existing_posts = os.listdir(output_dir)
for video_metadata in videos:
@ -130,13 +119,15 @@ for video_metadata in videos:
if (
video_metadata["uuid"] not in existing_posts
): # if there is a video we dont already have, make it
print("New: ", video_metadata["name"], "({})".format(video_metadata["uuid"]))
create_post(post_dir, video_metadata)
print(
"New: ", video_metadata["name"], "({})".format(video_metadata["uuid"])
)
create_post(post_dir, video_metadata, host)
elif (
video_metadata["uuid"] in existing_posts
): # if we already have the video do nothing, possibly update
update_post(post_dir, video_metadata)
update_post(post_dir, video_metadata, host)
existing_posts.remove(
video_metadata["uuid"]
) # create list of posts which have not been returned by peertube

View File

@ -19,3 +19,7 @@ peertube = {git = "https://framagit.org/framasoft/peertube/clients/python.git"}
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
[tool.poetry.scripts]
cal= "lumbunglib.cloudcal:main"
vid = "lumbunglib.video:main"