-
Notifications
You must be signed in to change notification settings - Fork 3
/
wordpress-cloning-js-script.py
75 lines (64 loc) · 2.99 KB
/
wordpress-cloning-js-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
import sys
import requests
from requests.auth import HTTPBasicAuth
from github_writer import GitHubWriter
def fetch_and_write_js(url, file, headers, auth, writer):
try:
response = requests.get(url, headers=headers, auth=auth)
if response.status_code == 200:
file.write(f"/* JS from {url} */\n")
file.write(response.text + "\n\n")
message = f"Successfully added JS from {url}"
writer.write_summary(f"- {message}\n")
else:
message = f"Skipping {url}: HTTP {response.status_code}"
writer.write_summary(f"- {message}\n")
except requests.exceptions.RequestException as e:
message = f"Skipping {url}. \n Error: {e}"
writer.write_summary(f"- {message}\n")
def download_js_files(environment, wordpress_staging_username, wordpress_staging_password):
writer = GitHubWriter()
base_url = "https://info.qa.orcid.org/" if environment != "PROD" else "https://info.orcid.org/"
# List of JS URLs to fetch
js_urls = [
f"{base_url}wp-includes/js/jquery/jquery.min.js",
f"{base_url}wp-includes/js/jquery/jquery-migrate.min.js",
f"{base_url}/wp-content/plugins/so-widgets-bundle/widgets/tabs/js/tabs.min.js",
# Add more JS URLs as needed
]
# Set authentication for non-PROD environments
auth = None
if environment != "PROD":
auth = HTTPBasicAuth(wordpress_staging_username, wordpress_staging_password)
headers = {
"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"
}
with open('dist/wordpress-homepage.js', 'w') as file:
# Read and write the adapter JS from wordpress-adapter.js
try:
with open('wordpress-adapter.js', 'r') as adapter_file:
adapter_js = adapter_file.read()
file.write("/* Adapter JS to ensure scripts can run outside of WordPress */\n")
file.write(adapter_js + "\n\n")
except FileNotFoundError:
message = "Error: wordpress-adapter.js file not found."
writer.write_summary(f"{message}\n")
writer.write_output("script-success", "false")
sys.exit(1)
# Fetch and write each JS file
for url in js_urls:
fetch_and_write_js(url, file, headers, auth, writer)
if __name__ == "__main__":
writer = GitHubWriter()
try:
if len(sys.argv) != 4:
raise ValueError("Usage: python js-cloning-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_js_files(env, wordpress_staging_username, wordpress_staging_password)
except Exception as e:
message = f"Error: {str(e)}"
writer.write_summary(f"{message}\n")
writer.write_output("script-success", "false")
sys.exit(1)