import bs4
import os
import prettytable
import requests

def select_manga():
    # Get manga list page
    manga_list_url = "http://mangastream.com/manga"
    res = requests.get(manga_list_url)
    res.raise_for_status()
    
    # Convert to soup for parsing
    manga_list_soup = bs4.BeautifulSoup(res.text, "html.parser")

    # Get list of manga and print them
    manga_list = manga_list_soup.select("td a")[::2]
    manga_table = prettytable.PrettyTable(["Index", "Manga"])
    for idx, manga in enumerate(manga_list):
        manga_table.add_row([str(idx + 1), manga.getText()])
    print(manga_table)

    # User select manga
    while True:
        chosen_manga_idx = int(input("\nSelect the manga you want to download.\nPlease input the index on the left.\n")) - 1
        if chosen_manga_idx >= 0 and chosen_manga_idx < len(manga_list):
            break
        else:
            print("Out of range. Index must be between 1 and " + str(len(manga_list)) + ".")
    chosen_manga = manga_list[chosen_manga_idx]
    print("\nYou have chosen " + chosen_manga.getText() + ".")
    
    # Create manga folder
    os.makedirs(chosen_manga.getText(), exist_ok=True)

    return chosen_manga

def download_manga(chosen_manga):
    # Get chapter list page
    chapter_list_url = chosen_manga.get("href")
    res = requests.get(chapter_list_url)
    res.raise_for_status()
    
    # Convert to soup for parsing
    chapter_list_soup = bs4.BeautifulSoup(res.text, "html.parser")

    # Get list of chapters and print them
    chapter_list = chapter_list_soup.select("td a")
    chapter_table = prettytable.PrettyTable(["Index", "Chapter"])
    for idx, chapter in enumerate(chapter_list):
        chapter_table.add_row([str(idx + 1), chapter.getText()])
    print(chapter_table)

    # User select chapters
    print("\nSelect the chapters you want to download.\nPlease input the index on the left.")
    
    # User select starting chapter
    while True:
        start_chapter = int(input("\nSelect the starting chapter.\n")) - 1
        if start_chapter >= 0 and start_chapter < len(chapter_list):
            break
        else:
            print("Out of range. Index must be between 1 and " + str(len(chapter_list)) + ".")

    # User select ending chapter
    while True:
        end_chapter = int(input("\nSelect the ending chapter.\n")) - 1
        if end_chapter >= start_chapter and end_chapter < len(chapter_list):
            break
        else:
            print("Out of range. Index must be between " + str(start_chapter + 1) + " and " + str(len(chapter_list)) + ".")
    print()

    # Go through each chapter
    chosen_chapters = chapter_list[start_chapter:end_chapter+1]
    for chapter in chosen_chapters:
        print("Downloading " + chapter.getText() + " ", end="", flush=True)

        # Create chapter folder
        os.makedirs(os.path.join(chosen_manga.getText(), chapter.getText()), exist_ok=True)
        
        # Get chapter's first page
        chapter_url = chapter.get("href")
        res = requests.get(chapter_url)
        res.raise_for_status()
    
        # Convert to soup for parsing
        page_soup = bs4.BeautifulSoup(res.text, "html.parser")

        # Initialize page number
        page_number = 1

        # Go through each page of a chapter
        while True:
            print(".", end="", flush=True)

            # Get source image page
            img_url = "http:" + page_soup.select("#manga-page")[0].get("src")
            res = requests.get(img_url)
            res.raise_for_status()

            # Stringify page number
            if page_number < 10:
                page_number_string = "00" + str(page_number)
            elif page_number < 100:
                page_number_string = "0" + str(page_number)
            else:
                page_number_string = str(page_number)

            # Get image file extension
            img_extension = img_url.rsplit(".", 1)[1]

            # Download image
            imageFile = open(os.path.join(chosen_manga.getText(), chapter.getText(), page_number_string + "." + img_extension), 'wb')
            for chunk in res.iter_content(100000):
                imageFile.write(chunk)
            imageFile.close()

            # Get next page url
            next_page_url = page_soup.select(".next a")[0].get("href")

            # Are we at the last page? If so, go to the next chapter
            if next_page_url[-3:] == "tip" or next_page_url.rsplit('/', 1)[0] != chapter_url.rsplit('/', 1)[0]:
                print()
                break

            # Get next page
            res = requests.get(next_page_url)
            res.raise_for_status()

            # Convert to soup for parsing
            page_soup = bs4.BeautifulSoup(res.text, "html.parser")

            # Increment page number
            page_number += 1

def main():
    manga = select_manga()
    download_manga(manga)

if __name__ == "__main__":
    main()
@@ -148,24 +148,35 @@
 /manga%22%0A    
+manga_list_
 res = reques
@@ -194,32 +194,43 @@
 a_list_url)%0A    
+manga_list_
 res.raise_for_st
@@ -307,32 +307,43 @@
 4.BeautifulSoup(
+manga_list_
 res.text, %22html.
@@ -1332,24 +1332,37 @@
 %22href%22)%0A    
+chapter_list_
 res = reques
@@ -1386,24 +1386,37 @@
 st_url)%0A    
+chapter_list_
 res.raise_fo
@@ -1499,32 +1499,45 @@
 4.BeautifulSoup(
+chapter_list_
 res.text, %22html.
@@ -3078,32 +3078,37 @@
 %22href%22)%0A        
+page_
 res = requests.g
@@ -3123,32 +3123,37 @@
 er_url)%0A        
+page_
 res.raise_for_st
@@ -3232,32 +3232,37 @@
 4.BeautifulSoup(
+page_
 res.text, %22html.
@@ -3555,32 +3555,36 @@
 c%22)%0A            
+img_
 res = requests.g
@@ -3599,32 +3599,36 @@
 rl)%0A            
+img_
 res.raise_for_st
@@ -4071,20 +4071,19 @@
       im
-ageF
+g_f
 ile = op
@@ -4215,16 +4215,20 @@
 hunk in 
+img_
 res.iter
@@ -4259,28 +4259,27 @@
           im
-ageF
+g_f
 ile.write(ch
@@ -4301,12 +4301,11 @@
   im
-ageF
+g_f
 ile.
@@ -4685,16 +4685,21 @@
         
+page_
 res = re
@@ -4736,16 +4736,21 @@
         
+page_
 res.rais
@@ -4849,16 +4849,21 @@
 fulSoup(
+page_
 res.text

Rename variables