-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixing multiple issues with the database by using docker compose
- Loading branch information
1 parent
64e5c5c
commit 73aba00
Showing
47 changed files
with
530 additions
and
339 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,8 @@ | ||
FROM python:3.7-alpine | ||
RUN apk add --no-cache gcc musl-dev | ||
RUN apk update && apk upgrade && \ | ||
apk add git alpine-sdk bash python | ||
RUN mkdir /usr/informer | ||
WORKDIR /usr/informer | ||
COPY . /usr/informer | ||
apk add git alpine-sdk bash python3 | ||
COPY app /usr/local/app | ||
WORKDIR /usr/local/app | ||
RUN pip3 install -r requirements.txt | ||
|
||
# Lets set the environment variable in the container | ||
ENV GAE_INSTANCE=prod | ||
|
||
RUN pip install -I Jinja2==2.10.3 | ||
RUN pip install -I SQLAlchemy==1.3.11 | ||
RUN pip install -I Werkzeug==0.16.0 | ||
RUN pip install -I pytz==2019.3 | ||
RUN pip install -I sqlalchemy-migrate==0.13.0 | ||
RUN pip install -I requests==2.7.0 | ||
RUN pip install -I Flask==1.1.1 | ||
RUN pip install -I Telethon==1.10.8 | ||
RUN pip install -I mysql-connector-python==8.0.18 | ||
RUN pip install -I gspread==3.1.0 | ||
RUN pip install -I oauth2client==4.1.3 | ||
|
||
# Comment this out if you plan to run the script inside docker with ENTRYPOINT. Replace 1234567 with your Telegram API user ID | ||
CMD ["python","bot.py","1234567"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import sys | ||
import os | ||
import logging | ||
from dotenv import load_dotenv | ||
from pathlib import Path | ||
|
||
# Lets set the logging level | ||
logging.getLogger().setLevel(logging.INFO) | ||
|
||
# ----------------- | ||
# Load the ENV file | ||
# ----------------- | ||
env_file = 'informer.env' if os.path.isfile('informer.env') else '../informer.env' | ||
logging.info(f'env_file: {env_file}') | ||
dotenv_path = Path(env_file) | ||
load_dotenv(dotenv_path=dotenv_path) | ||
|
||
from informer import TGInformer | ||
|
||
|
||
# =========== | ||
# Quick setup | ||
# =========== | ||
|
||
# virtualenv venv | ||
# source venv/bin/activate | ||
# pip install -r requirements.txt | ||
# python3 informer.py <account_id> | ||
|
||
# Read more: https://github.com/paulpierre/informer/ | ||
|
||
try: | ||
account_id = sys.argv[1] | ||
except: | ||
raise Exception('informer.py <account_id> - account_id is a required param') | ||
|
||
if not account_id: | ||
raise Exception('Account ID required') | ||
|
||
if __name__ == '__main__': | ||
|
||
informer = TGInformer( | ||
db_database = os.environ['MYSQL_DATABASE'], | ||
db_user = os.environ['MYSQL_USER'], | ||
db_password = os.environ['MYSQL_PASSWORD'], | ||
db_ip_address = os.environ['MYSQL_IP_ADDRESS'], | ||
db_port = os.environ['MYSQL_PORT'], | ||
tg_account_id = os.environ['TELEGRAM_ACCOUNT_ID'], | ||
tg_notifications_channel_id = os.environ['TELEGRAM_NOTIFICATIONS_CHANNEL_ID'], | ||
google_credentials_path = os.environ['GOOGLE_APPLICATION_CREDENTIALS'], | ||
google_sheet_name = os.environ['GOOGLE_SHEET_NAME'] | ||
) | ||
informer.init() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,22 @@ | ||
from models import Account, Channel, ChatUser, Keyword, Message, Monitor, Notification | ||
import sqlalchemy as db | ||
|
||
import csv | ||
from datetime import datetime | ||
import sys | ||
import os | ||
import logging | ||
from dotenv import load_dotenv | ||
from pathlib import Path | ||
import sqlalchemy as db | ||
from datetime import datetime | ||
from sqlalchemy.orm import sessionmaker | ||
from models import Account, Channel, ChatUser, Keyword, Message, Monitor, Notification | ||
logging.getLogger().setLevel(logging.INFO) | ||
|
||
# ----------------- | ||
# Load the ENV file | ||
# ----------------- | ||
dotenv_path = Path('informer.env') | ||
load_dotenv(dotenv_path=dotenv_path) | ||
|
||
Session = None | ||
session = None | ||
SERVER_MODE = None | ||
|
@@ -19,7 +28,7 @@ | |
|
||
def init_db(): | ||
global session, SERVER_MODE, engine | ||
logging.info('{}: Initializing the database'.format(sys._getframe().f_code.co_name)) | ||
logging.info(f'{sys._getframe().f_code.co_name}: Initializing the database') | ||
Account.metadata.create_all(engine) | ||
ChatUser.metadata.create_all(engine) | ||
Channel.metadata.create_all(engine) | ||
|
@@ -34,6 +43,7 @@ def init_db(): | |
Lets setup the channels to monitor in the database | ||
""" | ||
def init_data(): | ||
|
||
global session, SERVER_MODE, engine | ||
session = Session() | ||
init_add_account() | ||
|
@@ -43,22 +53,24 @@ def init_data(): | |
session.close() | ||
|
||
def init_add_account(): | ||
|
||
global session, SERVER_MODE, engine | ||
logging.info('{}: Adding bot account'.format(sys._getframe().f_code.co_name)) | ||
|
||
BOT_ACCOUNTS = [ | ||
logging.info(f'{sys._getframe().f_code.co_name}: Adding bot account') | ||
|
||
BOT_ACCOUNTS = [ | ||
|
||
Account( | ||
account_id=1234567, # Insert your own Telegram API ID here | ||
account_api_id=1234567, # Insert your own Telegram API ID here | ||
account_api_hash='21b277e0daa5911b0f2616b8b669533c', # Insert your own Telegram API Hash here | ||
account_id=os.environ['TELEGRAM_ACCOUNT_ID'], | ||
account_api_id=os.environ['TELEGRAM_API_APP_ID'], | ||
account_api_hash=os.environ['TELEGRAM_API_HASH'], | ||
account_is_bot=False, | ||
account_is_verified=False, | ||
account_is_restricted=False, | ||
account_first_name='Darrin', | ||
account_last_name='OBrien', | ||
account_user_name='informer', | ||
account_phone='+14151234567', # Enter your burner phone number here | ||
account_first_name=os.environ['TELEGRAM_ACCOUNT_FIRST_NAME'], | ||
account_last_name=os.environ['TELEGRAM_ACCOUNT_LAST_NAME'], | ||
account_user_name=os.environ['TELEGRAM_ACCOUNT_USER_NAME'], | ||
account_phone=os.environ['TELEGRAM_ACCOUNT_PHONE_NUMBER'], # Enter your burner phone number here | ||
account_is_enabled=True, | ||
account_tlogin=datetime.now(), | ||
account_tcreate=datetime.now(), | ||
|
@@ -80,15 +92,15 @@ def init_add_channels(): | |
CHANNELS = [ | ||
{ | ||
'channel_name': 'Informer monitoring', | ||
'channel_id': 1234567, # Enter your own Telegram channel ID for monitoring here | ||
'channel_url': 'https://t.me/joinchat/Blahblahblah', | ||
'channel_is_private': True | ||
'channel_id': os.environ['TELEGRAM_NOTIFICATIONS_CHANNEL_ID'], # Enter your own Telegram channel ID for monitoring here | ||
'channel_url': os.environ['TELEGRAM_NOTIFICATIONS_CHANNEL_URL'], | ||
'channel_is_private': False if os.environ['TELEGRAM_NOTIFICATIONS_CHANNEL_IS_PRIVATE']=='0' else True | ||
}, | ||
|
||
] | ||
|
||
# Lets import the CSV with the channel list | ||
with open('channels.csv') as csv_file: | ||
with open(os.environ['TELEGRAM_CHANNEL_MONITOR_LIST']) as csv_file: | ||
csv_reader = csv.reader(csv_file, delimiter=',') | ||
line_count = 0 | ||
for row in csv_reader: | ||
|
@@ -100,10 +112,11 @@ def init_add_channels(): | |
}) | ||
line_count += 1 | ||
|
||
logging.info('Inserted {} channels to database'.format(line_count)) | ||
|
||
logging.info(f'Inserting {line_count} channels to database') | ||
|
||
for channel in CHANNELS: | ||
logging.info('{}: Adding channel {} to database'.format(sys._getframe().f_code.co_name, channel['channel_name'])) | ||
logging.info(f"{sys._getframe().f_code.co_name}: Adding channel {channel['channel_name']} to database") | ||
|
||
channel_url = channel['channel_url'] if 'channel_url' in channel else None | ||
channel_id = channel['channel_id'] if 'channel_id' in channel else None | ||
|
@@ -159,7 +172,7 @@ def init_add_keywords(): | |
] | ||
|
||
for keyword in KEYWORDS: | ||
logging.info('{}: Adding keyword {} to the database'.format(sys._getframe().f_code.co_name, keyword['keyword_description'])) | ||
logging.info(f"{sys._getframe().f_code.co_name}: Adding keyword {keyword['keyword_description']} to the database") | ||
|
||
session.add(Keyword( | ||
keyword_description=keyword['keyword_description'], | ||
|
@@ -184,7 +197,7 @@ def init_add_monitors(): | |
for channel in channels: | ||
if account_index in accounts: | ||
account = accounts[account_index] | ||
logging.info('{}: Adding monitoring to channel {} with account_id {} to the database'.format(sys._getframe().f_code.co_name, channel.channel_name, account.account_id)) | ||
logging.info(f'{sys._getframe().f_code.co_name}: Adding monitoring to channel {channel.channel_name} with account_id {account.account_id} to the database') | ||
session.add(Monitor( | ||
channel_id=channel.id, | ||
account_id=account.account_id, | ||
|
@@ -200,31 +213,32 @@ def init_add_monitors(): | |
|
||
def initialize_db(): | ||
global session, SERVER_MODE, engine, Session | ||
DATABASE_NAME = 'informer_db' | ||
|
||
# NOTE: you will have to manually add your own DB string connector below | ||
DATABASE_NAME = os.environ['MYSQL_DATABASE'] | ||
|
||
if os.getenv('GAE_INSTANCE'): | ||
SERVER_MODE = 'prod' # prod vs local | ||
MYSQL_CONNECTOR_STRING = 'mysql+mysqlconnector://root:root@YOUR_OWN_IP_HERE:3306' | ||
else: | ||
SERVER_MODE = 'local' | ||
MYSQL_CONNECTOR_STRING = 'mysql+mysqlconnector://root:[email protected]:3306' | ||
db_database = os.environ['MYSQL_DATABASE'] | ||
db_user = os.environ['MYSQL_USER'] | ||
db_password = os.environ['MYSQL_PASSWORD'] | ||
db_ip_address = os.environ['MYSQL_IP_ADDRESS'] | ||
db_port = os.environ['MYSQL_PORT'] | ||
SERVER_MODE = os.environ['ENV'] | ||
MYSQL_CONNECTOR_STRING = f'mysql+mysqlconnector://{db_user}:{db_password}@{db_ip_address}:{db_port}/{db_database}?charset=utf8mb4&collation=utf8mb4_general_ci' | ||
|
||
engine = db.create_engine(MYSQL_CONNECTOR_STRING)#, echo=True) | ||
Session = sessionmaker(bind=engine) | ||
session = None | ||
session = Session() | ||
session.execute("CREATE DATABASE {} CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci';".format(DATABASE_NAME)) | ||
session.close() | ||
engine = db.create_engine('{}/{}?charset=utf8mb4'.format(MYSQL_CONNECTOR_STRING, DATABASE_NAME)) # , echo=True) # uncomment right most comment if you want to hear all the noise MySQL is making | ||
engine = db.create_engine(MYSQL_CONNECTOR_STRING, echo=True) | ||
Session = sessionmaker(bind=engine) | ||
session = None | ||
session = Session() | ||
session.execute(f"CREATE DATABASE IF NOT EXISTS {DATABASE_NAME} CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci';") | ||
session.execute('commit') | ||
#session.close() | ||
|
||
# engine = db.create_engine(f'{MYSQL_CONNECTOR_STRING}?charset=utf8mb4', echo=True) | ||
# Session = sessionmaker(bind=engine) | ||
# session = None | ||
# session = Session() | ||
|
||
# A hack to support unicode for emojis | ||
session.execute('SET NAMES "utf8mb4" COLLATE "utf8mb4_unicode_ci"') | ||
session.execute('ALTER DATABASE {} CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;'.format(DATABASE_NAME)) | ||
session.execute(f'ALTER DATABASE {DATABASE_NAME} CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;') | ||
session.execute('commit') | ||
|
||
init_db() | ||
|
File renamed without changes.
Oops, something went wrong.