Compare commits
37 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bbf7a2906a | ||
|
|
1862b83eac | ||
|
|
053ff5291f | ||
|
|
4e47d4a262 | ||
|
|
956c7e2640 | ||
|
|
f7d1830226 | ||
|
|
76b126faf2 | ||
|
|
bbd01d6523 | ||
|
|
f8fa0e394e | ||
|
|
efdf07e3de | ||
|
|
2573cc18de | ||
|
|
1c5852421b | ||
|
|
970d111e1b | ||
|
|
1643dd1ad1 | ||
|
|
0272319fa6 | ||
|
|
fb0edef136 | ||
|
|
260a2b35a9 | ||
|
|
e03d39475e | ||
|
|
2462b8aca0 | ||
|
|
a1024c8734 | ||
|
|
6b3ca00d35 | ||
|
|
385634b709 | ||
|
|
c1a78a2743 | ||
|
|
7961801e0c | ||
|
|
5fe4e7af04 | ||
|
|
892dfb8a91 | ||
|
|
fddab22a1f | ||
|
|
7194948066 | ||
|
|
a836d92194 | ||
|
|
c408cb2713 | ||
|
|
c3d2238ead | ||
|
|
6c8d390fc7 | ||
|
|
ff1ee70d7e | ||
|
|
404623546e | ||
|
|
3f9f64d19c | ||
|
|
7996c6adad | ||
|
|
50d55da7dc |
@@ -1,7 +1,5 @@
|
||||
include requirements.txt
|
||||
include requirements_test.txt
|
||||
prune testrun*.py
|
||||
prune log.txt
|
||||
prune quote.txt
|
||||
prune .gitignore
|
||||
prun tests
|
||||
include README.MD
|
||||
global-exclude tests/*
|
||||
global-exclude pytchat/testrun*.py
|
||||
93
README.md
93
README.md
@@ -7,13 +7,16 @@ pytchat is a python library for fetching youtube live chat.
|
||||
pytchat is a python library for fetching youtube live chat
|
||||
without using youtube api, Selenium or BeautifulSoup.
|
||||
|
||||
pytchatはAPIを使わずにYouTubeチャットを取得するための軽量pythonライブラリです。
|
||||
|
||||
Other features:
|
||||
+ Customizable chat data processors including youtube api compatible one.
|
||||
+ Available on asyncio context.
|
||||
+ Quick fetching of initial chat data by generating continuation params
|
||||
instead of web scraping.
|
||||
|
||||
For more detailed information, see [wiki](https://github.com/taizan-hokuto/pytchat/wiki).
|
||||
For more detailed information, see [wiki](https://github.com/taizan-hokuto/pytchat/wiki). <br>
|
||||
より詳細な解説は[wiki](https://github.com/taizan-hokuto/pytchat/wiki/Home_jp)を参照してください。
|
||||
|
||||
## Install
|
||||
```python
|
||||
@@ -26,13 +29,17 @@ pip install pytchat
|
||||
### on-demand mode
|
||||
```python
|
||||
from pytchat import LiveChat
|
||||
livechat = LiveChat(video_id = "Zvp1pJpie4I")
|
||||
|
||||
chat = LiveChat("rsHWP7IjMiw")
|
||||
while chat.is_alive():
|
||||
data = chat.get()
|
||||
for c in data.items:
|
||||
print(f"{c.datetime} [{c.author.name}]-{c.message} {c.amountString}")
|
||||
data.tick()
|
||||
while livechat.is_alive():
|
||||
try:
|
||||
chatdata = livechat.get()
|
||||
for c in chatdata.items:
|
||||
print(f"{c.datetime} [{c.author.name}]- {c.message}")
|
||||
chatdata.tick()
|
||||
except KeyboardInterrupt:
|
||||
livechat.terminate()
|
||||
break
|
||||
```
|
||||
|
||||
### callback mode
|
||||
@@ -40,17 +47,21 @@ while chat.is_alive():
|
||||
from pytchat import LiveChat
|
||||
import time
|
||||
|
||||
#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()
|
||||
def main():
|
||||
livechat = LiveChat(video_id = "Zvp1pJpie4I", callback = disp)
|
||||
while livechat.is_alive():
|
||||
#other background operation.
|
||||
time.sleep(1)
|
||||
livechat.terminate()
|
||||
|
||||
#callback function (automatically called)
|
||||
def disp(chatdata):
|
||||
for c in chatdata.items:
|
||||
print(f"{c.datetime} [{c.author.name}]- {c.message}")
|
||||
chatdata.tick()
|
||||
|
||||
if __name__ == '__main__':
|
||||
chat = LiveChat("rsHWP7IjMiw", callback = display)
|
||||
while chat.is_alive():
|
||||
#other background operation.
|
||||
time.sleep(3)
|
||||
main()
|
||||
|
||||
```
|
||||
|
||||
@@ -61,16 +72,16 @@ from concurrent.futures import CancelledError
|
||||
import asyncio
|
||||
|
||||
async def main():
|
||||
chat = LiveChatAsync("rsHWP7IjMiw", callback = func)
|
||||
while chat.is_alive():
|
||||
livechat = LiveChatAsync("Zvp1pJpie4I", callback = func)
|
||||
while livechat.is_alive():
|
||||
#other background operation.
|
||||
await asyncio.sleep(3)
|
||||
|
||||
#callback function is automatically called.
|
||||
async def func(data):
|
||||
for c in data.items:
|
||||
async def func(chatdata):
|
||||
for c in chatdata.items:
|
||||
print(f"{c.datetime} [{c.author.name}]-{c.message} {c.amountString}")
|
||||
await data.tick_async()
|
||||
await chatdata.tick_async()
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
@@ -86,18 +97,20 @@ if __name__ == '__main__':
|
||||
from pytchat import LiveChat, CompatibleProcessor
|
||||
import time
|
||||
|
||||
chat = LiveChat("rsHWP7IjMiw",
|
||||
chat = LiveChat("Zvp1pJpie4I",
|
||||
processor = CompatibleProcessor() )
|
||||
|
||||
while chat.is_alive():
|
||||
data = chat.get()
|
||||
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']))
|
||||
|
||||
try:
|
||||
data = chat.get()
|
||||
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']))
|
||||
except KeyboardInterrupt:
|
||||
chat.terminate()
|
||||
```
|
||||
### replay:
|
||||
If specified video is not live,
|
||||
@@ -108,19 +121,23 @@ from pytchat import LiveChat
|
||||
|
||||
def main():
|
||||
#seektime (seconds): start position of chat.
|
||||
chat = ReplayChat("ojes5ULOqhc", seektime = 60*30)
|
||||
while chat.is_alive():
|
||||
data = chat.get()
|
||||
for c in data.items:
|
||||
print(f"{c.elapsedTime} [{c.author.name}]-{c.message} {c.amountString}")
|
||||
data.tick()
|
||||
chat = LiveChat("ojes5ULOqhc", seektime = 60*30)
|
||||
print('Replay from 30:00')
|
||||
try:
|
||||
while chat.is_alive():
|
||||
data = chat.get()
|
||||
for c in data.items:
|
||||
print(f"{c.elapsedTime} [{c.author.name}]-{c.message} {c.amountString}")
|
||||
data.tick()
|
||||
except KeyboardInterrupt:
|
||||
chat.terminate()
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
```
|
||||
|
||||
## Structure of Default Processor
|
||||
Each item can be got with items() function.
|
||||
Each item can be got with `items` function.
|
||||
<table>
|
||||
<tr>
|
||||
<th>name</th>
|
||||
@@ -250,4 +267,4 @@ Structure of author object.
|
||||
|
||||
[taizan-hokuto](https://github.com/taizan-hokuto)
|
||||
|
||||
[twitter:@taizan205](https://twitter.com/taizan205)
|
||||
[twitter:@taizan205](https://twitter.com/taizan205)
|
||||
|
||||
@@ -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.4.3'
|
||||
__version__ = '0.0.5.2'
|
||||
__license__ = 'MIT'
|
||||
__author__ = 'taizan-hokuto'
|
||||
__author_email__ = '55448286+taizan-hokuto@users.noreply.github.com'
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
import logging
|
||||
from . import mylogger
|
||||
|
||||
LOGGER_MODE = None
|
||||
|
||||
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)
|
||||
def logger(module_name: str, loglevel = None):
|
||||
module_logger = mylogger.get_logger(module_name, loglevel = loglevel)
|
||||
return module_logger
|
||||
|
||||
|
||||
|
||||
@@ -3,21 +3,21 @@ import logging
|
||||
import datetime
|
||||
|
||||
|
||||
def get_logger(modname,mode=logging.DEBUG):
|
||||
def get_logger(modname,loglevel=logging.DEBUG):
|
||||
logger = getLogger(modname)
|
||||
if mode == None:
|
||||
if loglevel == None:
|
||||
logger.addHandler(NullHandler())
|
||||
return logger
|
||||
logger.setLevel(mode)
|
||||
logger.setLevel(loglevel)
|
||||
#create handler1 for showing info
|
||||
handler1 = StreamHandler()
|
||||
my_formatter = MyFormatter()
|
||||
handler1.setFormatter(my_formatter)
|
||||
|
||||
handler1.setLevel(mode)
|
||||
handler1.setLevel(loglevel)
|
||||
logger.addHandler(handler1)
|
||||
#create handler2 for recording log file
|
||||
if mode <= logging.DEBUG:
|
||||
if loglevel <= logging.DEBUG:
|
||||
handler2 = FileHandler(filename="log.txt", encoding='utf-8')
|
||||
handler2.setLevel(logging.ERROR)
|
||||
handler2.setFormatter(my_formatter)
|
||||
|
||||
@@ -17,7 +17,6 @@ from ..paramgen import liveparam, arcparam
|
||||
from ..processors.default.processor import DefaultProcessor
|
||||
from ..processors.combinator import Combinator
|
||||
|
||||
logger = config.logger(__name__)
|
||||
headers = config.headers
|
||||
MAX_RETRY = 10
|
||||
|
||||
@@ -63,6 +62,9 @@ class LiveChatAsync:
|
||||
force_replay : bool
|
||||
Trueの場合、ライブチャットが取得できる場合であっても
|
||||
強制的にアーカイブ済みチャットを取得する。
|
||||
|
||||
topchat_only : bool
|
||||
Trueの場合、上位チャットのみ取得する。
|
||||
|
||||
Attributes
|
||||
---------
|
||||
@@ -71,6 +73,7 @@ class LiveChatAsync:
|
||||
'''
|
||||
|
||||
_setup_finished = False
|
||||
_logger = config.logger(__name__)
|
||||
|
||||
def __init__(self, video_id,
|
||||
seektime = 0,
|
||||
@@ -81,7 +84,9 @@ class LiveChatAsync:
|
||||
done_callback = None,
|
||||
exception_handler = None,
|
||||
direct_mode = False,
|
||||
force_replay = False
|
||||
force_replay = False,
|
||||
topchat_only = False,
|
||||
logger = config.logger(__name__),
|
||||
):
|
||||
self.video_id = video_id
|
||||
self.seektime = seektime
|
||||
@@ -102,11 +107,13 @@ class LiveChatAsync:
|
||||
self._setup()
|
||||
self._first_fetch = True
|
||||
self._fetch_url = "live_chat/get_live_chat?continuation="
|
||||
self._topchat_only = topchat_only
|
||||
self._logger = logger
|
||||
LiveChatAsync._logger = logger
|
||||
|
||||
if not LiveChatAsync._setup_finished:
|
||||
LiveChatAsync._setup_finished = True
|
||||
if exception_handler == None:
|
||||
self._set_exception_handler(self._handle_exception)
|
||||
else:
|
||||
if exception_handler:
|
||||
self._set_exception_handler(exception_handler)
|
||||
if interruptable:
|
||||
signal.signal(signal.SIGINT,
|
||||
@@ -182,14 +189,14 @@ class LiveChatAsync:
|
||||
continuation = metadata.get('continuation')
|
||||
except ChatParseException as e:
|
||||
#self.terminate()
|
||||
logger.debug(f"[{self.video_id}]{str(e)}")
|
||||
self._logger.debug(f"[{self.video_id}]{str(e)}")
|
||||
return
|
||||
except (TypeError , json.JSONDecodeError) :
|
||||
#self.terminate()
|
||||
logger.error(f"{traceback.format_exc(limit = -1)}")
|
||||
self._logger.error(f"{traceback.format_exc(limit = -1)}")
|
||||
return
|
||||
|
||||
logger.debug(f"[{self.video_id}]finished fetching chat.")
|
||||
self._logger.debug(f"[{self.video_id}]finished fetching chat.")
|
||||
|
||||
async def _check_pause(self, continuation):
|
||||
if self._pauser.empty():
|
||||
@@ -200,7 +207,8 @@ class LiveChatAsync:
|
||||
'''
|
||||
self._pauser.put_nowait(None)
|
||||
if not self._is_replay:
|
||||
continuation = liveparam.getparam(self.video_id,3)
|
||||
continuation = liveparam.getparam(
|
||||
self.video_id, 3, self._topchat_only)
|
||||
return continuation
|
||||
|
||||
async def _get_contents(self, continuation, session, headers):
|
||||
@@ -220,11 +228,16 @@ class LiveChatAsync:
|
||||
if contents is None or self._is_replay:
|
||||
'''Try to fetch archive chat data.'''
|
||||
self._parser.is_replay = True
|
||||
self._fetch_url = ("live_chat_replay/"
|
||||
"get_live_chat_replay?continuation=")
|
||||
continuation = arcparam.getparam(self.video_id, self.seektime)
|
||||
self._fetch_url = "live_chat_replay/get_live_chat_replay?continuation="
|
||||
continuation = arcparam.getparam(
|
||||
self.video_id, self.seektime, self._topchat_only)
|
||||
livechat_json = (await self._get_livechat_json(
|
||||
continuation, session, headers))
|
||||
reload_continuation = self._parser.reload_continuation(
|
||||
self._parser.get_contents(livechat_json))
|
||||
if reload_continuation:
|
||||
livechat_json = (await self._get_livechat_json(
|
||||
reload_continuation, session, headers))
|
||||
contents = self._parser.get_contents(livechat_json)
|
||||
self._first_fetch = False
|
||||
return contents
|
||||
@@ -248,7 +261,7 @@ class LiveChatAsync:
|
||||
await asyncio.sleep(1)
|
||||
continue
|
||||
else:
|
||||
logger.error(f"[{self.video_id}]"
|
||||
self._logger.error(f"[{self.video_id}]"
|
||||
f"Exceeded retry count. status_code={status_code}")
|
||||
return None
|
||||
return livechat_json
|
||||
@@ -303,7 +316,7 @@ class LiveChatAsync:
|
||||
try:
|
||||
self.terminate()
|
||||
except CancelledError:
|
||||
logger.debug(f'[{self.video_id}]cancelled:{sender}')
|
||||
self._logger.debug(f'[{self.video_id}]cancelled:{sender}')
|
||||
|
||||
def terminate(self):
|
||||
'''
|
||||
@@ -313,28 +326,21 @@ class LiveChatAsync:
|
||||
if self._direct_mode == False:
|
||||
#bufferにダミーオブジェクトを入れてis_alive()を判定させる
|
||||
self._buffer.put_nowait({'chatdata':'','timeout':0})
|
||||
logger.info(f'[{self.video_id}]finished.')
|
||||
self._logger.info(f'[{self.video_id}]finished.')
|
||||
|
||||
@classmethod
|
||||
def _set_exception_handler(cls, handler):
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.set_exception_handler(handler)
|
||||
|
||||
@classmethod
|
||||
def _handle_exception(cls, loop, context):
|
||||
if not isinstance(context["exception"],CancelledError):
|
||||
logger.error(f"Caught exception: {context}")
|
||||
loop= asyncio.get_event_loop()
|
||||
loop.create_task(cls.shutdown(None,None,None))
|
||||
|
||||
@classmethod
|
||||
async def shutdown(cls, event, sig = None, handler=None):
|
||||
logger.debug("shutdown...")
|
||||
cls._logger.debug("shutdown...")
|
||||
tasks = [t for t in asyncio.all_tasks() if t is not
|
||||
asyncio.current_task()]
|
||||
[task.cancel() for task in tasks]
|
||||
|
||||
logger.debug(f"complete remaining tasks...")
|
||||
cls._logger.debug(f"complete remaining tasks...")
|
||||
await asyncio.gather(*tasks,return_exceptions=True)
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.stop()
|
||||
@@ -16,7 +16,6 @@ from ..paramgen import liveparam, arcparam
|
||||
from ..processors.default.processor import DefaultProcessor
|
||||
from ..processors.combinator import Combinator
|
||||
|
||||
logger = config.logger(__name__)
|
||||
headers = config.headers
|
||||
MAX_RETRY = 10
|
||||
|
||||
@@ -60,6 +59,9 @@ class LiveChat:
|
||||
Trueの場合、ライブチャットが取得できる場合であっても
|
||||
強制的にアーカイブ済みチャットを取得する。
|
||||
|
||||
topchat_only : bool
|
||||
Trueの場合、上位チャットのみ取得する。
|
||||
|
||||
Attributes
|
||||
---------
|
||||
_executor : ThreadPoolExecutor
|
||||
@@ -71,7 +73,9 @@ class LiveChat:
|
||||
|
||||
_setup_finished = False
|
||||
#チャット監視中のListenerのリスト
|
||||
_listeners= []
|
||||
_listeners = []
|
||||
_logger = config.logger(__name__)
|
||||
|
||||
def __init__(self, video_id,
|
||||
seektime = 0,
|
||||
processor = DefaultProcessor(),
|
||||
@@ -80,7 +84,9 @@ class LiveChat:
|
||||
callback = None,
|
||||
done_callback = None,
|
||||
direct_mode = False,
|
||||
force_replay = False
|
||||
force_replay = False,
|
||||
topchat_only = False,
|
||||
logger = config.logger(__name__)
|
||||
):
|
||||
self.video_id = video_id
|
||||
self.seektime = seektime
|
||||
@@ -101,7 +107,9 @@ class LiveChat:
|
||||
self._setup()
|
||||
self._first_fetch = True
|
||||
self._fetch_url = "live_chat/get_live_chat?continuation="
|
||||
|
||||
self._topchat_only = topchat_only
|
||||
self._logger = logger
|
||||
LiveChat._logger = logger
|
||||
if not LiveChat._setup_finished:
|
||||
LiveChat._setup_finished = True
|
||||
if interruptable:
|
||||
@@ -111,7 +119,6 @@ class LiveChat:
|
||||
LiveChat._listeners.append(self)
|
||||
|
||||
def _setup(self):
|
||||
#logger.debug("setup")
|
||||
#direct modeがTrueでcallback未設定の場合例外発生。
|
||||
if self._direct_mode:
|
||||
if self._callback is None:
|
||||
@@ -177,13 +184,13 @@ class LiveChat:
|
||||
time.sleep(diff_time if diff_time > 0 else 0)
|
||||
continuation = metadata.get('continuation')
|
||||
except ChatParseException as e:
|
||||
logger.debug(f"[{self.video_id}]{str(e)}")
|
||||
self._logger.debug(f"[{self.video_id}]{str(e)}")
|
||||
return
|
||||
except (TypeError , json.JSONDecodeError) :
|
||||
logger.error(f"{traceback.format_exc(limit = -1)}")
|
||||
self._logger.error(f"{traceback.format_exc(limit = -1)}")
|
||||
return
|
||||
|
||||
logger.debug(f"[{self.video_id}]finished fetching chat.")
|
||||
self._logger.debug(f"[{self.video_id}]finished fetching chat.")
|
||||
|
||||
def _check_pause(self, continuation):
|
||||
if self._pauser.empty():
|
||||
@@ -214,8 +221,7 @@ class LiveChat:
|
||||
if contents is None or self._is_replay:
|
||||
'''Try to fetch archive chat data.'''
|
||||
self._parser.is_replay = True
|
||||
self._fetch_url = ("live_chat_replay/"
|
||||
"get_live_chat_replay?continuation=")
|
||||
self._fetch_url = "live_chat_replay/get_live_chat_replay?continuation="
|
||||
continuation = arcparam.getparam(self.video_id, self.seektime)
|
||||
livechat_json = ( self._get_livechat_json(
|
||||
continuation, session, headers))
|
||||
@@ -230,8 +236,7 @@ class LiveChat:
|
||||
continuation = urllib.parse.quote(continuation)
|
||||
livechat_json = None
|
||||
status_code = 0
|
||||
url =(
|
||||
f"https://www.youtube.com/{self._fetch_url}{continuation}&pbj=1")
|
||||
url =f"https://www.youtube.com/{self._fetch_url}{continuation}&pbj=1"
|
||||
for _ in range(MAX_RETRY + 1):
|
||||
with session.get(url ,headers = headers) as resp:
|
||||
try:
|
||||
@@ -242,7 +247,7 @@ class LiveChat:
|
||||
time.sleep(1)
|
||||
continue
|
||||
else:
|
||||
logger.error(f"[{self.video_id}]"
|
||||
self._logger.error(f"[{self.video_id}]"
|
||||
f"Exceeded retry count. status_code={status_code}")
|
||||
return None
|
||||
return livechat_json
|
||||
@@ -297,7 +302,7 @@ class LiveChat:
|
||||
try:
|
||||
self.terminate()
|
||||
except CancelledError:
|
||||
logger.debug(f'[{self.video_id}]cancelled:{sender}')
|
||||
self._logger.debug(f'[{self.video_id}]cancelled:{sender}')
|
||||
|
||||
def terminate(self):
|
||||
'''
|
||||
@@ -307,10 +312,10 @@ class LiveChat:
|
||||
if self._direct_mode == False:
|
||||
#bufferにダミーオブジェクトを入れてis_alive()を判定させる
|
||||
self._buffer.put({'chatdata':'','timeout':0})
|
||||
logger.info(f'[{self.video_id}]finished.')
|
||||
self._logger.info(f'[{self.video_id}]finished.')
|
||||
|
||||
@classmethod
|
||||
def shutdown(cls, event, sig = None, handler=None):
|
||||
logger.debug("shutdown...")
|
||||
cls._logger.debug("shutdown...")
|
||||
for t in LiveChat._listeners:
|
||||
t._is_alive = False
|
||||
@@ -1,31 +0,0 @@
|
||||
import logging
|
||||
import datetime
|
||||
|
||||
|
||||
def get_logger(modname,mode=logging.DEBUG):
|
||||
logger = logging.getLogger(modname)
|
||||
if mode == None:
|
||||
logger.addHandler(logging.NullHandler())
|
||||
return logger
|
||||
logger.setLevel(mode)
|
||||
#create handler1 for showing info
|
||||
handler1 = logging.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 = logging.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
|
||||
@@ -52,8 +52,8 @@ def _nval(val):
|
||||
buf += val.to_bytes(1,'big')
|
||||
return buf
|
||||
|
||||
def _build(video_id, seektime, topchatonly = False):
|
||||
switch_01 = b'\x04' if topchatonly else b'\x01'
|
||||
def _build(video_id, seektime, topchat_only):
|
||||
switch_01 = b'\x04' if topchat_only else b'\x01'
|
||||
if seektime < 0:
|
||||
times =_nval(0)
|
||||
switch = b'\x04'
|
||||
@@ -102,12 +102,14 @@ def _build(video_id, seektime, topchatonly = False):
|
||||
).decode()
|
||||
)
|
||||
|
||||
def getparam(video_id, seektime = 0):
|
||||
def getparam(video_id, seektime = 0, topchat_only = False):
|
||||
'''
|
||||
Parameter
|
||||
---------
|
||||
seektime : int
|
||||
unit:seconds
|
||||
start position of fetching chat data.
|
||||
topchat_only : bool
|
||||
if True, fetch only 'top chat'
|
||||
'''
|
||||
return _build(video_id, seektime)
|
||||
return _build(video_id, seektime, topchat_only)
|
||||
|
||||
@@ -66,9 +66,9 @@ def _nval(val):
|
||||
buf += val.to_bytes(1,'big')
|
||||
return buf
|
||||
|
||||
def _build(video_id, _ts1, _ts2, _ts3, _ts4, _ts5, topchatonly = False):
|
||||
def _build(video_id, _ts1, _ts2, _ts3, _ts4, _ts5, topchat_only):
|
||||
#_short_type2
|
||||
switch_01 = b'\x04' if topchatonly else b'\x01'
|
||||
switch_01 = b'\x04' if topchat_only else b'\x01'
|
||||
parity = _tzparity(video_id, _ts1^_ts2^_ts3^_ts4^_ts5)
|
||||
|
||||
header_magic= b'\xD2\x87\xCC\xC8\x03'
|
||||
@@ -155,12 +155,14 @@ def _times(past_sec):
|
||||
return list(map(lambda x:int(x*1000000),[_ts1,_ts2,_ts3,_ts4,_ts5]))
|
||||
|
||||
|
||||
def getparam(video_id,past_sec = 0):
|
||||
def getparam(video_id, past_sec = 0, topchat_only = False):
|
||||
'''
|
||||
Parameter
|
||||
---------
|
||||
past_sec : int
|
||||
seconds to load past chat data
|
||||
topchat_only : bool
|
||||
if True, fetch only 'top chat'
|
||||
'''
|
||||
return _build(video_id,*_times(past_sec))
|
||||
return _build(video_id,*_times(past_sec),topchat_only)
|
||||
|
||||
|
||||
@@ -5,16 +5,12 @@ Parser of live chat JSON.
|
||||
"""
|
||||
|
||||
import json
|
||||
from .. import config
|
||||
from .. exceptions import (
|
||||
ResponseContextError,
|
||||
NoContentsException,
|
||||
NoContinuationsException,
|
||||
ChatParseException )
|
||||
|
||||
|
||||
logger = config.logger(__name__)
|
||||
|
||||
class Parser:
|
||||
|
||||
__slots__ = ['is_replay']
|
||||
@@ -26,7 +22,8 @@ class Parser:
|
||||
if jsn is None:
|
||||
raise ChatParseException('Called with none JSON object.')
|
||||
if jsn['response']['responseContext'].get('errors'):
|
||||
raise ResponseContextError('The video_id would be wrong, or video is deleted or private.')
|
||||
raise ResponseContextError('The video_id would be wrong,'
|
||||
'or video is deleted or private.')
|
||||
contents=jsn['response'].get('continuationContents')
|
||||
return contents
|
||||
|
||||
@@ -64,12 +61,28 @@ class Parser:
|
||||
raise ChatParseException('Finished chat data')
|
||||
unknown = list(cont.keys())[0]
|
||||
if unknown:
|
||||
logger.debug(f"Received unknown continuation type:{unknown}")
|
||||
metadata = cont.get(unknown)
|
||||
raise ChatParseException(f"Received unknown continuation type:{unknown}")
|
||||
else:
|
||||
raise ChatParseException('Cannot extract continuation data')
|
||||
return self._create_data(metadata, contents)
|
||||
|
||||
def reload_continuation(self, contents):
|
||||
"""
|
||||
When `seektime = 0` or seektime is abbreviated ,
|
||||
check if fetched chat json has no chat data.
|
||||
If so, try to fetch playerSeekContinuationData.
|
||||
This function must be run only first fetching.
|
||||
"""
|
||||
cont = contents['liveChatContinuation']['continuations'][0]
|
||||
if cont.get("liveChatReplayContinuationData"):
|
||||
#chat data exist.
|
||||
return None
|
||||
#chat data do not exist, get playerSeekContinuationData.
|
||||
init_cont = cont.get("playerSeekContinuationData")
|
||||
if init_cont:
|
||||
return init_cont.get("continuation")
|
||||
raise ChatParseException('Finished chat data')
|
||||
|
||||
def _create_data(self, metadata, contents):
|
||||
actions = contents['liveChatContinuation'].get('actions')
|
||||
if self.is_replay:
|
||||
@@ -77,7 +90,8 @@ class Parser:
|
||||
metadata.setdefault("timeoutMs",interval)
|
||||
"""Archived chat has different structures than live chat,
|
||||
so make it the same format."""
|
||||
chatdata = [action["replayChatItemAction"]["actions"][0] for action in actions]
|
||||
chatdata = [action["replayChatItemAction"]["actions"][0]
|
||||
for action in actions]
|
||||
else:
|
||||
metadata.setdefault('timeoutMs', 10000)
|
||||
chatdata = actions
|
||||
|
||||
@@ -33,5 +33,6 @@ symbols = {
|
||||
"ARS\xa0": {"fxtext": "ARS", "jptext": "アルゼンチン・ペソ"},
|
||||
"CLP\xa0": {"fxtext": "CLP", "jptext": "チリ・ペソ"},
|
||||
"NOK\xa0": {"fxtext": "NOK", "jptext": "ノルウェー・クローネ"},
|
||||
"BAM\xa0": {"fxtext": "BAM", "jptext": "ボスニア・兌換マルカ"}
|
||||
"BAM\xa0": {"fxtext": "BAM", "jptext": "ボスニア・兌換マルカ"},
|
||||
"SGD\xa0": {"fxtext": "SGD", "jptext": "シンガポール・ドル"}
|
||||
}
|
||||
@@ -61,7 +61,7 @@ class DefaultProcessor(ChatProcessor):
|
||||
|
||||
renderer.get_snippet()
|
||||
renderer.get_authordetails()
|
||||
except (KeyError,TypeError,AttributeError) as e:
|
||||
except (KeyError,TypeError) as e:
|
||||
logger.error(f"{str(type(e))}-{str(e)} sitem:{str(sitem)}")
|
||||
return None
|
||||
return renderer
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
from datetime import datetime
|
||||
|
||||
class Author:
|
||||
pass
|
||||
class BaseRenderer:
|
||||
@@ -67,16 +66,17 @@ class BaseRenderer:
|
||||
badges=renderer.get("authorBadges")
|
||||
if badges:
|
||||
for badge in badges:
|
||||
author_type = badge["liveChatAuthorBadgeRenderer"]["accessibility"]["accessibilityData"]["label"]
|
||||
if author_type == '確認済み':
|
||||
isVerified = True
|
||||
if author_type == '所有者':
|
||||
isChatOwner = True
|
||||
if 'メンバー' in author_type:
|
||||
if badge["liveChatAuthorBadgeRenderer"].get("icon"):
|
||||
author_type = badge["liveChatAuthorBadgeRenderer"]["icon"]["iconType"]
|
||||
if author_type == 'VERIFIED':
|
||||
isVerified = True
|
||||
if author_type == 'OWNER':
|
||||
isChatOwner = True
|
||||
if author_type == 'MODERATOR':
|
||||
isChatModerator = True
|
||||
if badge["liveChatAuthorBadgeRenderer"].get("customThumbnail"):
|
||||
isChatSponsor = True
|
||||
self.get_badgeurl(badge)
|
||||
if author_type == 'モデレーター':
|
||||
isChatModerator = True
|
||||
return isVerified, isChatOwner, isChatSponsor, isChatModerator
|
||||
|
||||
|
||||
|
||||
@@ -33,5 +33,6 @@ symbols = {
|
||||
"ARS\xa0": {"fxtext": "ARS", "jptext": "アルゼンチン・ペソ"},
|
||||
"CLP\xa0": {"fxtext": "CLP", "jptext": "チリ・ペソ"},
|
||||
"NOK\xa0": {"fxtext": "NOK", "jptext": "ノルウェー・クローネ"},
|
||||
"BAM\xa0": {"fxtext": "BAM", "jptext": "ボスニア・兌換マルカ"}
|
||||
"BAM\xa0": {"fxtext": "BAM", "jptext": "ボスニア・兌換マルカ"},
|
||||
"SGD\xa0": {"fxtext": "SGD", "jptext": "シンガポール・ドル"}
|
||||
}
|
||||
@@ -10,13 +10,9 @@ class LiveChatPaidMessageRenderer(BaseRenderer):
|
||||
|
||||
def get_snippet(self):
|
||||
super().get_snippet()
|
||||
|
||||
self.author.name = self.renderer["authorName"]["simpleText"]
|
||||
|
||||
amountDisplayString, symbol, amount =(
|
||||
self.get_amountdata(self.renderer)
|
||||
)
|
||||
self.message = self.get_message(self.renderer)
|
||||
self.amountValue= amount
|
||||
self.amountString = amountDisplayString
|
||||
self.currency= currency.symbols[symbol]["fxtext"] if currency.symbols.get(symbol) else symbol
|
||||
|
||||
@@ -10,13 +10,9 @@ class LiveChatPaidStickerRenderer(BaseRenderer):
|
||||
|
||||
def get_snippet(self):
|
||||
super().get_snippet()
|
||||
|
||||
self.author.name = self.renderer["authorName"]["simpleText"]
|
||||
|
||||
amountDisplayString, symbol, amount =(
|
||||
self.get_amountdata(self.renderer)
|
||||
)
|
||||
self.message = ""
|
||||
self.amountValue = amount
|
||||
self.amountString = amountDisplayString
|
||||
self.currency = currency.symbols[symbol]["fxtext"] if currency.symbols.get(symbol) else symbol
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
import json
|
||||
from .chat_processor import ChatProcessor
|
||||
|
||||
class JsonDisplayProcessor(ChatProcessor):
|
||||
|
||||
def process(self,chat_components: list):
|
||||
if chat_components:
|
||||
for component in chat_components:
|
||||
chatdata = component.get('chatdata')
|
||||
if chatdata:
|
||||
for chat in chatdata:
|
||||
print(json.dumps(chat,ensure_ascii=False)[:200])
|
||||
|
||||
22
setup.py
22
setup.py
@@ -1,6 +1,6 @@
|
||||
from setuptools import setup, find_packages, Command
|
||||
#from codecs import open as open_c
|
||||
from os import path, system, remove, rename
|
||||
from os import path, system, remove, rename, removedirs
|
||||
import re
|
||||
|
||||
package_name = "pytchat"
|
||||
@@ -13,6 +13,15 @@ def _requirements():
|
||||
def _test_requirements():
|
||||
return [name.rstrip() for name in open(path.join(root_dir, 'requirements_test.txt')).readlines()]
|
||||
|
||||
txt= ''
|
||||
with open('README.MD', 'r', encoding='utf-8') as f:
|
||||
txt = f.read()
|
||||
|
||||
with open('README1.MD', 'w', encoding='utf-8', newline='\n') as f:
|
||||
f.write(txt)
|
||||
|
||||
remove("README.MD")
|
||||
rename("README1.MD","README.MD")
|
||||
|
||||
with open(path.join(root_dir, package_name, '__init__.py')) as f:
|
||||
init_text = f.read()
|
||||
@@ -30,14 +39,7 @@ assert url
|
||||
|
||||
|
||||
|
||||
with open('README.MD', 'r', encoding='utf-8') as f:
|
||||
txt = f.read()
|
||||
|
||||
with open('README1.MD', 'w', encoding='utf-8', newline='\n') as f:
|
||||
f.write(txt)
|
||||
|
||||
remove("README.MD")
|
||||
rename("README1.MD","README.MD")
|
||||
with open('README.md', encoding='utf-8') as f:
|
||||
long_description = f.read()
|
||||
|
||||
@@ -45,7 +47,7 @@ with open('README.md', encoding='utf-8') as f:
|
||||
|
||||
setup(
|
||||
name=package_name,
|
||||
packages=find_packages(),
|
||||
packages=find_packages(exclude=['*log.txt','*tests']),
|
||||
version=version,
|
||||
url=url,
|
||||
author=author,
|
||||
@@ -54,7 +56,7 @@ setup(
|
||||
long_description_content_type='text/markdown',
|
||||
license=license,
|
||||
install_requires=_requirements(),
|
||||
tests_require=_test_requirements(),
|
||||
#tests_require=_test_requirements(),
|
||||
description="a python library for fetching youtube live chat.",
|
||||
classifiers=[
|
||||
'Natural Language :: Japanese',
|
||||
|
||||
@@ -124,6 +124,21 @@ def test_superchat(mocker):
|
||||
assert "LCC." in ret["items"][0]["id"]
|
||||
assert ret["items"][0]["snippet"]["type"]=="superChatEvent"
|
||||
|
||||
def test_unregistered_currency(mocker):
|
||||
processor = CompatibleProcessor()
|
||||
|
||||
_json = _open_file("tests/testdata/unregistered_currency.json")
|
||||
|
||||
_, chatdata = parser.parse(parser.get_contents(json.loads(_json)))
|
||||
|
||||
data = {
|
||||
"video_id" : "",
|
||||
"timeout" : 7,
|
||||
"chatdata" : chatdata
|
||||
}
|
||||
ret = processor.process([data])
|
||||
assert ret["items"][0]["snippet"]["superChatDetails"]["currency"] == "[UNREGISTERD]"
|
||||
|
||||
|
||||
def _open_file(path):
|
||||
with open(path,mode ='r',encoding = 'utf-8') as f:
|
||||
|
||||
@@ -4,6 +4,6 @@ from pytchat.paramgen import liveparam
|
||||
def test_liveparam_0(mocker):
|
||||
_ts1= 1546268400
|
||||
param = liveparam._build("01234567890",
|
||||
*([_ts1*1000000 for i in range(5)]))
|
||||
*([_ts1*1000000 for i in range(5)]), topchat_only=False)
|
||||
test_param="0ofMyAPiARp8Q2c4S0RRb0xNREV5TXpRMU5qYzRPVEFhUTZxNXdiMEJQUW83YUhSMGNITTZMeTkzZDNjdWVXOTFkSFZpWlM1amIyMHZiR2wyWlY5amFHRjBQM1k5TURFeU16UTFOamM0T1RBbWFYTmZjRzl3YjNWMFBURWdBZyUzRCUzRCiAuNbVqsrfAjAAOABAAkorCAEQABgAIAAqDnN0YXRpY2NoZWNrc3VtOgBAAEoCCAFQgLjW1arK3wJYA1CAuNbVqsrfAliAuNbVqsrfAmgBggEECAEQAIgBAKABgLjW1arK3wI%3D"
|
||||
assert test_param == param
|
||||
160
tests/testdata/unregistered_currency.json
vendored
Normal file
160
tests/testdata/unregistered_currency.json
vendored
Normal file
@@ -0,0 +1,160 @@
|
||||
{
|
||||
"timing": {
|
||||
"info": {
|
||||
"st": 164
|
||||
}
|
||||
},
|
||||
"csn": "",
|
||||
"response": {
|
||||
"responseContext": {
|
||||
"serviceTrackingParams": [{
|
||||
"service": "CSI",
|
||||
"params": [{
|
||||
"key": "GetLiveChat_rid",
|
||||
"value": ""
|
||||
}, {
|
||||
"key": "c",
|
||||
"value": "WEB"
|
||||
}, {
|
||||
"key": "cver",
|
||||
"value": "2.20191219.03.01"
|
||||
}, {
|
||||
"key": "yt_li",
|
||||
"value": "0"
|
||||
}]
|
||||
}, {
|
||||
"service": "GFEEDBACK",
|
||||
"params": [{
|
||||
"key": "e",
|
||||
"value": ""
|
||||
}, {
|
||||
"key": "logged_in",
|
||||
"value": "0"
|
||||
}]
|
||||
}, {
|
||||
"service": "GUIDED_HELP",
|
||||
"params": [{
|
||||
"key": "logged_in",
|
||||
"value": "0"
|
||||
}]
|
||||
}, {
|
||||
"service": "ECATCHER",
|
||||
"params": [{
|
||||
"key": "client.name",
|
||||
"value": "WEB"
|
||||
}, {
|
||||
"key": "client.version",
|
||||
"value": "2.2"
|
||||
}, {
|
||||
"key": "innertube.build.changelist",
|
||||
"value": "228"
|
||||
}, {
|
||||
"key": "innertube.build.experiments.source_version",
|
||||
"value": "2858"
|
||||
}, {
|
||||
"key": "innertube.build.label",
|
||||
"value": "youtube.ytfe.innertube_"
|
||||
}, {
|
||||
"key": "innertube.build.timestamp",
|
||||
"value": "154"
|
||||
}, {
|
||||
"key": "innertube.build.variants.checksum",
|
||||
"value": "e"
|
||||
}, {
|
||||
"key": "innertube.run.job",
|
||||
"value": "ytfe-innertube-replica-only.ytfe"
|
||||
}]
|
||||
}],
|
||||
"webResponseContextExtensionData": {
|
||||
"ytConfigData": {
|
||||
"csn": "ADw",
|
||||
"visitorData": "%3D%3D"
|
||||
}
|
||||
}
|
||||
},
|
||||
"continuationContents": {
|
||||
"liveChatContinuation": {
|
||||
"continuations": [{
|
||||
"timedContinuationData": {
|
||||
"timeoutMs": 10000,
|
||||
"continuation": "continuation"
|
||||
}
|
||||
}],
|
||||
"actions": [{
|
||||
"addChatItemAction": {
|
||||
"item": {
|
||||
"liveChatPaidMessageRenderer": {
|
||||
"id": "dummy_id",
|
||||
"timestampUsec": "1576850000000000",
|
||||
"authorName": {
|
||||
"simpleText": "author_name"
|
||||
},
|
||||
"authorPhoto": {
|
||||
"thumbnails": [
|
||||
{
|
||||
"url": "https://yt3.ggpht.com/------------/AAAAAAAAAAA/AAAAAAAAAAA/xxxxxxxxxxxx/s32-x-x-xx-xx-xx-c0xffffff/photo.jpg",
|
||||
"width": 32,
|
||||
"height": 32
|
||||
},
|
||||
{
|
||||
"url": "https://yt3.ggpht.com/------------/AAAAAAAAAAA/AAAAAAAAAAA/xxxxxxxxxxxx/s32-x-x-xx-xx-xx-c0xffffff/photo.jpg",
|
||||
"width": 64,
|
||||
"height": 64
|
||||
}
|
||||
]
|
||||
},
|
||||
"purchaseAmountText": {
|
||||
"simpleText": "[UNREGISTERD]10,800"
|
||||
},
|
||||
"message": {
|
||||
"runs": [
|
||||
{
|
||||
"text": "This is unregistered currency."
|
||||
}
|
||||
]
|
||||
},
|
||||
"headerBackgroundColor": 4291821568,
|
||||
"headerTextColor": 4294967295,
|
||||
"bodyBackgroundColor": 4293271831,
|
||||
"bodyTextColor": 4294967295,
|
||||
"authorExternalChannelId": "http://www.youtube.com/channel/author_channel_url",
|
||||
"authorNameTextColor": 3019898879,
|
||||
"contextMenuEndpoint": {
|
||||
"commandMetadata": {
|
||||
"webCommandMetadata": {
|
||||
"ignoreNavigation": true
|
||||
}
|
||||
},
|
||||
"liveChatItemContextMenuEndpoint": {
|
||||
"params": "___params___"
|
||||
}
|
||||
},
|
||||
"timestampColor": 2164260863,
|
||||
"contextMenuAccessibility": {
|
||||
"accessibilityData": {
|
||||
"label": "コメントの操作"
|
||||
}
|
||||
}
|
||||
}
|
||||
}, "clientId": "00000000000000000000"
|
||||
}
|
||||
}
|
||||
]}
|
||||
},
|
||||
|
||||
"xsrf_token": "xsrf_token",
|
||||
"url": "/live_chat/get_live_chat?continuation=0",
|
||||
"endpoint": {
|
||||
"commandMetadata": {
|
||||
"webCommandMetadata": {
|
||||
"url": "/live_chat/get_live_chat?continuation=0",
|
||||
"rootVe": 0
|
||||
}
|
||||
},
|
||||
"urlEndpoint": {
|
||||
"url": "/live_chat/get_live_chat?continuation=0"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user