Fix JsonfileArchiveProcessor:
Rename to `JsonfileArchiver` Add tests
This commit is contained in:
@@ -1,46 +0,0 @@
|
||||
import json
|
||||
import os
|
||||
import datetime
|
||||
from .chat_processor import ChatProcessor
|
||||
|
||||
class JsonfileArchiveProcessor(ChatProcessor):
|
||||
def __init__(self,filepath):
|
||||
super().__init__()
|
||||
if os.path.exists(filepath):
|
||||
print('filepath is already exists!: ')
|
||||
print(' '+filepath)
|
||||
newpath=os.path.dirname(filepath) + \
|
||||
'/'+datetime.datetime.now() \
|
||||
.strftime('%Y-%m-%d %H-%M-%S')+'.data'
|
||||
|
||||
print('created alternate filename:')
|
||||
print(' '+newpath)
|
||||
self.filepath = newpath
|
||||
else:
|
||||
print('filepath: '+filepath)
|
||||
self.filepath = filepath
|
||||
|
||||
def process(self,chat_components: list):
|
||||
if chat_components:
|
||||
with open(self.filepath, mode='a', encoding = 'utf-8') as f:
|
||||
for component in chat_components:
|
||||
if component:
|
||||
chatdata = component.get('chatdata')
|
||||
for action in chatdata:
|
||||
if action:
|
||||
if action.get("addChatItemAction"):
|
||||
if action["addChatItemAction"]["item"].get(
|
||||
"liveChatViewerEngagementMessageRenderer"):
|
||||
continue
|
||||
s = json.dumps(action,ensure_ascii = False)
|
||||
#print(s[:200])
|
||||
f.writelines(s+'\n')
|
||||
|
||||
def _parsedir(self,_dir):
|
||||
if _dir[-1]=='\\' or _dir[-1]=='/':
|
||||
separator =''
|
||||
else:
|
||||
separator ='/'
|
||||
os.makedirs(_dir + separator, exist_ok=True)
|
||||
return _dir + separator
|
||||
|
||||
66
pytchat/processors/jsonfile_archiver.py
Normal file
66
pytchat/processors/jsonfile_archiver.py
Normal file
@@ -0,0 +1,66 @@
|
||||
import datetime
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
from .chat_processor import ChatProcessor
|
||||
|
||||
PATTERN = re.compile(r"(.*)\(([0-9]+)\)$")
|
||||
|
||||
class JsonfileArchiver(ChatProcessor):
|
||||
"""
|
||||
JsonfileArchiver saves chat data as text of JSON lines.
|
||||
|
||||
Parameter:
|
||||
----------
|
||||
save_path : str :
|
||||
save path of file.If a file with the same name exists,
|
||||
it is automatically saved under a different name
|
||||
with suffix '(number)'
|
||||
"""
|
||||
def __init__(self,save_path):
|
||||
super().__init__()
|
||||
self.save_path = self._checkpath(save_path)
|
||||
self.line_counter = 0
|
||||
|
||||
def process(self,chat_components: list):
|
||||
"""
|
||||
Returns
|
||||
----------
|
||||
dict :
|
||||
save_path : str :
|
||||
Actual save path of file.
|
||||
total_lines : int :
|
||||
count of total lines written to the file.
|
||||
"""
|
||||
if chat_components is None: return
|
||||
with open(self.save_path, mode='a', encoding = 'utf-8') as f:
|
||||
for component in chat_components:
|
||||
if component is None: continue
|
||||
chatdata = component.get('chatdata')
|
||||
if chatdata is None: continue
|
||||
for action in chatdata:
|
||||
if action is None: continue
|
||||
json_line = json.dumps(action, ensure_ascii = False)
|
||||
f.writelines(json_line+'\n')
|
||||
self.line_counter+=1
|
||||
return { "save_path" : self.save_path,
|
||||
"total_lines": self.line_counter }
|
||||
|
||||
def _checkpath(self, filepath):
|
||||
splitter = os.path.splitext(os.path.basename(filepath))
|
||||
body = splitter[0]
|
||||
extention = splitter[1]
|
||||
newpath = filepath
|
||||
counter = 0
|
||||
while os.path.exists(newpath):
|
||||
match = re.search(PATTERN,body)
|
||||
if match:
|
||||
counter=int(match[2])+1
|
||||
num_with_bracket = f'({str(counter)})'
|
||||
body = f'{match[1]}{num_with_bracket}'
|
||||
else:
|
||||
body = f'{body}({str(counter)})'
|
||||
newpath = os.path.join(os.path.dirname(filepath),body+extention)
|
||||
return newpath
|
||||
|
||||
|
||||
Reference in New Issue
Block a user