PYTHON MONEY
1.74K subscribers
64 photos
11 videos
10 files
21 links
Earn money. Save time. πŸ’°β³
Download Telegram
script_writer.py
252 B
πŸ”₯10πŸ‘2
The creation of the skeleton for our classes is now complete.

Following the same principle, create the structure for the remaining child classes, changing only the class name.

Next, we'll move on to developing the bot's launch logic.
πŸ”₯16⚑3πŸ‘2πŸ‘Œ2
In the main.py file, we will implement the management of different operation modes for the bot.

Here's what we'll do:

1. Import our classes.
2. Add functions to run individual steps, each of which will create an instance of the corresponding class and call its execute method.
3. Add a function to run the full pipeline, which will sequentially execute all the steps.
4. Create a menu to select the bot's operation mode.
πŸ”₯11
Let’s implement this.

First, we'll import the classes. Then, for each class, we’ll create a function that accepts the project_folder argument.

In the function, we will create an instance of the class and pass the project_folder parameter to the class constructor. After that, we will call the execute method on this object.

* I will provide the code a bit later
πŸ”₯15πŸ‘1
Note that instead of importing all classes globally, we can move the imports inside their respective functions.

This will speed up the bot’s startup and reduce memory usage in situations where not all classes are needed.

For example, if we only need to run one mode, the bot won’t import all other classes.
πŸ”₯14
After declaring functions for each mode, we'll define a function that runs the full pipeline.

This function will sequentially execute all steps by calling the previously defined functions one by one.
πŸ”₯9❀‍πŸ”₯2
Next, we'll declare the main function.

In this function, we'll declare a variable project_folder and assign it the project folder name.

We'll also create a menu here to choose the bot's operation mode.

We can implement this in two ways:

1. By parsing command-line arguments (argparse), allowing the bot to be launched from the command line.
2. By using a simple numeric input in the console.

We'll go with the simpler second option for now. I'll demonstrate the first option separately later.
πŸ”₯10πŸ‘1
So, let's first print the selection menu, and then get the user's choice using input.
❀‍πŸ”₯11
Then, we'll run the corresponding mode based on the user's choice.
πŸ”₯12
All that's left is to create the entry point ifname at the bottom of the file, with a call to the main() function.

This line ensures that the main() function is executed only when this file is run as the main program, not when it's imported as a module.
πŸ”₯13
main.py
2.6 KB
❀‍πŸ”₯11
Example of bot control implementation via command line argument parsing (argparse)

How it works:

1. argparse is used to get the mode argument from the command line. This allows the user to choose which mode to run.

2. Depending on the value of args.mode, the corresponding functions are called (e.g., run_script_writer, run_script_divider, etc.).

3. Run it through the console, providing the required mode (e.g., full): python main.py full
πŸ‘18πŸ”₯1
The development of the bot control logic is now complete.
πŸ‘14πŸ”₯1
You can now run the bot in any mode and check if the constructor of the base class works.

After running, the project folders should be created automatically. This means that everything is working correctly.

You can create folders for another project by assigning a new value to the project_folder variable in the main function of the main.py file, and then run the bot again.

* Run the main.py file.
πŸ”₯14❀‍πŸ”₯4
As we finish working on the bot’s basis, let’s think about what we haven’t taken care of yet.

Storing API keys and other project settings.

To address this, we will follow the standard practice and create a config.py file to store such data.

In this file, we will later create a settings class, where we will store these data in its attributes.

We will read the API keys from environment variables. This approach protects the data from accidentally being exposed in the code and repository.
πŸ”₯21
So, let's create two files in the root of the project:

1. config.py - this will contain the settings class
2. .env - this is where we'll store API keys

To easily work with environment variables, we'll use the python-dotenv library. Install it via the terminal:

pip install python-dotenv
πŸ”₯11❀2
In the .env file, we'll place the necessary API keys (e.g., for OpenAI, Elevenlabs, Pexels) in the following format:

API_KEY_1=your_api_key_1

Next, we'll load these variables from the file using dotenv.
πŸ”₯13
Next, in the config.py file:

1. Import load_dotenv.
2. Call the load_dotenv() function to load variables from the .env file.
3. Declare a Config class, where we will store API keys and other settings in its attributes.
4. Declare class attributes corresponding to the required API keys and retrieve their values using os.getenv.
5. Additionally, you can store other settings, such as video parameters, in the attributes of this class.

* Pay attention to this class. Since this class is only needed for storing data, there's no need to create a class constructor here; we can simply store the data in class attributes, as we have done in this case.

Also, note that attributes created outside the class constructor (the init method) are called class attributes, not instance attributes. Class attributes are the same for all instances of the class (objects), unlike instance attributes, which are created in the constructor and can be unique for each object.
πŸ”₯13
Now, to use these parameters in other parts of the code, simply import the Config class and access its attributes.

For example, we import this class in the main.py file, retrieve the value of the Config.OPENAI_API_KEY attribute, and pass it to the ScriptWriter class constructor when creating an object.

* Note that in this case, we are accessing the attributes of the Config class directly without creating an instance of this class: Config.OPENAI_API_KEY

This is possible because OPENAI_API_KEY is a class attribute in the Config class, and we can access it through the class itself rather than through an instance.

However, we can also create an object and then access the class attributes through the object:

config = Config()
api_key = config.OPENAI_API_KEY


But if the OPENAI_API_KEY attribute were declared in the constructor of the Config class, we would only be able to access it through an instance (object) of the class.
πŸ”₯12πŸ‘1
config.py
363 B
πŸ‘12