Skip to content
This repository was archived by the owner on Jan 25, 2022. It is now read-only.

Commit 316fc55

Browse files
committed
Merge branch 'release/v0.0.6.4'
2 parents 36e0fd5 + 44dffc7 commit 316fc55

File tree

16 files changed

+342
-651
lines changed

16 files changed

+342
-651
lines changed

README.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,23 @@ For more detailed information, see [wiki](https://github.com/taizan-hokuto/pytch
2222
```python
2323
pip install pytchat
2424
```
25-
## Demo
26-
![demo](https://taizan-hokuto.github.io/statics/demo.gif "demo")
27-
2825
## Examples
26+
27+
### CLI
28+
29+
One-liner command.
30+
Save chat data to html.
31+
32+
```bash
33+
$ pytchat -v ZJ6Q4U_Vg6s -o "c:/temp/"
34+
35+
# options:
36+
# -v : video_id
37+
# -o : output directory (default path: './')
38+
# saved filename is [video_id].html
39+
```
40+
41+
2942
### on-demand mode
3043
```python
3144
from pytchat import LiveChat
@@ -263,6 +276,15 @@ Structure of author object.
263276

264277
[![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](LICENSE)
265278

279+
280+
## Contributes
281+
Great thanks:
282+
283+
Most of source code of CLI refer to:
284+
285+
[PetterKraabol / Twitch-Chat-Downloader](https://github.com/PetterKraabol/Twitch-Chat-Downloader)
286+
287+
266288
## Author
267289

268290
[taizan-hokuto](https://github.com/taizan-hokuto)

pytchat/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
pytchat is a python library for fetching youtube live chat without using yt api, Selenium, or BeautifulSoup.
33
"""
44
__copyright__ = 'Copyright (C) 2019 taizan-hokuto'
5-
__version__ = '0.0.6.3'
5+
__version__ = '0.0.6.4'
66
__license__ = 'MIT'
77
__author__ = 'taizan-hokuto'
88
__author_email__ = '55448286+taizan-hokuto@users.noreply.github.com'
@@ -11,6 +11,7 @@
1111
__all__ = ["core_async","core_multithread","processors"]
1212

1313
from .api import (
14+
cli,
1415
config,
1516
LiveChat,
1617
LiveChatAsync,
@@ -19,6 +20,8 @@
1920
DummyProcessor,
2021
DefaultProcessor,
2122
Extractor,
23+
HTMLArchiver,
24+
TSVArchiver,
2225
JsonfileArchiver,
2326
SimpleDisplayProcessor,
2427
SpeedCalculator,

pytchat/api.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
from . import cli
12
from . import config
23
from .core_multithread.livechat import LiveChat
34
from .core_async.livechat import LiveChatAsync
45
from .processors.chat_processor import ChatProcessor
56
from .processors.compatible.processor import CompatibleProcessor
67
from .processors.default.processor import DefaultProcessor
78
from .processors.dummy_processor import DummyProcessor
9+
from .processors.html_archiver import HTMLArchiver
10+
from .processors.tsv_archiver import TSVArchiver
811
from .processors.jsonfile_archiver import JsonfileArchiver
912
from .processors.simple_display_processor import SimpleDisplayProcessor
1013
from .processors.speed.calculator import SpeedCalculator

pytchat/cli/__init__.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import argparse
2+
import os
3+
from pathlib import Path
4+
from typing import List, Callable
5+
from .arguments import Arguments
6+
7+
from .. exceptions import InvalidVideoIdException, NoContentsException
8+
from .. processors.tsv_archiver import TSVArchiver
9+
from .. processors.html_archiver import HTMLArchiver
10+
from .. tool.extract.extractor import Extractor
11+
from .. tool.videoinfo import VideoInfo
12+
from .. import __version__
13+
14+
'''
15+
Most of CLI modules refer to
16+
Petter Kraabøl's Twitch-Chat-Downloader
17+
https://github.com/PetterKraabol/Twitch-Chat-Downloader
18+
(MIT License)
19+
20+
'''
21+
def main():
22+
# Arguments
23+
parser = argparse.ArgumentParser(description=f'pytchat v{__version__}')
24+
parser.add_argument('-v', f'--{Arguments.Name.VIDEO}', type=str,
25+
help='Video IDs separated by commas without space')
26+
parser.add_argument('-o', f'--{Arguments.Name.OUTPUT}', type=str,
27+
help='Output directory (end with "/")', default='./')
28+
parser.add_argument(f'--{Arguments.Name.VERSION}', action='store_true',
29+
help='Settings version')
30+
Arguments(parser.parse_args().__dict__)
31+
if Arguments().print_version:
32+
print(f'pytchat v{__version__}')
33+
return
34+
35+
# Extractor
36+
if Arguments().video_ids:
37+
for video_id in Arguments().video_ids:
38+
try:
39+
info = VideoInfo(video_id)
40+
print(f"Extracting...\n"
41+
f" video_id: {video_id}\n"
42+
f" channel: {info.get_channel_name()}\n"
43+
f" title: {info.get_title()}")
44+
Extractor(video_id,
45+
processor = HTMLArchiver(Arguments().output+video_id+'.html')
46+
).extract()
47+
print("Extraction end.\n")
48+
except (InvalidVideoIdException, NoContentsException) as e:
49+
print(e)
50+
return
51+
parser.print_help()

pytchat/cli/arguments.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from typing import Optional, Dict, Union, List
2+
from .singleton import Singleton
3+
4+
'''
5+
This modules refer to
6+
Petter Kraabøl's Twitch-Chat-Downloader
7+
https://github.com/PetterKraabol/Twitch-Chat-Downloader
8+
(MIT License)
9+
'''
10+
11+
class Arguments(metaclass=Singleton):
12+
"""
13+
Arguments singleton
14+
"""
15+
16+
class Name:
17+
VERSION: str = 'version'
18+
OUTPUT: str = 'output'
19+
VIDEO: str = 'video'
20+
21+
def __init__(self,
22+
arguments: Optional[Dict[str, Union[str, bool, int]]] = None):
23+
"""
24+
Initialize arguments
25+
:param arguments: Arguments from cli
26+
(Optional to call singleton instance without parameters)
27+
"""
28+
29+
if arguments is None:
30+
print('Error: arguments were not provided')
31+
exit()
32+
33+
self.print_version: bool = arguments[Arguments.Name.VERSION]
34+
self.output: str = arguments[Arguments.Name.OUTPUT]
35+
self.video_ids: List[int] = []
36+
# Videos
37+
if arguments[Arguments.Name.VIDEO]:
38+
self.video_ids = [video_id
39+
for video_id in arguments[Arguments.Name.VIDEO].split(',')]

pytchat/cli/singleton.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'''
2+
This modules refer to
3+
Petter Kraabøl's Twitch-Chat-Downloader
4+
https://github.com/PetterKraabol/Twitch-Chat-Downloader
5+
(MIT License)
6+
'''
7+
class Singleton(type):
8+
"""
9+
Abstract class for singletons
10+
"""
11+
_instances = {}
12+
13+
def __call__(cls, *args, **kwargs):
14+
if cls not in cls._instances:
15+
cls._instances[cls] = super().__call__(*args, **kwargs)
16+
return cls._instances[cls]
17+
18+
def get_instance(cls, *args, **kwargs):
19+
cls.__call__(*args, **kwargs)

pytchat/config/mylogger.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from logging import NullHandler, getLogger, StreamHandler, FileHandler, Formatter
22
import logging
3-
import datetime
3+
from datetime import datetime
44

55

66
def get_logger(modname,loglevel=logging.DEBUG):
@@ -28,5 +28,11 @@ def get_logger(modname,loglevel=logging.DEBUG):
2828

2929
class MyFormatter(logging.Formatter):
3030
def format(self, record):
31-
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()
32-
return s
31+
timestamp = (
32+
datetime.fromtimestamp(record.created)).strftime("%m-%d %H:%M:%S")
33+
module = (record.module).ljust(15)
34+
funcname = (record.funcName).ljust(18)
35+
lineno = str(record.lineno).rjust(4)
36+
message = record.getMessage()
37+
38+
return timestamp+'| '+module+' { '+funcname+':'+lineno+'} - '+message

0 commit comments

Comments
 (0)