# 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 "