-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
71 lines (57 loc) · 2.01 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import argparse
from utils import (
deduplicate_messages,
extract_messages,
extract_title_from_response,
fetch_content_from_url,
sort_messages_by_time,
validate_link,
write_to_md_file,
)
def main():
"""Main function to fetch, process, and save conversation data."""
parser = argparse.ArgumentParser(description="Process ChatGPT share link.")
parser.add_argument(
"--share-link", required=True, help="The ChatGPT share link to process"
)
args = parser.parse_args()
sharable_link = args.share_link
if not validate_link(sharable_link):
print(f"Invalid share link format: {sharable_link}")
return False
try:
content = fetch_content_from_url(sharable_link)
except Exception as e:
print(
f"Error fetching content from the link: {sharable_link}. Error: {e}",
)
return False
if content is None or len(content) == 0:
print(
f"Failed to fetch or empty content from the link: {sharable_link}",
)
return False
pattern = r'"message":\{.*?"author":\{"role":"(.*?)".*?"create_time":([0-9.]+).*?"parts":\s*\[(.*?)\]'
title = extract_title_from_response(content)
# Extract and process the data
matches = extract_messages(content, pattern)
if not matches:
print(
f"Failed to extract messages from the content: {sharable_link}",
)
return False
deduplicated_data = deduplicate_messages(matches)
sorted_data = sort_messages_by_time(deduplicated_data)
output_filename = f"{sharable_link.split('/')[-1]}.md"
try:
output_file_path = write_to_md_file(sorted_data, title, output_filename)
except Exception as e:
print(
f"Error writing content to file: {output_filename}. Error: {e}",
)
return False
print(f"Sorted and deduplicated content has been saved to {output_file_path}")
return True
if __name__ == "__main__":
success = main()
exit(0 if success else 1)