onecatper.day/main.py

110 lines
3.8 KiB
Python

# uses https://thecatapi.com/ to get an image of a cat
# todo on python side
# learn about rss and how to use this script to update a feed
import requests
import datetime
# this will change between calls due to microseconds
# better for it just to be a global
DATETIME_STR = str(datetime.datetime.today()).replace(' ', "-")
# we don't need those pesky microseconds any way
DATETIME_STR = DATETIME_STR[:-7]
def get_image_url():
# you can ask for a cat without a key, but the free tier has some nice benefits
# note below that I ask for 10 cats, which is the maximum with no api key. Having an
# api key gets you access to more cats however, which is purrfect for our needs
api_key = ""
with open(".secrets", "r") as f:
api_key = f.readline()
# I ask for 10 cats because I don't want an animated gif to use as the cat image of the day
# I'm specifically looking for a jpg, and may look for specific image dimeensions as well in the future
data = requests.get("https://api.thecatapi.com/v1/images/search?limit=10", headers={"x-api-key" : f"{api_key}"})
data = data.json()
# create a list of dictionaries
# each dictonary has the following keys
# url - the url to the iamge
# height - the height of the image in px
# width - the width of the image in px
image_data = {}
image_data_list = []
for url in data:
image_data["url"] = url["url"]
image_data["height"] = url["height"]
image_data["width"] = url["width"]
image_data_list.append(image_data)
image_data = {}
# we're looking for images of a specific height to help with a new image being on the
# index page every day. I've stored the width property if it is ever needed as well.
# As of now, it is not.
urls = []
for properties in image_data_list:
if properties["height"] >= 500 and properties["height"] <= 800:
urls.append(properties["url"])
image_url = ""
# not a big deal if both happen to be a jpg, but elminates the possibility of saving a gif to disk
for url in urls:
if ".jpg" in url:
image_url = url
return image_url
def update_html_img_tag(index):
old_img_tag = ""
tag_list = []
with open(index, "r") as f:
tag_list = f.readlines()
for tag in tag_list:
if "<img" in tag:
old_img_tag += tag
split_img_element = old_img_tag.split(" ")
time = datetime.datetime.today()
time = str(time).strip()
for i in range(0, len(split_img_element)):
if "src=" in split_img_element[i]:
split_img_element[i] = f"src=img/cat{DATETIME_STR}.jpg"
in_progress_img_tag = " ".join(split_img_element)
# cleaning up some things that don't need to be in the tag before writing it to index.html
img_element_as_list = in_progress_img_tag.split(" ")
new_img_tag = " ".join(img_element_as_list)
html = " ".join(tag_list)
html = html.replace(old_img_tag, new_img_tag)
with open(index, "w") as f:
f.write(html)
def main():
update_html_img_tag("index.html")
image_url = get_image_url()
# with our requirements for image picking in the above function, it is possible we end up
# with no url to get an image from. If that is the case, image_url will be empty.
# It can only be empty or have a url, so we should call it again if it is empty.
while image_url == "":
image_url = get_image_url()
# get the actual image and write to disk
# img/ should be a folder relative to main.py
image = requests.get(image_url)
path = "img/"
# a consistent name and placement of the image makes it easy for the index page to update
# when this script is run.
with open(f"{path}cat{DATETIME_STR}.jpg", "wb") as f:
f.write(image.content)
main()