Merge branch 'feature/config_logger' into develop
This commit is contained in:
69
README.md
69
README.md
@@ -27,7 +27,7 @@ pip install pytchat
|
|||||||
```python
|
```python
|
||||||
from pytchat import LiveChat
|
from pytchat import LiveChat
|
||||||
|
|
||||||
chat = LiveChat("G1w62uEMZ74")
|
chat = LiveChat("rsHWP7IjMiw")
|
||||||
while chat.is_alive():
|
while chat.is_alive():
|
||||||
data = chat.get()
|
data = chat.get()
|
||||||
for c in data.items:
|
for c in data.items:
|
||||||
@@ -40,17 +40,17 @@ while chat.is_alive():
|
|||||||
from pytchat import LiveChat
|
from pytchat import LiveChat
|
||||||
import time
|
import time
|
||||||
|
|
||||||
def main()
|
#callback function is automatically called.
|
||||||
chat = LiveChat("G1w62uEMZ74", callback = func)
|
def display(data):
|
||||||
while chat.is_alive():
|
|
||||||
time.sleep(3)
|
|
||||||
#other background operation.
|
|
||||||
|
|
||||||
#callback function is automatically called periodically.
|
|
||||||
def func(data):
|
|
||||||
for c in data.items:
|
for c in data.items:
|
||||||
print(f"{c.datetime} [{c.author.name}]-{c.message} {c.amountString}")
|
print(f"{c.datetime} [{c.author.name}]-{c.message} {c.amountString}")
|
||||||
data.tick()
|
data.tick()
|
||||||
|
|
||||||
|
#entry point
|
||||||
|
chat = LiveChat("rsHWP7IjMiw", callback = display)
|
||||||
|
while chat.is_alive():
|
||||||
|
time.sleep(3)
|
||||||
|
#other background operation.
|
||||||
```
|
```
|
||||||
|
|
||||||
### asyncio context:
|
### asyncio context:
|
||||||
@@ -59,63 +59,56 @@ from pytchat import LiveChatAsync
|
|||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
chat = LiveChatAsync("G1w62uEMZ74", callback = func)
|
chat = LiveChatAsync("rsHWP7IjMiw", callback = func)
|
||||||
while chat.is_alive():
|
while chat.is_alive():
|
||||||
await asyncio.sleep(3)
|
await asyncio.sleep(3)
|
||||||
#other background operation.
|
#other background operation.
|
||||||
|
|
||||||
#callback function is automatically called periodically.
|
#callback function is automatically called.
|
||||||
async def func(data):
|
async def func(data):
|
||||||
for c in data.items:
|
for c in data.items:
|
||||||
print(f"{c.datetime} [{c.author.name}]-{c.message} {c.amountString}")
|
print(f"{c.datetime} [{c.author.name}]-{c.message} {c.amountString}")
|
||||||
await data.tick_async()
|
await data.tick_async()
|
||||||
|
|
||||||
loop = asyncio.get_event_loop()
|
try:
|
||||||
loop.run_until_complete(main())
|
loop = asyncio.get_event_loop()
|
||||||
|
loop.run_until_complete(main())
|
||||||
|
except CancelledError:
|
||||||
|
pass
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### youtube api compatible processor:
|
### youtube api compatible processor:
|
||||||
```python
|
```python
|
||||||
from pytchat import LiveChat, CompatibleProcessor
|
from pytchat import LiveChat, CompatibleProcessor
|
||||||
|
import time
|
||||||
|
|
||||||
chat = LiveChat("G1w62uEMZ74",
|
chat = LiveChat("rsHWP7IjMiw",
|
||||||
processor = CompatibleProcessor() )
|
processor = CompatibleProcessor() )
|
||||||
|
|
||||||
while chat.is_alive():
|
while chat.is_alive():
|
||||||
data = chat.get()
|
data = chat.get()
|
||||||
polling = data["pollingIntervalMillis"]/1000
|
polling = data['pollingIntervalMillis']/1000
|
||||||
for c in data["items"]:
|
for c in data['items']:
|
||||||
if c.get("snippet"):
|
if c.get('snippet'):
|
||||||
print(f"[{c['authorDetails']['displayName']}]"
|
print(f"[{c['authorDetails']['displayName']}]"
|
||||||
f"-{c['snippet']['displayMessage']}")
|
f"-{c['snippet']['displayMessage']}")
|
||||||
time.sleep(polling/len(data["items"]))
|
time.sleep(polling/len(data['items']))
|
||||||
|
|
||||||
```
|
```
|
||||||
### replay:
|
### replay:
|
||||||
```python
|
```python
|
||||||
from pytchat import ReplayChatAsync
|
from pytchat import ReplayChat
|
||||||
import asyncio
|
|
||||||
|
|
||||||
async def main():
|
def main():
|
||||||
chat = ReplayChatAsync("G1w62uEMZ74", seektime = 1000, callback = func)
|
chat = ReplayChat("ojes5ULOqhc", seektime = 60*30)
|
||||||
while chat.is_alive():
|
while True:
|
||||||
await asyncio.sleep(3)
|
data = chat.get()
|
||||||
#other background operation here.
|
for c in data.items:
|
||||||
|
print(f"{c.elapsedTime} [{c.author.name}]-{c.message} {c.amountString}")
|
||||||
|
data.tick()
|
||||||
|
|
||||||
#callback function is automatically called periodically.
|
main()
|
||||||
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())
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Structure of Default Processor
|
## Structure of Default Processor
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
pytchat is a python library for fetching youtube live chat without using yt api, Selenium, or BeautifulSoup.
|
pytchat is a python library for fetching youtube live chat without using yt api, Selenium, or BeautifulSoup.
|
||||||
"""
|
"""
|
||||||
__copyright__ = 'Copyright (C) 2019 taizan-hokuto'
|
__copyright__ = 'Copyright (C) 2019 taizan-hokuto'
|
||||||
__version__ = '0.0.3.8'
|
__version__ = '0.0.3.9'
|
||||||
__license__ = 'MIT'
|
__license__ = 'MIT'
|
||||||
__author__ = 'taizan-hokuto'
|
__author__ = 'taizan-hokuto'
|
||||||
__author_email__ = '55448286+taizan-hokuto@users.noreply.github.com'
|
__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"]
|
__all__ = ["core_async","core_multithread","processors"]
|
||||||
|
|
||||||
from .api import (
|
from .api import (
|
||||||
|
config,
|
||||||
LiveChat,
|
LiveChat,
|
||||||
LiveChatAsync,
|
LiveChatAsync,
|
||||||
ReplayChat,
|
ReplayChat,
|
||||||
|
|||||||
@@ -9,3 +9,4 @@ from .processors.simple_display_processor import SimpleDisplayProcessor
|
|||||||
from .processors.jsonfile_archive_processor import JsonfileArchiveProcessor
|
from .processors.jsonfile_archive_processor import JsonfileArchiveProcessor
|
||||||
from .processors.speed_calculator import SpeedCalculator
|
from .processors.speed_calculator import SpeedCalculator
|
||||||
from .processors.dummy_processor import DummyProcessor
|
from .processors.dummy_processor import DummyProcessor
|
||||||
|
from . import config
|
||||||
@@ -1,4 +1,13 @@
|
|||||||
import logging
|
import logging
|
||||||
LOGGER_MODE = None
|
from . import mylogger
|
||||||
|
|
||||||
|
LOGGER_MODE = logging.DEBUG
|
||||||
|
|
||||||
headers = {
|
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'}
|
'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
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
32
pytchat/config/mylogger.py
Normal file
32
pytchat/config/mylogger.py
Normal 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
|
||||||
@@ -17,7 +17,7 @@ from ..paramgen import liveparam
|
|||||||
from ..processors.default.processor import DefaultProcessor
|
from ..processors.default.processor import DefaultProcessor
|
||||||
from ..processors.combinator import Combinator
|
from ..processors.combinator import Combinator
|
||||||
|
|
||||||
logger = mylogger.get_logger(__name__,mode=config.LOGGER_MODE)
|
logger = config.logger(__name__)
|
||||||
MAX_RETRY = 10
|
MAX_RETRY = 10
|
||||||
headers = config.headers
|
headers = config.headers
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ from ..paramgen import arcparam
|
|||||||
from ..processors.default.processor import DefaultProcessor
|
from ..processors.default.processor import DefaultProcessor
|
||||||
from ..processors.combinator import Combinator
|
from ..processors.combinator import Combinator
|
||||||
|
|
||||||
logger = mylogger.get_logger(__name__,mode=config.LOGGER_MODE)
|
logger = config.logger(__name__)
|
||||||
MAX_RETRY = 10
|
MAX_RETRY = 10
|
||||||
headers = config.headers
|
headers = config.headers
|
||||||
|
|
||||||
|
|||||||
@@ -16,10 +16,9 @@ from ..paramgen import liveparam
|
|||||||
from ..processors.default.processor import DefaultProcessor
|
from ..processors.default.processor import DefaultProcessor
|
||||||
from ..processors.combinator import Combinator
|
from ..processors.combinator import Combinator
|
||||||
|
|
||||||
logger = mylogger.get_logger(__name__,mode=config.LOGGER_MODE)
|
logger = config.logger(__name__)
|
||||||
MAX_RETRY = 10
|
|
||||||
headers = config.headers
|
headers = config.headers
|
||||||
|
MAX_RETRY = 10
|
||||||
|
|
||||||
|
|
||||||
class LiveChat:
|
class LiveChat:
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ from ..paramgen import arcparam
|
|||||||
from ..processors.default.processor import DefaultProcessor
|
from ..processors.default.processor import DefaultProcessor
|
||||||
from ..processors.combinator import Combinator
|
from ..processors.combinator import Combinator
|
||||||
|
|
||||||
logger = mylogger.get_logger(__name__,mode=config.LOGGER_MODE)
|
logger = config.logger(__name__)
|
||||||
MAX_RETRY = 10
|
MAX_RETRY = 10
|
||||||
headers = config.headers
|
headers = config.headers
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ from .. exceptions import (
|
|||||||
NoContinuationsException )
|
NoContinuationsException )
|
||||||
|
|
||||||
|
|
||||||
logger = mylogger.get_logger(__name__,mode=config.LOGGER_MODE)
|
logger = config.logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class Parser:
|
class Parser:
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ from .. exceptions import (
|
|||||||
NoContinuationsException )
|
NoContinuationsException )
|
||||||
|
|
||||||
|
|
||||||
logger = mylogger.get_logger(__name__,mode=config.LOGGER_MODE)
|
logger = config.logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class Parser:
|
class Parser:
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ from .renderer.legacypaid import LiveChatLegacyPaidMessageRenderer
|
|||||||
from .. chat_processor import ChatProcessor
|
from .. chat_processor import ChatProcessor
|
||||||
from ... import mylogger
|
from ... import mylogger
|
||||||
from ... import config
|
from ... import config
|
||||||
logger = mylogger.get_logger(__name__,mode=config.LOGGER_MODE)
|
logger = config.logger(__name__)
|
||||||
|
|
||||||
class CompatibleProcessor(ChatProcessor):
|
class CompatibleProcessor(ChatProcessor):
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ from .renderer.legacypaid import LiveChatLegacyPaidMessageRenderer
|
|||||||
from .. chat_processor import ChatProcessor
|
from .. chat_processor import ChatProcessor
|
||||||
from ... import config
|
from ... import config
|
||||||
from ... import mylogger
|
from ... import mylogger
|
||||||
logger = mylogger.get_logger(__name__,mode=config.LOGGER_MODE)
|
logger = config.logger(__name__)
|
||||||
|
|
||||||
class Chatdata:
|
class Chatdata:
|
||||||
def __init__(self,chatlist:list, timeout:float):
|
def __init__(self,chatlist:list, timeout:float):
|
||||||
|
|||||||
Reference in New Issue
Block a user