-
Notifications
You must be signed in to change notification settings - Fork 0
/
asset_proxy_cleanup.py
237 lines (217 loc) · 9.48 KB
/
asset_proxy_cleanup.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
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
# file: asset_proxy_cleanup.py
# This script will report on assets in LibGuides with URLs that are proxied
# inappropriately. Link and Book assets will be checked for a proxy prefix on
# the URL or for a URL with a domain in the exception list that should not be
# proxied. The `--update` flag will update the assets with the appropriate
# changes.
# Required values in `settings.ini` include:
#
# - LIBGUIDES_API_SITE_ID
# - LIBGUIDES_API_KEY
# - PROXY_PREFIXES
# - EXCEPTION_DOMAINS
# - LIBAPPS_BASE_URL [with `--update` only]
# - LIBAPPS_USERNAME [with `--update` only]
# - LIBAPPS_PASSWORD [with `--update` only]
# USAGE: python asset_proxy_cleanup.py [--update]
import json
import requests
import urllib.parse
from decouple import config # pypi python-decouple
from playwright.sync_api import (
expect,
sync_playwright,
TimeoutError as PlaywrightTimeoutError,
)
types = {
1: "Rich Text / HTML",
2: "Link",
3: "RSS Feed",
4: "Document / File",
5: "Book from the Catalog",
6: "Poll",
7: "Google Search",
9: "Media / Widget",
10: "Database",
11: "Guide List",
12: "LibAnswers Widget",
13: "LibWizard Item",
14: "Remote Script",
}
def main(
update: ("update records", "flag", "u"), # type: ignore
dry_run: ("mock update without saving", "flag", "d"), # type: ignore
):
# LibGuides API Response
response = requests.get(
"https://lgapi-us.libapps.com/1.1/assets",
params={
"site_id": config("LIBGUIDES_API_SITE_ID"),
"key": config("LIBGUIDES_API_KEY"),
},
)
# Parse Response
data = json.loads(response.text)
with sync_playwright() as playwright:
try:
if update:
if dry_run:
print("\n🐞 DRY RUN: no changes will be saved")
browser = playwright.firefox.launch()
page = browser.new_page(
base_url=config("LIBAPPS_BASE_URL"),
record_video_dir="_outputs",
)
page.goto("/libapps/login.php")
page.fill("#s-libapps-email", config("LIBAPPS_USERNAME"))
page.fill("#s-libapps-password", config("LIBAPPS_PASSWORD"))
page.click("#s-libapps-login-button")
page.wait_for_load_state("networkidle")
for asset in data:
if (
types[asset["type_id"]] != "Link"
and types[asset["type_id"]] != "Book from the Catalog"
and types[asset["type_id"]] != "Database"
):
continue
# NOTE reset loop variables
toggled = ""
use_proxy = "No"
if "meta" in asset and asset["meta"]:
if asset["meta"].get("enable_proxy", ""):
use_proxy = "Yes"
proxy_prefix = contains_proxy_prefix(asset["url"])
exception_domain = contains_exception_domain(asset["url"])
if proxy_prefix:
print_asset(asset, use_proxy)
working_url = urllib.parse.unquote(
asset["url"].replace(proxy_prefix, "")
)
if not update:
print("➡️ Replace URL", working_url)
if not exception_domain and use_proxy == "No":
print("➡️ Toggle “Use Proxy?” to Yes")
elif exception_domain and use_proxy == "Yes":
print(f"[Exception: {exception_domain}]")
print("➡️ Toggle “Use Proxy?” to No")
elif update:
if types[asset["type_id"]] == "Database":
page.goto("/libguides/az.php")
else:
page.goto("/libguides/assets.php")
# NOTE reset search
page.get_by_role("textbox", name="ID").fill("")
page.keyboard.up("ArrowRight")
expect(page.get_by_role("status")).to_contain_text(
"Showing 1 to 25"
)
page.get_by_role("textbox", name="ID").fill(str(asset["id"]))
page.keyboard.up("ArrowRight")
expect(page.get_by_role("status")).to_contain_text(
"Showing 1 to 1 of 1 entries"
)
page.screenshot(
path=f'_outputs/{asset["id"]}-filtered.png'
)
page.get_by_title("Edit Item").click()
page.locator("#form-group-enable_proxy").wait_for()
page.screenshot(
path=f'_outputs/{asset["id"]}-pre-edit.png'
)
if "Link" in types[asset["type_id"]]:
page.get_by_label("Link URL").fill(working_url)
elif "Book" in types[asset["type_id"]]:
page.get_by_role("textbox", name="URL", exact=True).fill(
working_url
)
if not exception_domain and use_proxy == "No":
# NOTE clicking the label rather than the input works
page.locator("#label-enable_proxy_1").click()
toggled = "☑️ Toggled “Use Proxy?” to Yes"
elif exception_domain and use_proxy == "Yes":
# NOTE clicking the label rather than the input works
page.locator("#label-enable_proxy_0").click()
toggled = f"☑️ Toggled “Use Proxy?” to No [Exception: {exception_domain}]"
page.screenshot(
path=f'_outputs/{asset["id"]}-pre-save.png'
)
if dry_run:
page.get_by_role("button", name="Cancel").click()
else:
page.get_by_role("button", name="Save").click()
print("☑️ Replaced URL", working_url)
if toggled:
print(toggled)
elif exception_domain and use_proxy == "Yes":
print_asset(asset, use_proxy)
if not update:
print(
f"➡️ Toggle “Use Proxy?” to No [Exception: {exception_domain}]"
)
if update:
if types[asset["type_id"]] == "Database":
page.goto("/libguides/az.php")
else:
page.goto("/libguides/assets.php")
# NOTE reset search
page.get_by_role("textbox", name="ID").fill("")
page.keyboard.up("ArrowRight")
expect(page.get_by_role("status")).to_contain_text(
"Showing 1 to 25"
)
page.get_by_role("textbox", name="ID").fill(str(asset["id"]))
page.keyboard.up("ArrowRight")
expect(page.get_by_role("status")).to_contain_text(
"Showing 1 to 1 of 1 entries"
)
page.screenshot(
path=f'_outputs/{asset["id"]}-filtered.png'
)
page.get_by_title("Edit Item").click()
page.locator("#form-group-enable_proxy").wait_for()
page.screenshot(
path=f'_outputs/{asset["id"]}-pre-edit.png'
)
# NOTE clicking the label rather than the input works
page.locator("#label-enable_proxy_0").click()
page.screenshot(
path=f'_outputs/{asset["id"]}-pre-save.png'
)
if dry_run:
page.get_by_role("button", name="Cancel").click()
else:
page.get_by_role("button", name="Save").click()
print(
f"☑️ Toggled “Use Proxy?” to No [Exception: {exception_domain}]"
)
if update:
browser.close()
if dry_run:
print("\n🐞 DRY RUN: no changes were saved")
print("")
except PlaywrightTimeoutError as e:
print(e)
if update:
browser.close()
def print_asset(asset, use_proxy):
print("")
print(
asset["id"],
types[asset["type_id"]],
f"[Use Proxy? {use_proxy}]",
asset["name"],
)
print(asset["url"])
def contains_proxy_prefix(url):
for prefix in config("PROXY_PREFIXES").split(","):
if prefix in url:
return prefix
return False
def contains_exception_domain(url):
for domain in config("EXCEPTION_DOMAINS").split(","):
if domain in url:
return domain
return False
if __name__ == "__main__":
# fmt: off
import plac; plac.call(main)