Skip to content

Commit

Permalink
Started the high-level functions #4
Browse files Browse the repository at this point in the history
- Renamed YoutubeManagerV3 to YoutubeApiV3
-  child class YoutubeManager that used the mysql class and contains the high-level functions
- Created the add_channel and remove_channel functions and integrated the current functionality into the main.py
  • Loading branch information
drkostas committed Jul 2, 2021
1 parent e160720 commit 0221369
Show file tree
Hide file tree
Showing 7 changed files with 467 additions and 383 deletions.
6 changes: 3 additions & 3 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ See the [issues](https://github.com/drkostas/youbot/issues) too.
- [X] Load starter
- [X] Get channel name automatically
- [X] Build YouTube Manager class
- [X] Create child MySQL class
- [ ] Integrate YoutubeMysql class into the YoutubeManager class
- [ ] Roll the comments for each channel
- [X] Create child MySQL class
- [X] Integrate YoutubeMysql class into the YoutubeManager class
- [ ] Create the workflow for the commenter
- [ ] Roll the comments for each channel
- [ ] Create the workflow for the accumulator
- [ ] Add SQL script for creating the tables needed
- [ ] Recreate the Livestreaming module
Expand Down
2 changes: 1 addition & 1 deletion youbot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from youbot.cloudstore import DropboxCloudstore
from youbot.datastore import YoutubeMySqlDatastore
from youbot.emailer import GmailEmailer
from youbot.youtube_utils import YoutubeManagerV3
from youbot.youtube_utils import YoutubeManager, YoutubeApiV3

__author__ = "drkostas"
__email__ = "georgiou.kostas94@gmail.com"
Expand Down
12 changes: 8 additions & 4 deletions youbot/datastore/mysql_datastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,25 @@ def truncate_table(self, table: str) -> None:
query = "TRUNCATE TABLE {table}".format(table=table)
self.execute_query(query, commit=True)

def insert_into_table(self, table: str, data: dict) -> None:
def insert_into_table(self, table: str, data: dict, if_not_exists: bool = False) -> None:
"""
Inserts into the specified table a row based on a column_name: value dictionary
:param self:
:param table:
:param data:
:param if_not_exists:
:return:
"""

data_str = ", ".join(
list(map(lambda key, val: "{key}='{val}'".format(key=str(key), val=str(val)), data.keys(),
data.values())))

query = "INSERT INTO {table} SET {data}".format(table=table, data=data_str)
if if_not_exists:
ignore = 'IGNORE'
else:
ignore = ''
query = f"INSERT {ignore} INTO {table} SET {data_str}"
self.execute_query(query, commit=True)

def update_table(self, table: str, set_data: dict, where: str) -> None:
Expand Down Expand Up @@ -337,7 +341,7 @@ def add_channel(self, channel_data: Dict) -> None:
""" Insert the provided channel into the database"""

try:
self.insert_into_table(table=self.CHANNEL_TABLE, data=channel_data)
self.insert_into_table(table=self.CHANNEL_TABLE, data=channel_data, if_not_exists=True)
except mysql.connector.errors.IntegrityError as e:
logger.error(f"MySQL error: {e}")

Expand Down
69 changes: 52 additions & 17 deletions youbot/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import traceback
import argparse

from youbot import Configuration, ColorizedLogger, \
DropboxCloudstore, MySqlDatastore, GmailEmailer, YoutubeManagerV3
from youbot import Configuration, ColorizedLogger, YoutubeManager

logger = ColorizedLogger(logger_name='Main', color='yellow')

Expand All @@ -13,6 +12,7 @@ def get_args() -> argparse.Namespace:
Returns:
argparse.Namespace:
"""

parser = argparse.ArgumentParser(
description='A template for python projects.',
add_help=False)
Expand All @@ -27,14 +27,54 @@ def get_args() -> argparse.Namespace:
required_args.add_argument('-l', '--log', required=True, help="Name of the output log file")
# Optional args
optional_args = parser.add_argument_group('Optional Arguments')
optional_args.add_argument('-m', '--run-mode', choices=['run_mode_1', 'run_mode_2', 'run_mode_3'],
default='run_mode_1',
commands = ['commenter', 'accumulator',
'add_channel', 'remove_channel', 'list_channels', 'list_comments',
'refresh_photos']
optional_args.add_argument('-m', '--run-mode', choices=commands,
default=commands[0],
help='Description of the run modes')
optional_args.add_argument('-i', '--id', help="The ID of the YouTube Channel")
optional_args.add_argument('-u', '--username',
help="The Username of the YouTube Channel")
optional_args.add_argument('-d', '--debug', action='store_true',
help='Enables the debug log messages')
optional_args.add_argument("-h", "--help", action="help", help="Show this help message and exit")

return parser.parse_args()
args = parser.parse_args()
# Custom Condition Checking
if (args.id is None and args.username is None) and \
args.run_mode in ['add_channel', 'remove_channel']:
parser.error('You need to pass either --id or --username when selecting '
'the `add_channel` and `remove_channel` actions')
return args


def commenter(youtube: YoutubeManager, args: argparse.Namespace) -> None:
raise NotImplementedError()


def accumulator(youtube: YoutubeManager, args: argparse.Namespace) -> None:
raise NotImplementedError()


def add_channel(youtube: YoutubeManager, args: argparse.Namespace) -> None:
youtube.add_channel(channel_id=args.id, username=args.username)


def remove_channel(youtube: YoutubeManager, args: argparse.Namespace) -> None:
youtube.remove_channel(channel_id=args.id, username=args.username)


def list_channels(youtube: YoutubeManager, args: argparse.Namespace) -> None:
raise NotImplementedError()


def list_comments(youtube: YoutubeManager, args: argparse.Namespace) -> None:
raise NotImplementedError()


def refresh_photos(youtube: YoutubeManager, args: argparse.Namespace) -> None:
raise NotImplementedError()


def main():
Expand All @@ -47,21 +87,16 @@ def main():
# Initializing
args = get_args()
ColorizedLogger.setup_logger(log_path=args.log, debug=args.debug, clear_log=True)
# Load the configuration
# Load the configurations
conf_obj = Configuration(config_src=args.config_file)
you_conf = conf_obj.get_config('youtube')[0]
db_conf = conf_obj.get_config('datastore')[0]
# Setup Youtube API
yout_manager = YoutubeManagerV3(config=you_conf['config'],
channel_name=you_conf['channel'],
tag=conf_obj.tag)

# Test the video retrieval for 3 channels
pewd_info = yout_manager.get_channel_info_by_username('Pewdiepie')
v_info = yout_manager.get_channel_info_by_username('Veritasium')
ku_info = yout_manager.get_channel_info_by_username('Kurzgesagt')
channel_ids = [pewd_info['id'], v_info['id'], ku_info['id']]
for video in yout_manager.get_uploads(channels=channel_ids, last_n_hours=12000):
logger.info(video)
youtube = YoutubeManager(config=you_conf['config'], db_conf=db_conf,
tag=conf_obj.tag)
# Run in the specified run mode
func = globals()[args.run_mode]
func(youtube, args)


if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion youbot/youtube_utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Youtube Utils sub-package of YoutubeCommentBot."""

from .youtube_manager import YoutubeManagerV3
from .youtube_manager import YoutubeApiV3, YoutubeManager

__author__ = "drkostas"
__email__ = "georgiou.kostas94@gmail.com"
Expand Down
Loading

0 comments on commit 0221369

Please sign in to comment.