add environment variable support for configuration

This commit is contained in:
Антон Скалкин 2025-02-18 17:40:36 +04:00
parent 9b3fd192bc
commit 3f7300ef67
3 changed files with 13 additions and 6 deletions

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
datetime.json datetime.json
.env
# ---> Python # ---> Python
# Byte-compiled / optimized / DLL files # Byte-compiled / optimized / DLL files

17
main.py
View File

@ -1,19 +1,23 @@
import json import json
import os
from datetime import datetime, timedelta from datetime import datetime, timedelta
import pyodbc import pyodbc
import requests import requests
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from dotenv import load_dotenv
load_dotenv() # take environment variables from .env.
messageTo = {'sam': '331491581', 'is': '-1001637207513'} messageTo = os.getenv('MESSAGE_IS')
driverFrom = {'lxc': '/opt/microsoft/msodbcsql18/lib64/libmsodbcsql-18.0.so.1.1', 'windows': '{ODBC Driver 17 for SQL Server}'} driverFrom = {'lxc': '/opt/microsoft/msodbcsql18/lib64/libmsodbcsql-18.0.so.1.1', 'windows': '{ODBC Driver 17 for SQL Server}'}
bot_token = os.getenv('TELEGRAM_BOT_TOKEN')
server = 'sis.ustu\SQLEXPRESS' server = os.getenv('DB_SERVER')
database = 'DepartmentDatabaseContext' database = os.getenv('DB_DATABASE')
username = 'tgbot' username = os.getenv('DB_USERNAME')
password = 'kafISbot' password = os.getenv('DB_PASSWORD')
cnxn = pyodbc.connect( cnxn = pyodbc.connect(
'DRIVER=' + driverFrom.get('lxc') + ';SERVER=' + server + ';' + 'DRIVER=' + driverFrom.get('lxc') + ';SERVER=' + server + ';' +
'DATABASE=' + database + ';UID=' + username + ';PWD=' + password + ';Encrypt=no') 'DATABASE=' + database + ';UID=' + username + ';PWD=' + password + ';Encrypt=no')
@ -39,8 +43,9 @@ def main():
textNews = textNews[:3000] + '...' textNews = textNews[:3000] + '...'
params = {'chat_id': messageTo.get('is'), params = {'chat_id': messageTo.get('is'),
'text': f'{row[1]}\n{textNews}\nhttp://is.ulstu.ru/News/ShowNews/{row[0]}'} 'text': f'{row[1]}\n{textNews}\nhttp://is.ulstu.ru/News/ShowNews/{row[0]}'}
requests.get('https://api.telegram.org/bot5567223643:AAG6DYNUNq7BNqm7-pI2p-SdvEmAKielViE/sendMessage', requests.get(f'https://api.telegram.org/bot{bot_token}/sendMessage',
params=params) params=params)
date = row[3] date = row[3]
row = cursor.fetchone() row = cursor.fetchone()

View File

@ -1,3 +1,4 @@
bs4 bs4
pyodbc pyodbc
requests requests
python-dotenv