Skip to content

Commit ab4de4e

Browse files
authored
Update update_blog.py
1 parent c7eea73 commit ab4de4e

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

scripts/update_blog.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import os
44
import hashlib
55
import json
6-
from git.exc import GitCommandError
76
from googletrans import Translator
87

98
# Initialize the Google Translate API
@@ -56,6 +55,9 @@ def generate_content_hash(content):
5655
else:
5756
processed_hashes = {}
5857

58+
# Track whether there are new or updated posts
59+
has_changes = False
60+
5961
# Save each post as a file and commit
6062
for entry in feed.entries:
6163

@@ -84,27 +86,23 @@ def generate_content_hash(content):
8486

8587
# If content is new, mark it as processed
8688
processed_hashes[content_hash] = translated_name
89+
has_changes = True
8790

88-
# Check if the file exists and if its content has changed
89-
content_changed = True
90-
new_content = entry.description
91-
if os.path.exists(file_path):
92-
with open(file_path, 'r', encoding='utf-8') as file:
93-
existing_content = file.read()
94-
if existing_content == new_content:
95-
content_changed = False
96-
97-
# Create or overwrite the file only if content has changed
98-
if content_changed:
99-
with open(file_path, 'w', encoding='utf-8') as file:
100-
file.write(new_content) # Write the post content into the file
101-
102-
# Stage and commit the changes
103-
repo.git.add(file_path)
104-
repo.git.commit('-m', f'Add or update post: {translated_name}')
105-
106-
# Push changes to repository
107-
repo.git.push()
91+
# Create or overwrite the file
92+
with open(file_path, 'w', encoding='utf-8') as file:
93+
file.write(entry.description)
94+
95+
# Stage the changes
96+
repo.git.add(file_path)
97+
print(f"Staged post: {translated_name}")
98+
99+
# Commit and push only if there were changes
100+
if has_changes:
101+
repo.git.commit('-m', 'Add or update new Velog posts')
102+
repo.git.push()
103+
print("Changes pushed to repository.")
104+
else:
105+
print("No new posts to update.")
108106

109107
# Save the updated processed hashes to file
110108
with open(hash_file_path, 'w', encoding='utf-8') as hash_file:

0 commit comments

Comments
 (0)