Skip to content

Commit

Permalink
Incorporated the config tag in the logger #4
Browse files Browse the repository at this point in the history
  • Loading branch information
drkostas committed May 29, 2022
1 parent 24e3d55 commit 9ec91c1
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 19 deletions.
3 changes: 2 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ See the [issues](https://github.com/drkostas/youbot/issues) too.
- [X] Store comments in dropbox
- [X] \[Merged\] Regularly backup logs files from logs/ to dropbox (for when running on Heroku) + Store errors in sql or dropbox
- [X] Ensure code works without dropbox and emailer modules
- [ ] Add SQL scripts for creating the tables needed
- [ ] Create the workflow for the accumulator
- [ ] Add SQL scripts for creating the tables needed
- [ ] Update Readme
- [ ] Recreate the Livestreaming module
- [ ] Use multiple accounts (different api keys) to check for new comments
- [ ] Improve the YouTube api functions used (Activities api func - https://developers.google.com/youtube/v3/docs/activities/list)
Expand Down
18 changes: 9 additions & 9 deletions confs/commenter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ youtube:
client_secret: !ENV ${CLIENT_SECRET}
api_version: v3
read_only_scope: https://www.googleapis.com/auth/youtube.force-ssl
sleep_time: 1
max_posted_hours: 250 # max num. of hours to check back for posted videos
sleep_time: !ENV ${SLEEP_TIME}
max_posted_hours: !ENV ${MAX_POSTED_HOURS} # max num. of hours to check back for posted videos
type: simulated # normal, simulated
comments:
- config:
local_folder_name: comments
dropbox_folder_name: /yt-commenter/comments
type: local # local, dropbox (should set `cloudstore` config), or mysql (not implemented)
#cloudstore: # Optional
# - config:
# api_key: !ENV ${DROPBOX_API_KEY}
# logs_folder_path: /yt-commenter/logs
# upload_logs_every: 120 # number of loops in commenter()
# type: dropbox
type: !ENV ${COMMENTS_TYPE} # local, dropbox (should set `cloudstore` config), or mysql (not implemented)
cloudstore: # Optional
- config:
api_key: !ENV ${DROPBOX_API_KEY}
logs_folder_path: /yt-commenter/logs
upload_logs_every: !ENV ${UPLOAD_LOGS_EVERY} # number of loops in commenter()
type: dropbox
#emailer: # Not implemented yet
# - config:
# email_address: !ENV ${EMAIL_ADDRESS}
Expand Down
7 changes: 5 additions & 2 deletions youbot/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,15 @@ def main():
Example:
python youbot/run.py -m commenter -c confs/commenter.yml -l logs/commenter.log
"""
global logger

# Initializing
args = get_args()
ColorLogger.setup_logger(log_path=args.log, debug=args.debug, clear_log=False)
# Load the configurations
conf_obj = Configuration(config_src=args.config_file)
tag = conf_obj.tag
logger = ColorLogger(logger_name=f'[{tag}] Main', color='yellow')
you_conf = conf_obj.get_config('youtube')[0]
db_conf = conf_obj.get_config('datastore')[0]
comments_conf = conf_obj.get_config('comments')[0]
Expand All @@ -119,8 +122,8 @@ def main():
# Setup YouTube API
youtube = YoutubeManager(config=you_conf['config'],
db_conf=db_conf, cloud_conf=cloud_conf, comments_conf=comments_conf,
sleep_time=you_conf['sleep_time'],
max_posted_hours=you_conf['max_posted_hours'],
sleep_time=int(you_conf['sleep_time']),
max_posted_hours=int(you_conf['max_posted_hours']),
api_type=you_conf['type'], tag=conf_obj.tag, log_path=args.log)
# Run in the specified run mode
func = globals()[args.run_mode]
Expand Down
4 changes: 3 additions & 1 deletion youbot/youtube_utils/youtube_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from youbot import ColorLogger

logger = ColorLogger('YoutubeApi')
logger = ColorLogger(logger_name='YoutubeApi', color='green')


class AbstractYoutubeApi(ABC):
Expand Down Expand Up @@ -45,6 +45,8 @@ def _get_my_username_and_id(self) -> str:
class YoutubeApiV3(AbstractYoutubeApi):

def __init__(self, config: Dict, tag: str):
global logger
logger = ColorLogger(logger_name=f'[{tag}] YoutubeApi', color='green')
super().__init__(config, tag)

@staticmethod
Expand Down
8 changes: 5 additions & 3 deletions youbot/youtube_utils/youtube_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from youbot import ColorLogger, YoutubeMySqlDatastore, DropboxCloudManager
from .youtube_api import YoutubeApiV3

logger = ColorLogger('YoutubeManager')
logger = ColorLogger(logger_name='YoutubeManager', color='cyan')


class YoutubeManager(YoutubeApiV3):
Expand All @@ -21,13 +21,15 @@ class YoutubeManager(YoutubeApiV3):
def __init__(self, config: Dict, db_conf: Dict, cloud_conf: Dict, comments_conf: Dict,
sleep_time: int, max_posted_hours: int,
api_type: str, tag: str, log_path: str):
self.db = YoutubeMySqlDatastore(config=db_conf['config'])
global logger
logger = ColorLogger(logger_name=f'[{tag}] YoutubeManager', color='cyan')
self.db = YoutubeMySqlDatastore(config=db_conf['config'], tag=tag)
self.comments_conf = comments_conf['config']
self.dbox = None
if cloud_conf is not None:
self.dbox = DropboxCloudManager(config=cloud_conf['config'])
self.dbox_logs_folder_path = cloud_conf['logs_folder_path']
self.upload_logs_every = cloud_conf['upload_logs_every']
self.upload_logs_every = int(cloud_conf['upload_logs_every'])
elif self.comments_conf['type'] == 'dropbox':
raise YoutubeManagerError("Requested `dropbox` comments type "
"but `cloudstore` config is not set!")
Expand Down
8 changes: 5 additions & 3 deletions youbot/yt_mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@
from typing import *
from datetime import datetime

logger = ColorLogger('YoutubeMySqlDatastore')
logger = ColorLogger(logger_name='YoutubeMySqlDatastore', color='red')


class YoutubeMySqlDatastore(HighMySQL):
CHANNEL_TABLE = 'channels'
COMMENTS_TABLE = 'comments'

def __init__(self, config: Dict) -> None:
def __init__(self, config: Dict, tag: str) -> None:
"""
The basic constructor. Creates a new instance of Datastore using the specified credentials
:param config:
:param tag:
"""

global logger
logger = ColorLogger(logger_name=f'[{tag}] YoutubeMySqlDatastore', color='red')
super().__init__(config)
self.create_tables_if_not_exist()

Expand Down

0 comments on commit 9ec91c1

Please sign in to comment.