From be557c43e1bf1bb3f1ef6ca706f5e0db97fd7c4c Mon Sep 17 00:00:00 2001
From: Zaki-1052 <134018102+Zaki-1052@users.noreply.github.com>
Date: Mon, 29 Jul 2024 13:42:43 -0700
Subject: [PATCH] backup script fix
---
public/uploads/backup.py | 108 --------------------------------
public/uploads/chatBackup.py | 116 +++++++++++++++++++++++++++++++++++
2 files changed, 116 insertions(+), 108 deletions(-)
delete mode 100644 public/uploads/backup.py
create mode 100644 public/uploads/chatBackup.py
diff --git a/public/uploads/backup.py b/public/uploads/backup.py
deleted file mode 100644
index 834d224..0000000
--- a/public/uploads/backup.py
+++ /dev/null
@@ -1,108 +0,0 @@
-import os
-import shutil
-from datetime import datetime
-import re
-
-# Source and destination paths
-source_path = "path/to/repo/GPTPortal/public/uploads/chats"
-icloud_path = "/Users/your_username/Library/Mobile Documents/com~apple~CloudDocs/ChatGPT/chats"
-
-def format_content(content):
- lines = content.split('\n')
- formatted_content = []
-
- for line in lines:
- if line.startswith("System:") or line.startswith("Human:") or line.startswith("Assistant:"):
- formatted_content.append(f"\n{line}\n")
- else:
- formatted_content.append(line)
-
- return '\n'.join(formatted_content)
-
-def create_html(content, filename):
- html_content = f"""
-
-
-
-
-
- {filename}
-
-
-
- """
-
- lines = content.split('\n')
- current_speaker = None
- for line in lines:
- if line.startswith("Human:"):
- if current_speaker != "human":
- if current_speaker:
- html_content += ""
- html_content += ''
- current_speaker = "human"
- elif line.startswith("Assistant:"):
- if current_speaker != "assistant":
- if current_speaker:
- html_content += "
"
- html_content += ''
- current_speaker = "assistant"
-
- html_content += f"{line}
"
-
- if current_speaker:
- html_content += "
"
-
- html_content += """
-
-
- """
-
- return html_content
-
-def move_and_format_files():
- # Create the destination folder if it doesn't exist
- os.makedirs(icloud_path, exist_ok=True)
-
- # Get the current timestamp
- timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
-
- # Iterate through files in the source directory
- for filename in os.listdir(source_path):
- if filename.endswith(".txt"):
- source_file = os.path.join(source_path, filename)
-
- # Read the content of the file
- with open(source_file, 'r', encoding='utf-8') as file:
- content = file.read()
-
- # Format the content
- formatted_content = format_content(content)
-
- # Create new filenames with timestamp
- base_name = os.path.splitext(filename)[0]
- new_txt_filename = f"{base_name}_{timestamp}.txt"
- new_html_filename = f"{base_name}_{timestamp}.html"
-
- # Save formatted content as .txt
- txt_destination = os.path.join(icloud_path, new_txt_filename)
- with open(txt_destination, 'w', encoding='utf-8') as file:
- file.write(formatted_content)
-
- # Save formatted content as .html
- html_content = create_html(formatted_content, base_name)
- html_destination = os.path.join(icloud_path, new_html_filename)
- with open(html_destination, 'w', encoding='utf-8') as file:
- file.write(html_content)
-
- # Remove the original file
- os.remove(source_file)
-
- print(f"Moved and formatted: {filename} -> {new_txt_filename} and {new_html_filename}")
-
-if __name__ == "__main__":
- move_and_format_files()
\ No newline at end of file
diff --git a/public/uploads/chatBackup.py b/public/uploads/chatBackup.py
new file mode 100644
index 0000000..4d802a7
--- /dev/null
+++ b/public/uploads/chatBackup.py
@@ -0,0 +1,116 @@
+import os
+import shutil
+from datetime import datetime
+import re
+
+# Source and destination paths
+source_path = "/Users/zakiralibhai/Documents/VS_Code/MyGPTPortal/public/uploads/chats"
+html_source_path = "/Users/zakiralibhai/Documents/HTML_ChatGPTs"
+icloud_base_path = "/Users/zakiralibhai/Library/Mobile Documents/com~apple~CloudDocs/ChatGPT"
+
+# Create subdirectories
+txt_path = os.path.join(icloud_base_path, "txt")
+processed_markdown_path = os.path.join(icloud_base_path, "markdown")
+original_html_path = os.path.join(icloud_base_path, "html")
+
+def ensure_dir(directory):
+ if not os.path.exists(directory):
+ os.makedirs(directory)
+
+def format_filename(filename):
+ # Remove any existing date pattern and file extension
+ base_name = re.sub(r'-\d{2}-\d{2}.*$', '', os.path.splitext(filename)[0])
+ # Add current date
+ current_date = datetime.now().strftime("-%m-%d")
+ return f"{base_name}{current_date}"
+
+def format_content(content):
+ formatted_content = []
+ current_speaker = None
+ in_summary = False
+ in_context = False
+
+ for line in content.split('\n'):
+ line = line.strip()
+ if not line:
+ formatted_content.append("")
+ continue
+
+ if line.startswith(("System:", "Human:", "Assistant:")):
+ if current_speaker:
+ formatted_content.append("") # Add a blank line between speakers
+ current_speaker = line.split(":")[0]
+ formatted_content.append(f"## {line}")
+ elif line.startswith("---"):
+ if "Conversation Summary:" in line:
+ in_summary = True
+ formatted_content.append("\n## Summary")
+ else:
+ formatted_content.append(line)
+ elif line.startswith("CONTEXT:"):
+ in_context = True
+ formatted_content.append("\n## Context")
+ elif line.startswith(("Total Tokens:", "Total Cost:")):
+ formatted_content.append(f"**{line}**")
+ elif in_summary or in_context:
+ formatted_content.append(line)
+ else:
+ formatted_content.append(line)
+
+ return '\n'.join(formatted_content)
+
+def create_markdown(content, filename):
+ markdown_content = f"# {filename}\n\n"
+ markdown_content += content
+ return markdown_content
+
+def process_txt_files():
+ for filename in os.listdir(source_path):
+ if filename.endswith(".txt"):
+ source_file = os.path.join(source_path, filename)
+ new_filename = format_filename(filename)
+
+ # Move txt file to txt_path without modification
+ txt_destination = os.path.join(txt_path, f"{new_filename}.txt")
+ shutil.copy2(source_file, txt_destination)
+
+ # Read the content of the file
+ with open(source_file, 'r', encoding='utf-8') as file:
+ content = file.read()
+
+ # Format the content and create Markdown
+ formatted_content = format_content(content)
+ markdown_content = create_markdown(formatted_content, new_filename)
+
+ # Save as Markdown
+ markdown_destination = os.path.join(processed_markdown_path, f"{new_filename}.md")
+ with open(markdown_destination, 'w', encoding='utf-8') as file:
+ file.write(markdown_content)
+
+ print(f"Processed: {filename} -> {new_filename}.txt and {new_filename}.md")
+
+ # Remove the original file from source_path
+ os.remove(source_file)
+
+def move_html_files():
+ for filename in os.listdir(html_source_path):
+ if filename.endswith(".html"):
+ source_file = os.path.join(html_source_path, filename)
+ new_filename = format_filename(filename)
+ destination = os.path.join(original_html_path, f"{new_filename}.html")
+ shutil.copy2(source_file, destination)
+ print(f"Moved: {filename} -> {new_filename}.html")
+
+def main():
+ # Ensure all directories exist
+ for directory in [txt_path, processed_markdown_path, original_html_path]:
+ ensure_dir(directory)
+
+ # Process text files
+ process_txt_files()
+
+ # Move HTML files
+ move_html_files()
+
+if __name__ == "__main__":
+ main()