From 6c66a2ad8a508c3d7bd0591191cadb62acc8913f Mon Sep 17 00:00:00 2001 From: lzzy12 Date: Mon, 9 Dec 2019 15:02:16 +0530 Subject: [PATCH] Read setup variables from environment variables Also Adds support for heroku env variables Signed-off-by: lzzy12 --- .gitignore | 5 +---- README.md | 5 ++--- bot/__init__.py | 8 +++----- bot/config_sample.ini => config_sample.env | 11 +++++------ requirements.txt | 1 + 5 files changed, 12 insertions(+), 18 deletions(-) rename bot/config_sample.ini => config_sample.env (58%) diff --git a/.gitignore b/.gitignore index b865fb2..b651b32 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,4 @@ -*.env -config.ini -*conf.ini -conf.ini +config.env *auth_token.txt *.pyc downloads/* diff --git a/README.md b/README.md index e1a8bb4..8ecdfa3 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ sudo pacman -S docker python ## Setting up config file ``` -cp bot/config_sample.ini bot/config.ini +cp config_sample.env config.env ``` - Remove the first line saying: ``` @@ -87,7 +87,6 @@ sudo docker run mirror-bot ``` python3 generate_drive_token.py ``` -- Change Bot Download Dir to /bot/downloads in config.ini file. - Install [Heroku cli](https://devcenter.heroku.com/articles/heroku-cli) - Login into your heroku account with command: ``` @@ -107,7 +106,7 @@ heroku stack:set container ``` - Add Private Credentials and Config Stuff: ``` -git add -f credentials.json token.pickle ./bot/config.ini +git add -f credentials.json token.pickle config.env ``` - Commit new changes: ``` diff --git a/bot/__init__.py b/bot/__init__.py index 1555825..5f87421 100644 --- a/bot/__init__.py +++ b/bot/__init__.py @@ -1,10 +1,9 @@ import logging -import configparser import aria2p import threading from telegram.ext import Updater -from telegram import Bot import os +from dotenv import load_dotenv import time botStartTime = time.time() @@ -16,12 +15,11 @@ logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s handlers=[logging.FileHandler('log.txt'), logging.StreamHandler()], level=logging.INFO) -config = configparser.ConfigParser() -config.read('bot/config.ini') +load_dotenv('config.env') def getConfig(name: str): - return config['DEFAULT'][name] + return os.environ[name] LOGGER = logging.getLogger(__name__) diff --git a/bot/config_sample.ini b/config_sample.env similarity index 58% rename from bot/config_sample.ini rename to config_sample.env index 9bfbb73..92845b8 100644 --- a/bot/config_sample.ini +++ b/config_sample.env @@ -1,11 +1,10 @@ #Remove this line before deploying _____REMOVE_THIS_LINE_____=True -[DEFAULT] # ENTER BOT TOKEN (Get your BOT_TOKEN by talking to @botfather) -BOT_TOKEN = -GDRIVE_FOLDER_ID = -DOWNLOAD_DIR = /home/username/mirror-bot/downloads -DOWNLOAD_STATUS_UPDATE_INTERVAL = 5 -AUTO_DELETE_MESSAGE_DURATION = 20 +BOT_TOKEN = "" +GDRIVE_FOLDER_ID = "" OWNER_ID = +DOWNLOAD_DIR = "/home/username/mirror-bot/downloads" +DOWNLOAD_STATUS_UPDATE_INTERVAL = 5 +AUTO_DELETE_MESSAGE_DURATION = 20 \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 4a078a0..22817b9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,3 +10,4 @@ uritemplate>=3.0.0 urllib3>=1.23 aria2p mimelib +python-dotenv>=0.10 \ No newline at end of file