make it work

This commit is contained in:
nebula 2025-04-27 15:08:13 +00:00
parent c8d8d8916d
commit c1a4b5d336

50
main.py Normal file
View File

@ -0,0 +1,50 @@
from urllib.request import urlopen
from json import load, dump
from os.path import expanduser
from os import mkdir
from random import choice
from time import time_ns
posted_path = expanduser("~/.binkbot.sent")
bink = expanduser("~/.bink")
try:
mkdir(bink)
except FileExistsError:
pass
try:
with open(posted_path, "r") as f:
posted = load(f)
except FileNotFoundError:
posted = []
with open(posted_path, "w") as f:
dump(posted, f)
all_photos = load(urlopen("https://helixnebula.space/photos.json"))
photos = [
(placecode, filename)
for placecode, place in all_photos.items()
for filename in place["photos"]
]
def in_posted(obj):
return obj not in posted
filtered = [photo for photo in filter(in_posted, photos)]
if not filtered:
posted = []
else:
photos = filtered
random_photo = choice(photos)
posted.append(random_photo)
with open(posted_path, "w") as f:
dump(posted, f)
with open(f"{bink}/{time_ns()}", "w") as f:
post = f"""daily random photo from ~nebula:
https://helixnebula.space/{random_photo[0]}/{random_photo[1]}"""
f.write(post)