Dual format persistence fixes

This commit is contained in:
Piotr Dec 2025-04-30 13:39:31 +02:00
parent c36276aee5
commit 066232222f
No known key found for this signature in database
GPG key ID: D3B5A5D0150D147A

View file

@ -373,10 +373,11 @@ class SlackExporter:
self.export_user_list(True) self.export_user_list(True)
for channel_id in channels: for channel_id in channels:
print(f"Eksport {channels_map.get(channel_id).label}...") print(f"Eksport {channels_map.get(channel_id).label}...")
print(f"[{channel_id}] Historia")
self.export_channel_history(channel_id) self.export_channel_history(channel_id)
self.export_channel_history(channel_id, as_json=True) print(f"[{channel_id}] Odpowiedzi")
self.export_channel_replies(channel_id) self.export_channel_replies(channel_id)
self.export_channel_replies(channel_id, as_json=True) print(f"[{channel_id}] Pliki")
self.export_channel_files(channel_id) self.export_channel_files(channel_id)
def export_channel_list(self, as_json: bool = False): def export_channel_list(self, as_json: bool = False):
@ -396,10 +397,13 @@ class SlackExporter:
self._save_data(data, "user_list", as_json) self._save_data(data, "user_list", as_json)
def export_channel_history(self, channel_id: str, oldest: Optional[str] = None, def export_channel_history(self, channel_id: str, oldest: Optional[str] = None,
latest: Optional[str] = None, as_json: bool = False): latest: Optional[str] = None):
"""Eksportuje historię kanału"""
history = self.api.get_channel_history(channel_id, oldest, latest) history = self.api.get_channel_history(channel_id, oldest, latest)
self._export_channel_history(channel_id, history, True)
self._export_channel_history(channel_id, history, False)
def _export_channel_history(self, channel_id: str, history: List[Dict], as_json: bool):
"""Eksportuje historię kanału"""
if as_json: if as_json:
data = history data = history
else: else:
@ -416,16 +420,18 @@ class SlackExporter:
self._save_data(data, f"channel_{channel_id}", as_json) self._save_data(data, f"channel_{channel_id}", as_json)
def export_channel_replies(self, channel_id: str, oldest: Optional[str] = None, def export_channel_replies(self, channel_id: str, oldest: Optional[str] = None,
latest: Optional[str] = None, as_json: bool = False): latest: Optional[str] = None):
"""Eksportuje wątki w kanale"""
history = self.api.get_channel_history(channel_id, oldest, latest) history = self.api.get_channel_history(channel_id, oldest, latest)
thread_messages = [msg for msg in history if "reply_count" in msg] thread_messages = [msg for msg in history if "reply_count" in msg]
all_replies = [] all_replies = []
for msg in thread_messages: for msg in thread_messages:
replies = self.api.get_replies(channel_id, msg["ts"]) replies = self.api.get_replies(channel_id, msg["ts"])
all_replies.extend(replies) all_replies.extend(replies)
self._export_channel_replies(channel_id, all_replies, True)
self._export_channel_replies(channel_id, all_replies, False)
def _export_channel_replies(self, channel_id: str, all_replies: List[Dict], as_json: bool):
"""Eksportuje wątki w kanale"""
if as_json: if as_json:
data = all_replies data = all_replies
else: else:
@ -459,6 +465,7 @@ class SlackExporter:
with open(target, 'wb') as f: with open(target, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192): for chunk in response.iter_content(chunk_size=8192):
f.write(chunk) f.write(chunk)
print(f"Zapisano plik {target}")
return True return True
except requests.exceptions.RequestException as e: except requests.exceptions.RequestException as e:
print(f"Error downloading file {filename}: {e}. {attempts} attempts left.") print(f"Error downloading file {filename}: {e}. {attempts} attempts left.")