Change description of getting logger

This commit is contained in:
taizan-hokuto
2019-12-30 14:38:02 +09:00
parent a835d58e10
commit 7305e4178b
13 changed files with 85 additions and 50 deletions

View File

@@ -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

View File

@@ -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,

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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:

View File

@@ -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

View File

@@ -13,7 +13,7 @@ from .. exceptions import (
NoContinuationsException )
logger = mylogger.get_logger(__name__,mode=config.LOGGER_MODE)
logger = config.logger(__name__)
class Parser:

View File

@@ -7,7 +7,7 @@ from .. exceptions import (
NoContinuationsException )
logger = mylogger.get_logger(__name__,mode=config.LOGGER_MODE)
logger = config.logger(__name__)
class Parser:

View File

@@ -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):

View File

@@ -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):