From 7305e4178b5842b1a2aade37bc81d4b6b8e427e4 Mon Sep 17 00:00:00 2001 From: taizan-hokuto <55448286+taizan-hokuto@users.noreply.github.com> Date: Mon, 30 Dec 2019 14:38:02 +0900 Subject: [PATCH] Change description of getting logger --- README.md | 69 ++++++++++------------ pytchat/__init__.py | 3 +- pytchat/api.py | 1 + pytchat/config/__init__.py | 11 +++- pytchat/config/mylogger.py | 32 ++++++++++ pytchat/core_async/livechat.py | 2 +- pytchat/core_async/replaychat.py | 2 +- pytchat/core_multithread/livechat.py | 5 +- pytchat/core_multithread/replaychat.py | 2 +- pytchat/parser/live.py | 2 +- pytchat/parser/replay.py | 2 +- pytchat/processors/compatible/processor.py | 2 +- pytchat/processors/default/processor.py | 2 +- 13 files changed, 85 insertions(+), 50 deletions(-) create mode 100644 pytchat/config/mylogger.py diff --git a/README.md b/README.md index ab904d4..e8bae28 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ pip install pytchat ```python from pytchat import LiveChat -chat = LiveChat("G1w62uEMZ74") +chat = LiveChat("rsHWP7IjMiw") while chat.is_alive(): data = chat.get() for c in data.items: @@ -40,17 +40,17 @@ while chat.is_alive(): from pytchat import LiveChat import time -def main() - chat = LiveChat("G1w62uEMZ74", callback = func) - while chat.is_alive(): - time.sleep(3) - #other background operation. - -#callback function is automatically called periodically. -def func(data): +#callback function is automatically called. +def display(data): for c in data.items: print(f"{c.datetime} [{c.author.name}]-{c.message} {c.amountString}") data.tick() + +#entry point +chat = LiveChat("rsHWP7IjMiw", callback = display) +while chat.is_alive(): + time.sleep(3) + #other background operation. ``` ### asyncio context: @@ -59,63 +59,56 @@ from pytchat import LiveChatAsync import asyncio async def main(): - chat = LiveChatAsync("G1w62uEMZ74", callback = func) + chat = LiveChatAsync("rsHWP7IjMiw", callback = func) while chat.is_alive(): await asyncio.sleep(3) #other background operation. -#callback function is automatically called periodically. +#callback function is automatically called. async def func(data): for c in data.items: print(f"{c.datetime} [{c.author.name}]-{c.message} {c.amountString}") await data.tick_async() -loop = asyncio.get_event_loop() -loop.run_until_complete(main()) +try: + loop = asyncio.get_event_loop() + loop.run_until_complete(main()) +except CancelledError: + pass ``` ### youtube api compatible processor: ```python from pytchat import LiveChat, CompatibleProcessor +import time -chat = LiveChat("G1w62uEMZ74", +chat = LiveChat("rsHWP7IjMiw", processor = CompatibleProcessor() ) while chat.is_alive(): data = chat.get() - polling = data["pollingIntervalMillis"]/1000 - for c in data["items"]: - if c.get("snippet"): + polling = data['pollingIntervalMillis']/1000 + for c in data['items']: + if c.get('snippet'): print(f"[{c['authorDetails']['displayName']}]" f"-{c['snippet']['displayMessage']}") - time.sleep(polling/len(data["items"])) + time.sleep(polling/len(data['items'])) ``` ### replay: ```python -from pytchat import ReplayChatAsync -import asyncio +from pytchat import ReplayChat -async def main(): - chat = ReplayChatAsync("G1w62uEMZ74", seektime = 1000, callback = func) - while chat.is_alive(): - await asyncio.sleep(3) - #other background operation here. +def main(): + chat = ReplayChat("ojes5ULOqhc", seektime = 60*30) + while True: + data = chat.get() + for c in data.items: + print(f"{c.elapsedTime} [{c.author.name}]-{c.message} {c.amountString}") + data.tick() -#callback function is automatically called periodically. -async def func(data): - for count in range(0,len(data.items)): - c= data.items[count] - if count!=len(data.items): - tick=data.items[count+1].timestamp -data.items[count].timestamp - else: - tick=0 - print(f"<{c.elapsedTime}> [{c.author.name}]-{c.message} {c.amountString}") - await asyncio.sleep(tick/1000) - -loop = asyncio.get_event_loop() -loop.run_until_complete(main()) +main() ``` ## Structure of Default Processor diff --git a/pytchat/__init__.py b/pytchat/__init__.py index b87e086..65b61e4 100644 --- a/pytchat/__init__.py +++ b/pytchat/__init__.py @@ -2,7 +2,7 @@ pytchat is a python library for fetching youtube live chat without using yt api, Selenium, or BeautifulSoup. """ __copyright__ = 'Copyright (C) 2019 taizan-hokuto' -__version__ = '0.0.3.8' +__version__ = '0.0.3.9' __license__ = 'MIT' __author__ = 'taizan-hokuto' __author_email__ = '55448286+taizan-hokuto@users.noreply.github.com' @@ -11,6 +11,7 @@ __url__ = 'https://github.com/taizan-hokuto/pytchat' __all__ = ["core_async","core_multithread","processors"] from .api import ( + config, LiveChat, LiveChatAsync, ReplayChat, diff --git a/pytchat/api.py b/pytchat/api.py index 92164d6..10a7fba 100644 --- a/pytchat/api.py +++ b/pytchat/api.py @@ -9,3 +9,4 @@ from .processors.simple_display_processor import SimpleDisplayProcessor from .processors.jsonfile_archive_processor import JsonfileArchiveProcessor from .processors.speed_calculator import SpeedCalculator from .processors.dummy_processor import DummyProcessor +from . import config \ No newline at end of file diff --git a/pytchat/config/__init__.py b/pytchat/config/__init__.py index eb109d2..542551e 100644 --- a/pytchat/config/__init__.py +++ b/pytchat/config/__init__.py @@ -1,4 +1,13 @@ import logging -LOGGER_MODE = None +from . import mylogger + +LOGGER_MODE = logging.DEBUG + headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36'} + +def logger(module_name: str): + module_logger = mylogger.get_logger(module_name, mode = LOGGER_MODE) + return module_logger + + diff --git a/pytchat/config/mylogger.py b/pytchat/config/mylogger.py new file mode 100644 index 0000000..83e325f --- /dev/null +++ b/pytchat/config/mylogger.py @@ -0,0 +1,32 @@ +from logging import NullHandler, getLogger, StreamHandler, FileHandler, Formatter +import logging +import datetime + + +def get_logger(modname,mode=logging.DEBUG): + logger = getLogger(modname) + if mode == None: + logger.addHandler(NullHandler()) + return logger + logger.setLevel(mode) + #create handler1 for showing info + handler1 = StreamHandler() + my_formatter = MyFormatter() + handler1.setFormatter(my_formatter) + + handler1.setLevel(mode) + logger.addHandler(handler1) + #create handler2 for recording log file + if mode <= logging.DEBUG: + handler2 = FileHandler(filename="log.txt", encoding='utf-8') + handler2.setLevel(logging.ERROR) + handler2.setFormatter(my_formatter) + + + logger.addHandler(handler2) + return logger + +class MyFormatter(logging.Formatter): + def format(self, record): + s =(datetime.datetime.fromtimestamp(record.created)).strftime("%m-%d %H:%M:%S")+'| '+ (record.module).ljust(15)+(' { '+record.funcName).ljust(20) +":"+str(record.lineno).rjust(4)+'} - '+record.getMessage() + return s diff --git a/pytchat/core_async/livechat.py b/pytchat/core_async/livechat.py index 7d5e640..8fbfbd4 100644 --- a/pytchat/core_async/livechat.py +++ b/pytchat/core_async/livechat.py @@ -17,7 +17,7 @@ from ..paramgen import liveparam from ..processors.default.processor import DefaultProcessor from ..processors.combinator import Combinator -logger = mylogger.get_logger(__name__,mode=config.LOGGER_MODE) +logger = config.logger(__name__) MAX_RETRY = 10 headers = config.headers diff --git a/pytchat/core_async/replaychat.py b/pytchat/core_async/replaychat.py index 951c58e..9141e25 100644 --- a/pytchat/core_async/replaychat.py +++ b/pytchat/core_async/replaychat.py @@ -18,7 +18,7 @@ from ..paramgen import arcparam from ..processors.default.processor import DefaultProcessor from ..processors.combinator import Combinator -logger = mylogger.get_logger(__name__,mode=config.LOGGER_MODE) +logger = config.logger(__name__) MAX_RETRY = 10 headers = config.headers diff --git a/pytchat/core_multithread/livechat.py b/pytchat/core_multithread/livechat.py index da9e52c..076a6f1 100644 --- a/pytchat/core_multithread/livechat.py +++ b/pytchat/core_multithread/livechat.py @@ -16,10 +16,9 @@ from ..paramgen import liveparam from ..processors.default.processor import DefaultProcessor from ..processors.combinator import Combinator -logger = mylogger.get_logger(__name__,mode=config.LOGGER_MODE) -MAX_RETRY = 10 +logger = config.logger(__name__) headers = config.headers - +MAX_RETRY = 10 class LiveChat: diff --git a/pytchat/core_multithread/replaychat.py b/pytchat/core_multithread/replaychat.py index 936f337..bc89aa1 100644 --- a/pytchat/core_multithread/replaychat.py +++ b/pytchat/core_multithread/replaychat.py @@ -17,7 +17,7 @@ from ..paramgen import arcparam from ..processors.default.processor import DefaultProcessor from ..processors.combinator import Combinator -logger = mylogger.get_logger(__name__,mode=config.LOGGER_MODE) +logger = config.logger(__name__) MAX_RETRY = 10 headers = config.headers diff --git a/pytchat/parser/live.py b/pytchat/parser/live.py index a8cf12c..4236c7f 100644 --- a/pytchat/parser/live.py +++ b/pytchat/parser/live.py @@ -13,7 +13,7 @@ from .. exceptions import ( NoContinuationsException ) -logger = mylogger.get_logger(__name__,mode=config.LOGGER_MODE) +logger = config.logger(__name__) class Parser: diff --git a/pytchat/parser/replay.py b/pytchat/parser/replay.py index fc27a8d..93a17b8 100644 --- a/pytchat/parser/replay.py +++ b/pytchat/parser/replay.py @@ -7,7 +7,7 @@ from .. exceptions import ( NoContinuationsException ) -logger = mylogger.get_logger(__name__,mode=config.LOGGER_MODE) +logger = config.logger(__name__) class Parser: diff --git a/pytchat/processors/compatible/processor.py b/pytchat/processors/compatible/processor.py index 31e1b15..32a3a64 100644 --- a/pytchat/processors/compatible/processor.py +++ b/pytchat/processors/compatible/processor.py @@ -7,7 +7,7 @@ from .renderer.legacypaid import LiveChatLegacyPaidMessageRenderer from .. chat_processor import ChatProcessor from ... import mylogger from ... import config -logger = mylogger.get_logger(__name__,mode=config.LOGGER_MODE) +logger = config.logger(__name__) class CompatibleProcessor(ChatProcessor): diff --git a/pytchat/processors/default/processor.py b/pytchat/processors/default/processor.py index be3dfd6..4bff021 100644 --- a/pytchat/processors/default/processor.py +++ b/pytchat/processors/default/processor.py @@ -7,7 +7,7 @@ from .renderer.legacypaid import LiveChatLegacyPaidMessageRenderer from .. chat_processor import ChatProcessor from ... import config from ... import mylogger -logger = mylogger.get_logger(__name__,mode=config.LOGGER_MODE) +logger = config.logger(__name__) class Chatdata: def __init__(self,chatlist:list, timeout:float):