50 lines
1.1 KiB
Python
50 lines
1.1 KiB
Python
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) |