From 29e6ebdbd3f0d4c19c676eb2fd94b768132ab267 Mon Sep 17 00:00:00 2001 From: ashish-patwal Date: Sun, 18 Jul 2021 20:33:03 +0530 Subject: [PATCH 1/7] fixed colons and spaces --- web_programming/random_anime_character.py | 43 +++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 web_programming/random_anime_character.py diff --git a/web_programming/random_anime_character.py b/web_programming/random_anime_character.py new file mode 100644 index 000000000000..dc19b9496443 --- /dev/null +++ b/web_programming/random_anime_character.py @@ -0,0 +1,43 @@ +import os +import requests +from bs4 import BeautifulSoup +from fake_useragent import UserAgent + + +headers = {"UserAgent": UserAgent().random} +URL = "https://www.mywaifulist.moe/random" + + +def save_image(image_url: str, image_title: str) -> None: + """ + Saves the image of anime character + """ + image = requests.get(image_url, headers=headers) + with open(image_title, "wb") as file: + file.write(image.content) + + +def random_anime_character() -> None: + """ + Returns the Name and Description of the anime character . + """ + + html = requests.get(URL, headers=headers).text + soup = BeautifulSoup(html, "html.parser") + + title = soup.find("meta", attrs={"property": "og:title"}).attrs["content"] + image_url = soup.find("meta", attrs={"property": "og:image"}).attrs["content"] + description = soup.find("p", id="description").get_text() + + _, image_extension = os.path.splitext(os.path.basename(image_url)) + image_title = title.strip().replace(" ", "_") + image_title = f"{image_title}{image_extension}" + + save_image(image_url, image_title) + + return (title, description, image_title) + + +if __name__ == "__main__": + title, desc, image_title = random_anime_character() + print(f"{title}\n\n{desc}\n\nImage saved : {image_title}") From f7bdf5acd5111ae1f4a723b3550a88318fa3b997 Mon Sep 17 00:00:00 2001 From: ashish-patwal Date: Sun, 18 Jul 2021 20:38:12 +0530 Subject: [PATCH 2/7] fixed colons and spaces --- random_anime_character.py | 43 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 random_anime_character.py diff --git a/random_anime_character.py b/random_anime_character.py new file mode 100644 index 000000000000..dc19b9496443 --- /dev/null +++ b/random_anime_character.py @@ -0,0 +1,43 @@ +import os +import requests +from bs4 import BeautifulSoup +from fake_useragent import UserAgent + + +headers = {"UserAgent": UserAgent().random} +URL = "https://www.mywaifulist.moe/random" + + +def save_image(image_url: str, image_title: str) -> None: + """ + Saves the image of anime character + """ + image = requests.get(image_url, headers=headers) + with open(image_title, "wb") as file: + file.write(image.content) + + +def random_anime_character() -> None: + """ + Returns the Name and Description of the anime character . + """ + + html = requests.get(URL, headers=headers).text + soup = BeautifulSoup(html, "html.parser") + + title = soup.find("meta", attrs={"property": "og:title"}).attrs["content"] + image_url = soup.find("meta", attrs={"property": "og:image"}).attrs["content"] + description = soup.find("p", id="description").get_text() + + _, image_extension = os.path.splitext(os.path.basename(image_url)) + image_title = title.strip().replace(" ", "_") + image_title = f"{image_title}{image_extension}" + + save_image(image_url, image_title) + + return (title, description, image_title) + + +if __name__ == "__main__": + title, desc, image_title = random_anime_character() + print(f"{title}\n\n{desc}\n\nImage saved : {image_title}") From 9ac182fbe61ad2e83475d2db9bacf9fb5b2dc76c Mon Sep 17 00:00:00 2001 From: ashish-patwal Date: Sun, 18 Jul 2021 21:27:21 +0530 Subject: [PATCH 3/7] random anime character python script --- random_anime_character.py | 43 --------------------------------------- 1 file changed, 43 deletions(-) delete mode 100644 random_anime_character.py diff --git a/random_anime_character.py b/random_anime_character.py deleted file mode 100644 index dc19b9496443..000000000000 --- a/random_anime_character.py +++ /dev/null @@ -1,43 +0,0 @@ -import os -import requests -from bs4 import BeautifulSoup -from fake_useragent import UserAgent - - -headers = {"UserAgent": UserAgent().random} -URL = "https://www.mywaifulist.moe/random" - - -def save_image(image_url: str, image_title: str) -> None: - """ - Saves the image of anime character - """ - image = requests.get(image_url, headers=headers) - with open(image_title, "wb") as file: - file.write(image.content) - - -def random_anime_character() -> None: - """ - Returns the Name and Description of the anime character . - """ - - html = requests.get(URL, headers=headers).text - soup = BeautifulSoup(html, "html.parser") - - title = soup.find("meta", attrs={"property": "og:title"}).attrs["content"] - image_url = soup.find("meta", attrs={"property": "og:image"}).attrs["content"] - description = soup.find("p", id="description").get_text() - - _, image_extension = os.path.splitext(os.path.basename(image_url)) - image_title = title.strip().replace(" ", "_") - image_title = f"{image_title}{image_extension}" - - save_image(image_url, image_title) - - return (title, description, image_title) - - -if __name__ == "__main__": - title, desc, image_title = random_anime_character() - print(f"{title}\n\n{desc}\n\nImage saved : {image_title}") From 4af41d673a40c44073a7f87798443109e06dee88 Mon Sep 17 00:00:00 2001 From: ashish-patwal Date: Mon, 19 Jul 2021 11:06:59 +0530 Subject: [PATCH 4/7] more tests passed --- web_programming/random_anime_character.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/web_programming/random_anime_character.py b/web_programming/random_anime_character.py index dc19b9496443..0502ff679f97 100644 --- a/web_programming/random_anime_character.py +++ b/web_programming/random_anime_character.py @@ -1,9 +1,9 @@ import os + import requests from bs4 import BeautifulSoup from fake_useragent import UserAgent - headers = {"UserAgent": UserAgent().random} URL = "https://www.mywaifulist.moe/random" @@ -16,6 +16,8 @@ def save_image(image_url: str, image_title: str) -> None: with open(image_title, "wb") as file: file.write(image.content) + return None + def random_anime_character() -> None: """ From 034108375efab5ea2bba0c142b9ce771f74a78d7 Mon Sep 17 00:00:00 2001 From: Lucifer <63491234+ashish-patwal@users.noreply.github.com> Date: Mon, 19 Jul 2021 15:36:49 +0530 Subject: [PATCH 5/7] type hint updated Co-authored-by: Christian Clauss --- web_programming/random_anime_character.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_programming/random_anime_character.py b/web_programming/random_anime_character.py index 0502ff679f97..cb78b04e48a4 100644 --- a/web_programming/random_anime_character.py +++ b/web_programming/random_anime_character.py @@ -21,7 +21,7 @@ def save_image(image_url: str, image_title: str) -> None: def random_anime_character() -> None: """ - Returns the Name and Description of the anime character . + Returns the Title, Description, and Image Title of a random anime character . """ html = requests.get(URL, headers=headers).text From 5375cba5b85a82eb822730eab8dfb77db0237fc6 Mon Sep 17 00:00:00 2001 From: Lucifer <63491234+ashish-patwal@users.noreply.github.com> Date: Mon, 19 Jul 2021 15:37:20 +0530 Subject: [PATCH 6/7] type hint updated again Co-authored-by: Christian Clauss --- web_programming/random_anime_character.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_programming/random_anime_character.py b/web_programming/random_anime_character.py index cb78b04e48a4..7cc28886455d 100644 --- a/web_programming/random_anime_character.py +++ b/web_programming/random_anime_character.py @@ -19,7 +19,7 @@ def save_image(image_url: str, image_title: str) -> None: return None -def random_anime_character() -> None: +def random_anime_character() -> tuple[str, str, str]: """ Returns the Title, Description, and Image Title of a random anime character . """ From f7b7da1f48fa7a5e928866ec0374281afffa6f24 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 19 Jul 2021 17:34:47 +0200 Subject: [PATCH 7/7] Update random_anime_character.py --- web_programming/random_anime_character.py | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/web_programming/random_anime_character.py b/web_programming/random_anime_character.py index 7cc28886455d..f15a9c05d9e5 100644 --- a/web_programming/random_anime_character.py +++ b/web_programming/random_anime_character.py @@ -16,27 +16,19 @@ def save_image(image_url: str, image_title: str) -> None: with open(image_title, "wb") as file: file.write(image.content) - return None - def random_anime_character() -> tuple[str, str, str]: """ Returns the Title, Description, and Image Title of a random anime character . """ - - html = requests.get(URL, headers=headers).text - soup = BeautifulSoup(html, "html.parser") - + soup = BeautifulSoup(requests.get(URL, headers=headers).text, "html.parser") title = soup.find("meta", attrs={"property": "og:title"}).attrs["content"] image_url = soup.find("meta", attrs={"property": "og:image"}).attrs["content"] description = soup.find("p", id="description").get_text() - _, image_extension = os.path.splitext(os.path.basename(image_url)) image_title = title.strip().replace(" ", "_") image_title = f"{image_title}{image_extension}" - save_image(image_url, image_title) - return (title, description, image_title)