# AUTHOR: 0-0
# DATE: 7/5/2019
# DESCRIPTION: Scans a website for open directories.

import requests
import re

# Checks For Open Directories.
# Precondition: A String.
# Postcondition: Saves Links to File.


def search(raw_url):

    # Local Variable: User-agent
    try:

        user_agent = {"User-Agent":
                      "Mozilla/5.0 (X11; Linux x86_64)\
                     AppleWebKit/536.5 (KHTML, like Gecko)\
                     Chrome/19.0.1084.9 Safari/536.5"}

        # Parses Input (Input Validation)
        raw_url = raw_url.split(":")
        base_url, port = "{}://{}".format(raw_url[0],
                                          raw_url[1][2:]), ":" + raw_url[2]

        # Check If Valid Website.
        if requests.get(base_url + port,
                        headers=user_agent).status_code == 200:

            # Open & Store Test Directories.
            with open("paths.txt", "r") as reader:
                paths = tuple(reader.read().split("\n"))

            # Open & Write Data.
            with open("Directories.txt", "w") as save:
                save.write("\nScan Website: {}\n\n".format(base_url))
                del raw_url, port

                # Test & Write If Found.
                for path in paths:

                    request = base_url + "/" + path
                    response = requests.get(request, headers=user_agent)
                    title = str(re.findall(
                        r'<title.*?>(.+?)</title>', str(response.content)))

                    if title:
                        if response.status_code == 200 and title.find(
                                "404") == -1:
                            save.write(request + "\n")

    except (KeyboardInterrupt, Exception) as e:
        print(str(e))


# Runs Program.
# Precondition: "http(s)://www.example.com:PORT"
# Postcondition: Saves Found Links.
search("")

# AUTHOR: 0-0
# DATE: 7/5/2019
# DESCRIPTION: Scans a website for open directories.

import requests
import re

# Checks For Open Directories.
# Precondition: A String.
# Postcondition: Saves Links to File.


def search(raw_url):

    # Local Variable: User-agent
    try:

        user_agent = {"User-Agent":
                      "Mozilla/5.0 (X11; Linux x86_64)\
                     AppleWebKit/536.5 (KHTML, like Gecko)\
                     Chrome/19.0.1084.9 Safari/536.5"}

        # Parses Input (Input Validation)
        raw_url = raw_url.split(":")
        base_url, port = "{}://{}".format(raw_url[0],
                                          raw_url[1][2:]), ":" + raw_url[2]

        # Check If Valid Website.
        if requests.get(base_url + port,
                        headers=user_agent).status_code == 200:

            # Open & Store Test Directories.
            with open("paths.txt", "r") as reader:
                paths = tuple(reader.read().split("\n"))

            # Open & Write Data.
            with open("Directories.txt", "w") as save:
                save.write("\nScan Website: {}\n\n".format(base_url))
                del raw_url, port

                # Test & Write If Found.
                for path in paths:

                    request = base_url + "/" + path
                    response = requests.get(request, headers=user_agent)
                    title = str(re.findall(
                        r'<title.*?>(.+?)</title>', str(response.content)))

                    if title:
                        if response.status_code == 200 and title.find(
                                "404") == -1:
                            save.write(request + "\n")

    except (KeyboardInterrupt, Exception) as e:
        print(str(e))


# Runs Program.
# Precondition: "http(s)://www.example.com:PORT"
# Postcondition: Saves Found Links.
search("")

# AUTHOR: 0-0 # DATE: 7/5/2019 # DESCRIPTION: Scans a website for open directories. # Checks For Open Directories. # Precondition: A String. # Postcondition: Saves Links to File. # Local Variable: User-agent # Parses Input (Input Validation) # Check If Valid Website. # Open & Store Test Directories. # Open & Write Data. # Test & Write If Found. # Runs Program. # Precondition: "http(s)://www.example.com:PORT" # Postcondition: Saves Found Links.