Skip to content
/ gptauto Public

A fully functional ChatGPT selenium scraper written in Python

License

Notifications You must be signed in to change notification settings

st1vms/gptauto

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gptauto

What is this?

This Python scraper provides access to the conversational capabilities of ChatGPT through a simple chat messaging interface.

While not officially supported by OpenAI, this library can enable interesting conversational applications.

It allows for:

  • Creating chat sessions with ChatGPT and getting chat IDs.
  • Sending messages to specific chat ids, and even toggle chat history.
  • Get an ordered, strong typed list of messages from any chat.

It relies on geckodriver, selenium-wire and selgym (selenium) libraries only.

Table of Content

Installation

Download this repository and install it from source by runnig this command inside the repository folder:

pip install -e .

Uninstallation

pip uninstall gptauto

Requirements

  • Python >= 3.10
  • geckodriver installed in a folder registered in PATH
  • A valid login to ChatGPT on a newly created Firefox profile (A new Firefox profile is needed in order for selenium-wire proxy to work).
  • Disabling ChatGPT's chat history on this profile is also recommended.

Example usage

ask-gpt

By installing this library using pip, a command-line interface is provided, ask-gpt, it takes one positional argument, the input TEXT for chat-gpt, that can also be prepended with an header (prompt) using the -p string parameter.

Other parameters are:

-f Firefox profile
(Firefox root profile path string, default behaviour is creating a new one)

-ts Type speed
(Maximum type speed expressed as a float number >= 0.001, defaults to 0.01)
This value is used to randomly sleep for a specific interval between each individual key-stroke.

-nh No-Headless
(Turn off selenium Headless mode, for debugging purposes)

This utility is also pipe friendly, having the ability to pipe input strings as the Chat GPT input directly into the command.

For example in Linux distributions:

echo "Roll a D20" | ask-gpt -p "Only answer with a number" | cat "output.txt"

Or in Windows CMD:

( echo "Roll a D20" & ask-gpt -p "Only answer with a number" ) >> output.txt

Using Python

from gptauto.scraper import GPTScraper

# Set to None to use default firefox profile
# Set to a string with the root profile directory path
# to use a different firefox profile
PROFILE_PATH = None


def _main() -> None:

    scraper = GPTScraper(profile_path=PROFILE_PATH)
    try:
        # Creates a new webdriver instance
        # opening chatgpt page
        scraper.start()

        # Pick text to send
        text = input("\nText\n>>").strip()
        scraper.send_message(text)

        # Waits for completion to finish
        scraper.wait_completion()

        # Retrieves chat messages
        # as an ordered list of AssistantMessage
        # or UserMessage
        messages = list(scraper.get_messages())
        if messages:
            print(f"\nANSWER:\n\n{messages[-1].text}\n")
    except KeyboardInterrupt:
        return
    finally:
        # Gracefully quit the webdriver instance
        scraper.quit()
        # After calling quit()
        # a new session can be started with .start()


if __name__ == "__main__":
    _main()

Toggling chat history

from gptauto.scraper import GPTScraper

# Open new session on default firefox profile
scraper = GPTScraper()
try:
    scraper.start()

    # Toggle chat history
    # If On -> Off
    # If Off -> On
    scraper.toggle_history()
finally:
    scraper.quit()

Known bugs

  • Sometimes a captcha may appear after sending a message, so far this software does nothing to prevent this or act accordingly, if a captcha is triggered, the only current solution is to solve it manually, having non-headless behavior set by default.

  • Currently this software does not work in headless mode, I don't know if I will ever be able to find a solution in the near future. Feel free to open a pull request if you found a better one and would like to contribute 🦾

Disclaimer

This repository provides a way for automating free accounts on ChatGPT. Please note that this software is not endorsed, supported, or maintained by OpenAI. Use it at your own discretion and risk. OpenAI may make changes to their official product or APIs at any time, which could affect the functionality of this software. We do not guarantee the accuracy, reliability, or security of the information and data retrieved using this API. By using this repository, you agree that the maintainers are not responsible for any damages, issues, or consequences that may arise from its usage. Always refer to OpenAI's official documentation and terms of use. This project is maintained independently by contributors who are not affiliated with OpenAI.

Donations

A huge thank you in advance to anyone who wants to donate :)

Buy Me a Pizza