-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTML-CleanUP-Script.py
More file actions
282 lines (209 loc) · 9.98 KB
/
HTML-CleanUP-Script.py
File metadata and controls
282 lines (209 loc) · 9.98 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import os
import zipfile
import yaml
import re
import urllib.parse
from urllib.parse import unquote
from bs4 import BeautifulSoup
# Specify the YAML file path directly
yaml_file_path = "config.yml"
# Load YAML configuration
with open(yaml_file_path, 'r') as yaml_file:
config = yaml.safe_load(yaml_file)
# Get the path where to extract the contents
extract_to = config.get('extract_to')
if not extract_to:
print("Error: 'extract_to' path not found in the YAML file.")
exit(1)
video_snippet = config.get('Video_html_Code_Snippet')
if not video_snippet:
print("Error: 'extract_to' path not found in the YAML file.")
exit(1)
Head_snippet = config.get('html_Head_Code_Snippet')
if not Head_snippet:
print("Error: 'extract_to' path not found in the YAML file.")
exit(1)
def get_active_zip_file(folder_path):
zip_files = [file for file in os.listdir(folder_path) if file.endswith('.zip')]
if not zip_files:
print("No zip files found in the folder.")
return None
return zip_files[0] # Return the first zip file found
def unzip_folder(zip_file, extract_to):
with zipfile.ZipFile(zip_file, 'r') as zip_ref:
zip_ref.extractall(extract_to)
print(f"Folder '{zip_file}' successfully extracted to '{extract_to}'")
def rename_folder_and_html(folder_path, old_folder_name):
old_folder_path = os.path.join(folder_path, old_folder_name)
new_folder_name = re.sub(r' [a-f0-9]+$', '', old_folder_name)
new_folder_path = os.path.join(folder_path, new_folder_name)
os.rename(old_folder_path, new_folder_path)
print(f"Folder '{old_folder_name}' renamed to '{new_folder_name}'")
old_html_file_path = os.path.join(folder_path, old_folder_name + ".html")
new_html_file_path = os.path.join(folder_path, new_folder_name + ".html")
os.rename(old_html_file_path, new_html_file_path)
print(f"File '{old_folder_name}.html' renamed to '{new_folder_name}.html'")
return new_html_file_path
def replace_and_write_path(path):
modified_path = path.replace("%26", "&")
print("Modified path:", modified_path)
return modified_path
def find_replace_html_file(html_file, old_path, new_path):
try:
# Open the HTML file for reading
with open(html_file, 'r', encoding='utf-8') as file:
html_content = file.read()
# Perform the find and replace operation
updated_content = html_content.replace(old_path, new_path)
# Write the updated content back to the file
with open(html_file, 'w', encoding='utf-8') as file:
file.write(updated_content)
print(f"File location '{old_path}' replaced with '{new_path}' in '{html_file}' successfully.")
except FileNotFoundError:
print("Error: HTML file not found.")
except Exception as e:
print(f"An error occurred: {e}")
#video html tag replacement
def replace_video_html_in_file(html_file):
try:
# Read the HTML content from the file
with open(html_file, 'r', encoding='utf-8') as file:
html_content = file.read()
# Define the pattern to match the existing video link
pattern = r'<div class="source"><a href="([^"]+)">([^<]+)</a></div>'
# Perform the replacement
updated_content = re.sub(pattern, replacement_callback, html_content)
# Write the updated content back to the file
with open(html_file, 'w', encoding='utf-8') as file:
file.write(updated_content)
print(f"HTML content in '{html_file}' replaced successfully.")
except FileNotFoundError:
print("Error: HTML file not found.")
except Exception as e:
print(f"An error occurred: {e}")
def replacement_callback(match):
old_link = match.group(1)
video_file = unquote(old_link)
new_link = f'./{video_file}'
# replacement = f'<br><div style="position: rselative; display: flex; justify-content: center; align-items: center;"><video playsinline="" controls="" preload="metadata" src="{new_link}" style="display: block; width: 80%; background-color: rgba(255, 255, 255, 0.03);"></video></div><br>'
replacement = video_snippet.format(new_link=new_link)
return replacement
def replace_html_content(html_file, old_content, new_content):
# Read the HTML file
with open(html_file, 'r', encoding='utf-8') as f:
html_content = f.read()
# Replace the old content with the new content
updated_html = html_content.replace(old_content, new_content)
# Write the updated HTML content back to the file
with open(html_file, 'w', encoding='utf-8') as f:
f.write(updated_html)
print("HTML content replaced successfully.")
#video html tag replacement
def replace_video_html_in_file(html_file):
try:
# Read the HTML content from the file
with open(html_file, 'r', encoding='utf-8') as file:
html_content = file.read()
# Define the pattern to match the existing video link
pattern = r'<div class="source"><a href="([^"]+)">([^<]+)</a></div>'
# Perform the replacement
updated_content = re.sub(pattern, replacement_callback, html_content)
# Write the updated content back to the file
with open(html_file, 'w', encoding='utf-8') as file:
file.write(updated_content)
print(f"HTML content in '{html_file}' replaced successfully.")
except FileNotFoundError:
print("Error: HTML file not found.")
except Exception as e:
print(f"An error occurred: {e}")
#replacing the spaces
def replace_html_in_file(html_file):
try:
# Read the HTML content from the file
with open(html_file, 'r', encoding='utf-8') as file:
html_content = file.read()
# Define the pattern to match the existing content
pattern = r'<p\s+id="([^"]+)"\s*class="">\s*</p>'
# Perform the replacement
updated_content = re.sub(pattern, '<br>', html_content)
# Write the updated content back to the file
with open(html_file, 'w', encoding='utf-8') as file:
file.write(updated_content)
print(f"HTML content in '{html_file}' replaced successfully.")
except FileNotFoundError:
print("Error: HTML file not found.")
except Exception as e:
print(f"An error occurred: {e}")
def format_html_file(input_file):
# Read the HTML content from the input file
with open(input_file, 'r', encoding='utf-8') as f:
html_content = f.read()
# Parse the HTML content
soup = BeautifulSoup(html_content, 'html.parser')
# Prettify the parsed HTML content
formatted_html = soup.prettify()
# Overwrite the input file with the formatted HTML content
with open(input_file, 'w', encoding='utf-8') as f:
f.write(formatted_html)
print("HTML document formatted and saved successfully.")
if __name__ == "__main__":
# Get the folder path
folder_path = os.path.dirname(os.path.abspath(yaml_file_path))
# Ensure that the destination folder exists, create it if not
if not os.path.exists(extract_to):
os.makedirs(extract_to)
# Get the active zip file in the folder
zip_file = get_active_zip_file(folder_path)
if not zip_file:
exit(1)
# Extract the contents to a temporary location
temp_extract_to = os.path.join(folder_path, 'temp_extract')
unzip_folder(os.path.join(folder_path, zip_file), temp_extract_to)
# Find the folder and file with unique IDs
unique_id_folder_name = os.listdir(temp_extract_to)[0]
# unique_id_html_file_name = os.listdir(os.path.join(temp_extract_to, unique_id_folder_name))[0]
unique_id_html_file_name = unique_id_folder_name + ".html"
# print(unique_id_html_file_name)
# Remove the unique IDs from folder and file names
rename_folder_and_html(temp_extract_to, unique_id_folder_name)
# Update paths in the HTML file
# print(unique_id_html_file_name)
new_folder_name = re.sub(r' [a-f0-9]+$', '', unique_id_folder_name)
unique_id_html_file_name = new_folder_name + ".html"
html_file_path = os.path.join(temp_extract_to, unique_id_html_file_name)
print(html_file_path)
print(new_folder_name)
html_file = html_file_path
old_path = urllib.parse.quote(unique_id_folder_name) +'/'
# print("this is before: "+old_path)
old_path = replace_and_write_path(old_path)
# print("this is after: "+old_path)
new_path = new_folder_name+'/'
find_replace_html_file(html_file, old_path, new_path)
# Get the path where to extract the contents
replace_video_html_in_file(html_file)
#replacing the head html tags with proper tags
old_content = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>'
replace_html_content(html_file, old_content, Head_snippet)
#replacing the intent spaces in htm file
replace_html_in_file(html_file)
#replacing the head html tags with proper tags
old_content = 'white-space: pre-wrap;'
new_content = '/*white-space: pre-wrap;*/'
replace_html_content(html_file, old_content,new_content )
format_html_file(html_file)
# # Move the folder to the final destination
final_extract_to = os.path.join(folder_path, config['extract_to'])
# Move the folder and HTML file to the final destination
final_folder_path = os.path.join(final_extract_to, new_folder_name)
# os.makedirs(final_folder_path, exist_ok=True) # Create the folder if it doesn't exist
# os.rename(os.path.join(temp_extract_to, new_folder_name), final_folder_path)
os.rename(os.path.join(temp_extract_to, new_folder_name), os.path.join(final_extract_to, new_folder_name))
os.rename(os.path.join(temp_extract_to, new_folder_name + ".html"), os.path.join(final_extract_to, new_folder_name + ".html"))
print("Folder and HTML file moved to the final destination")
# Delete the temporary extraction directory
os.rmdir(temp_extract_to)
print("Temporary extraction directory deleted")
# Delete the zip file after extraction
os.remove(os.path.join(folder_path, zip_file))
print(f"Zip file '{zip_file}' deleted.")