From 0f3188c4cdfdd43a34f7d427c2f745e94ead60e4 Mon Sep 17 00:00:00 2001 From: zepp Date: Thu, 25 Jul 2024 10:09:27 -0400 Subject: [PATCH] commit mixups, again. I'll get this down at some point --- .gitignore | 4 ++ README.md | 18 ++++++++ helper.sh | 3 ++ index.html | 30 ++++++++++++++ main.py | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++ rss_update.py | 90 ++++++++++++++++++++++++++++++++++++++++ style.css | 73 +++++++++++++++++++++++++++++++++ 7 files changed, 329 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 helper.sh create mode 100644 index.html create mode 100644 main.py create mode 100644 rss_update.py create mode 100644 style.css diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..583a0f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +img/ +__pycache__/ +rss.xml +.secrets diff --git a/README.md b/README.md new file mode 100644 index 0000000..f95198a --- /dev/null +++ b/README.md @@ -0,0 +1,18 @@ +# onecatper.day + +A small website where you can see one cat image, once per day. With the ability to (hopefully) subscribe via RSS as well! + +TODO: +- Add RSS support + +## Things I used for this that you might wanna check out + +[The Cat API](https://thecatapi.com/) - Kind of the heart of this whole website. You can play around with this wihtout signing up for an API key. For this project I think the free tier with an API key is more than enough. + +Google CDN replacement for Google Fonts. Used for material icons currently on buttons [fonts.coollabs.io](https://fonts.coollabs.io/) - this is on the [32bit cafe resource page](https://discourse.32bit.cafe/t/resources-list-for-the-personal-web/49) + +Color scheme for index.html: [AG-500Redux Palette](https://lospec.com/palette-list/ag-500redux) + +## check it out + +You can see the site currently here - [onecatper.day](https://onecatper.day) diff --git a/helper.sh b/helper.sh new file mode 100644 index 0000000..3d7aa6b --- /dev/null +++ b/helper.sh @@ -0,0 +1,3 @@ +#!/bin/sh +cd public_html/ +python3 main.py && python3 rss_update.py \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..9489223 --- /dev/null +++ b/index.html @@ -0,0 +1,30 @@ + + + One cat per day! + + + + + + +
+ +
+
+ + A picture of a cat +
+ + + \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..ca81ce2 --- /dev/null +++ b/main.py @@ -0,0 +1,111 @@ +# 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.readlines() + api_key = api_key[0].strip("\n") + + # 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 "" in el: + items.append(el) + if "" in el: + items.append(el) + + guid_items = [] + for el in items: + if "https://onecatper.day#" in el: + guid_items.append(el) + + hash_located_at = [] + for el in guid_items: + hash_located_at.append(el.index('#')) + + CLOSING_TAG_LENGTH = 7 + as_char_list = list(items[2]) + for i in range(hash_located_at[0] + 1, len(as_char_list) - CLOSING_TAG_LENGTH): + as_char_list[i] = str(randint(0, 9)) + new_guid_link_one = "".join(as_char_list) + + as_char_list = list(items[4]) + for i in range(hash_located_at[1] + 1, len(as_char_list) - CLOSING_TAG_LENGTH): + as_char_list[i] = str(randint(0, 9)) + new_guid_link_two = "".join(as_char_list) + + split_file = rss_file.split("\n") + old_guid_links = [] + + for element in split_file: + if "https://onecatper.day#" in element: + old_guid_links.append(element) + + # preppring strings for replacement of the old strings + + # cat image stored in img/ + old_img_link = items[-1].split(" ") + old_cat_image = old_img_link[-1].split("\"") + old_cat_image_url = old_cat_image[1] + new_cat_image_url = f"https://onecatper.day/img/cat{DATETIME_STR}.jpg" + + # storess dates and times in the rss.xml file, needs to be updated for feed to update + old_last_build_date = items[1] + old_pub_date = items[3] + # date and time need to be a specific format + # don't want to use constant for this, it needs to be formated + rss_date = datetime.datetime.strftime(datetime.datetime.now(), "%a, %d %b %Y %H:%M:%S -0500") + # create new date/time links for feed + new_last_build_date = f"\t\t{rss_date}" + new_pub_date = f"\t\t\t{rss_date}" + + # replace what is necessary to make the feed update + rss_file = rss_file.replace(old_guid_links[0], new_guid_link_one) + rss_file = rss_file.replace(old_guid_links[1], new_guid_link_two) + rss_file = rss_file.replace(old_cat_image_url, new_cat_image_url) + rss_file = rss_file.replace(old_last_build_date, new_last_build_date) + rss_file = rss_file.replace(old_pub_date, new_pub_date) + + # write the changes to disk + with open(rss_file_name, "w") as f: + f.write(rss_file) + +update_rss_feed("rss.xml") diff --git a/style.css b/style.css new file mode 100644 index 0000000..f92ef07 --- /dev/null +++ b/style.css @@ -0,0 +1,73 @@ +* { + margin: 0; + padding: 0; +} + +nav { + display: flex; + justify-content: space-around; +} + +header { + padding: 0.5em; +} + +header, footer { + background-color: #4c8ed2; + color: #403f46; +} + +header div h1 { + padding: 0.1em; + font-size: 3em; + display: inline; +} + +.greeting { + width: 50%; + display: flex; + flex-direction: column; +} + +.buttons { + display: flex; + align-items: center; + justify-content: flex-end; +} + +button { + font-size: 1.25em; + margin: 5px; + padding: 10px; + border-radius: 10px; + color: #403f46; + background-color: #1cb9b5; + box-shadow: 10px 5px 5px #67cbc4; +} + +button span { + vertical-align: middle; + padding-left: 5px; + padding-right: 5px; +} + +main { + background-color: #9a6db6; + display: flex; + justify-content: center ; +} + +.cat-img { + height: 500px; + margin: 15px; + width: auto; + border-radius: 10px; + border: 5px solid #6d5a8c; +} + +footer p { + margin: auto; + padding: 1em; + text-align: center; + font-size: 1.5em; +} \ No newline at end of file