|
3 | 3 | import os |
4 | 4 | import hashlib |
5 | 5 | import json |
6 | | -from git.exc import GitCommandError |
7 | 6 | from googletrans import Translator |
8 | 7 |
|
9 | 8 | # Initialize the Google Translate API |
@@ -56,6 +55,9 @@ def generate_content_hash(content): |
56 | 55 | else: |
57 | 56 | processed_hashes = {} |
58 | 57 |
|
| 58 | +# Track whether there are new or updated posts |
| 59 | +has_changes = False |
| 60 | + |
59 | 61 | # Save each post as a file and commit |
60 | 62 | for entry in feed.entries: |
61 | 63 |
|
@@ -84,27 +86,23 @@ def generate_content_hash(content): |
84 | 86 |
|
85 | 87 | # If content is new, mark it as processed |
86 | 88 | processed_hashes[content_hash] = translated_name |
| 89 | + has_changes = True |
87 | 90 |
|
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.") |
108 | 106 |
|
109 | 107 | # Save the updated processed hashes to file |
110 | 108 | with open(hash_file_path, 'w', encoding='utf-8') as hash_file: |
|
0 commit comments