-
Notifications
You must be signed in to change notification settings - Fork 3
/
wordpress-cloning-img-in-css-script.py
76 lines (63 loc) · 2.63 KB
/
wordpress-cloning-img-in-css-script.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
72
73
74
75
76
import os
import sys
import requests
import shutil
from urllib.parse import urljoin, urlparse, urlunparse
from requests.auth import HTTPBasicAuth
from github_writer import GitHubWriter
from wordpress_image_downloader import download_image_if_not_exists
def extract_urls_from_style(style):
# Extract URLs from a CSS style string
urls = []
start = style.find('url(')
while start != -1:
start += 4 # Skip 'url('
end = style.find(')', start)
url = style[start:end].strip('\'"')
urls.append(url)
start = style.find('url(', end)
return urls
def download_and_update_css(environment, wordpress_staging_username, wordpress_staging_password):
writer = GitHubWriter()
headers = {
'Accept': 'application/json',
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
}
css_file = 'dist/wordpress-homepage.css'
folder_path = './dist/assets'
base_path = './assets'
# Setup authentication if not in production environment
auth = None
if environment != "PROD":
auth = HTTPBasicAuth(wordpress_staging_username, wordpress_staging_password)
# Create directory for saving images if it does not exist
if not os.path.exists(folder_path):
os.makedirs(folder_path)
if not os.path.exists(css_file):
writer.write_summary(f"- {css_file} does not exist, skipping...\n")
return
with open(css_file, "r") as file:
css_content = file.read()
# Find all URLs in the CSS content
urls = extract_urls_from_style(css_content)
for url in urls:
sanitized_filepath =download_image_if_not_exists(url, headers, auth, environment, writer)
new_url = os.path.join(base_path, os.path.basename(sanitized_filepath))
css_content = css_content.replace(url, new_url)
# Write the updated CSS content back to the file
with open(css_file, 'w') as file:
file.write(css_content)
if __name__ == "__main__":
writer = GitHubWriter()
try:
if len(sys.argv) != 4:
raise ValueError("Usage: python wordpress-css-script.py <environment> <wordpress_staging_username> <wordpress_staging_password>")
env = sys.argv[1]
wordpress_staging_username = sys.argv[2]
wordpress_staging_password = sys.argv[3]
download_and_update_css(env, wordpress_staging_username, wordpress_staging_password)
writer.write_output("script-success", "true")
except Exception as e:
writer.write_summary(f"Error: {str(e)}\n")
writer.write_output("script-success", "false")
sys.exit(1)