Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ascii art code correction suggestion #4

Open
HenraL opened this issue Aug 24, 2022 · 0 comments
Open

Ascii art code correction suggestion #4

HenraL opened this issue Aug 24, 2022 · 0 comments

Comments

@HenraL
Copy link

HenraL commented Aug 24, 2022

Dear @DanCRichards,
Having recently found your program about ascii generation, I found that it was a great Idea.
I thus took a look into you code (to try and understand how it worked) and was surprised about it's build quality.
I thus re-wrote the code and it supports local and remote image fetching, the next three bullets describes the reaction of to code depending on it's situation:

  • If the computer has an active internet connection the file will be retrieved from your repository
  • If the computer does not have an active internet connection, the file will be retrieved from a defined folder
  • If the two options above fail, an error message will be displayed (Sorry no images today)

In order to run the file, it requires execution rigths and/or python3

I hope you like this new version of the script.
Yours sincerely
Henry Letellier

#!/bin/env python3
import random
import os
import requests

default_art = """
 #!/bin/env python3
##
## EPITECH PROJECT, 2022
## ASCII-Art-Splash-Screen
## File description:
## ascii_revisited.py
##

# Code written by (c) Henry Letellier (please leave this in the file)

import random
import os
import requests

default_art = """
  _________
 /   _____/ __________________ ___.__.
 \\_____  \\ /  _ \\_  __ \\_  __ <   |  |
 /        (  <_> )  | \\/|  | \\/\\___  |
/_______  /\\____/|__|   |__|   / ____|
        \\/                     \\/
            _______
            \\      \\   ____
            /   |   \\ /  _ \\
           /    |    (  <_> )
           \\____|__  /\\____/
                   \\/
                       .__
                       |__| _____ _____     ____   ____   ______
                       |  |/     \\\\__  \\   / ___\\_/ __ \\ /  ___/
                       |  |  Y Y  \\/ __ \\_/ /_/  >  ___/ \\___ \\ 
                       |__|__|_|  (____  /\\___  / \\___  >____  >
                                \\/     \\//_____/      \\/     \\/ 
                             __             .___
                           _/  |_  ____   __| _/____  ___.__.   
                           \\   __\\/  _ \\ / __ |\\__  \\<   |  |
                            |  | (  <_> ) /_/ | / __ \\\\___  |   
                            |__|  \\____/\\____ |(____  / ____|   
                                             \\/     \\/\\/
"""

def get_file_content(filename:str, location:str) -> str:
    f = open(f"{location}/{filename}", "r", encoding="utf-8")
    content = f.read()
    f.close()
    return content

def display(the_file_content:str) -> None:
    print(the_file_content)

def get_file_online(url_start:str, url_end:str, int_max:int) -> str:
    content = requests.get(f"{url_start}{str(random.randint(1,int_max))}{url_end}")
    if content.status_code == 200:
        raw_file_content = content.content
        decoded_content = raw_file_content.decode()
        if "</html>" not in decoded_content:
            return decoded_content
        else:
            return ""
    else:
        return ""

usr_location = os.getcwd()
file_url_start = 'https://raw.githubusercontent.com/DanCRichards/ASCII-Art-Splash-Screen/master/art/'
file_url_end = ".txt"
max_nb_files = 9
file_content = get_file_online(file_url_start, file_url_end, max_nb_files)
if file_content == "":
    try:
        program_location = f"{os.environ['HOME']}/manually_installed_software/ascii_in_terminal/ASCII-Art-Splash-Screen/"
        # Change the path of program_location in order to set tge repository containing the folder with the ascii art folder
        art_location = "art"
        # Change the content of the variable art_location in order to change the name of the folder containing the art files
        os.chdir(program_location)
        art_files = os.listdir(art_location)
        art_file = random.randint(1, len(art_files)-1)
        file_name = art_files[art_file]
        file_content = get_file_content(file_name, art_location)
        os.chdir(usr_location)
    except:
        file_content = default_art

display(file_content)

PS: I would appreciate it if you leave my credit in the source code
PPS: why not add a file containing the number of available ascii images (avoid's having to hard code it in case you decide to add more images one day, you just have to change the number in the file, no need to roll out a new code for everyone)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant