From b9b1a3bd84d1de20041616c33257bb16908c9dc1 Mon Sep 17 00:00:00 2001 From: Matt Troutman Date: Thu, 5 Sep 2024 01:41:03 -0500 Subject: [PATCH 1/3] Update so script takes an argument from the command line. --- Avatars/Current Gravatar/fetch.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Avatars/Current Gravatar/fetch.py b/Avatars/Current Gravatar/fetch.py index 951fa6f..7447144 100644 --- a/Avatars/Current Gravatar/fetch.py +++ b/Avatars/Current Gravatar/fetch.py @@ -2,10 +2,14 @@ import hashlib import requests +# allow script to receive an argument +import sys +email_input = sys.argv[1] + def hash_email(email): return hashlib.sha256(email.encode('utf-8')).hexdigest() -def fetch_gravatar(email="", size=1000, rating="g"): +def fetch_gravatar(email, size=1000, rating="g"): url = f"https://www.gravatar.com/avatar/{hash_email(email)}?s={size}&r={rating}" print(url) response = requests.get(url) @@ -34,4 +38,4 @@ def variety_of_sizes(email,sizes=[1000,1500,800,300,200,150,100]): if __name__ == '__main__': # print( hash_email(email)) - variety_of_sizes("") + variety_of_sizes(email_input) From 9eefa1305804a465577cd3c7c46eeaabeecdc417 Mon Sep 17 00:00:00 2001 From: Matt Troutman Date: Thu, 5 Sep 2024 01:44:58 -0500 Subject: [PATCH 2/3] a little cleanup --- Avatars/Current Gravatar/fetch.py | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/Avatars/Current Gravatar/fetch.py b/Avatars/Current Gravatar/fetch.py index 7447144..be9be6c 100644 --- a/Avatars/Current Gravatar/fetch.py +++ b/Avatars/Current Gravatar/fetch.py @@ -1,9 +1,10 @@ # Script to grab the current Gravatar image for a given email address import hashlib import requests +import sys +from PIL import Image # allow script to receive an argument -import sys email_input = sys.argv[1] def hash_email(email): @@ -15,27 +16,18 @@ def fetch_gravatar(email, size=1000, rating="g"): response = requests.get(url) return response.content - - def save_gravatar(email, size=1000, rating="g"): with open(f"gravatar-{size}.png", "wb") as f: # Use "wb" mode for binary write f.write(fetch_gravatar(email, size)) -# Convert to webp format -import PIL -from PIL import Image + def convert_to_webp(size=1000): im = Image.open(f"gravatar-{size}.png") im.save(f"gravatar-{size}.webp") -def variety_of_sizes(email,sizes=[1000,1500,800,300,200,150,100]): +def variety_of_sizes(email, sizes=[1000, 1500, 800, 300, 200, 150, 100]): for x in sizes: save_gravatar(email, size=x) convert_to_webp(size=x) - - - - if __name__ == '__main__': - # print( hash_email(email)) - variety_of_sizes(email_input) + variety_of_sizes(email_input) \ No newline at end of file From 20f3a8de1c633f3f2fde46fb3426d6ea11090348 Mon Sep 17 00:00:00 2001 From: Matt Troutman Date: Thu, 5 Sep 2024 01:46:27 -0500 Subject: [PATCH 3/3] Documentation for the script --- Avatars/Current Gravatar/fetch.py | 68 ++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 2 deletions(-) diff --git a/Avatars/Current Gravatar/fetch.py b/Avatars/Current Gravatar/fetch.py index be9be6c..da95d47 100644 --- a/Avatars/Current Gravatar/fetch.py +++ b/Avatars/Current Gravatar/fetch.py @@ -1,30 +1,94 @@ -# Script to grab the current Gravatar image for a given email address +""" +Script to grab the current Gravatar image for a given email address and save it in multiple sizes and formats. + +Functions: + hash_email(email: str) -> str: + Hashes the email address using SHA-256 and returns the hexadecimal digest. + + fetch_gravatar(email: str, size: int = 1000, rating: str = "g") -> bytes: + Fetches the Gravatar image for the given email address, size, and rating. + Returns the image content as bytes. + + save_gravatar(email: str, size: int = 1000, rating: str = "g") -> None: + Saves the Gravatar image for the given email address, size, and rating to a PNG file. + + convert_to_webp(size: int = 1000) -> None: + Converts the saved PNG Gravatar image to WebP format. + + variety_of_sizes(email: str, sizes: list = [1000, 1500, 800, 300, 200, 150, 100]) -> None: + Fetches and saves Gravatar images in multiple sizes and converts them to WebP format. + +Usage: + Run the script with an email address as a command-line argument to fetch and save Gravatar images in multiple sizes and formats. +""" + import hashlib import requests import sys from PIL import Image -# allow script to receive an argument +# Allow script to receive an argument email_input = sys.argv[1] def hash_email(email): + """ + Hashes the email address using SHA-256 and returns the hexadecimal digest. + + Args: + email (str): The email address to hash. + + Returns: + str: The SHA-256 hash of the email address. + """ return hashlib.sha256(email.encode('utf-8')).hexdigest() def fetch_gravatar(email, size=1000, rating="g"): + """ + Fetches the Gravatar image for the given email address, size, and rating. + + Args: + email (str): The email address to fetch the Gravatar for. + size (int, optional): The size of the Gravatar image. Defaults to 1000. + rating (str, optional): The rating of the Gravatar image. Defaults to "g". + + Returns: + bytes: The content of the Gravatar image. + """ url = f"https://www.gravatar.com/avatar/{hash_email(email)}?s={size}&r={rating}" print(url) response = requests.get(url) return response.content def save_gravatar(email, size=1000, rating="g"): + """ + Saves the Gravatar image for the given email address, size, and rating to a PNG file. + + Args: + email (str): The email address to fetch the Gravatar for. + size (int, optional): The size of the Gravatar image. Defaults to 1000. + rating (str, optional): The rating of the Gravatar image. Defaults to "g". + """ with open(f"gravatar-{size}.png", "wb") as f: # Use "wb" mode for binary write f.write(fetch_gravatar(email, size)) def convert_to_webp(size=1000): + """ + Converts the saved PNG Gravatar image to WebP format. + + Args: + size (int, optional): The size of the Gravatar image. Defaults to 1000. + """ im = Image.open(f"gravatar-{size}.png") im.save(f"gravatar-{size}.webp") def variety_of_sizes(email, sizes=[1000, 1500, 800, 300, 200, 150, 100]): + """ + Fetches and saves Gravatar images in multiple sizes and converts them to WebP format. + + Args: + email (str): The email address to fetch the Gravatar for. + sizes (list, optional): A list of sizes for the Gravatar images. Defaults to [1000, 1500, 800, 300, 200, 150, 100]. + """ for x in sizes: save_gravatar(email, size=x) convert_to_webp(size=x)