-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathezproxy_links_guide_owners.py
52 lines (40 loc) · 1.51 KB
/
ezproxy_links_guide_owners.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
import csv
import json
import requests
from decouple import config
payload = {
"site_id": config("LIBGUIDES_API_SITE_ID"),
"key": config("LIBGUIDES_API_KEY"),
"expand": "owner",
}
response = requests.get("https://lgapi-us.libapps.com/1.1/guides", params=payload)
data = json.loads(response.text)
def search_nested_dicts(nested_dicts, search_strings):
results = []
for d in nested_dicts:
for k, v in d.items():
if any(s in str(v) for s in search_strings):
results.append(d)
if isinstance(v, dict):
results.extend(search_nested_dicts([v], search_strings))
return results
# results = search_nested_dicts(data, assets)
with open("in.csv") as csv_in:
# NOTE `in.csv` is created from copying the Outer HTML from the
# Search Results table in LibGuides > Tools > Search & Replace
reader = csv.DictReader(csv_in)
with open("out.csv", "a", newline="") as csv_out:
writer = csv.writer(csv_out)
writer.writerow(["url", "name", "owner_name", "owner_email"])
for row in reader:
print(row["GuideID"])
guide_data = search_nested_dicts(data, [row["GuideID"]])
writer.writerow(
[
row["URL"],
row["Guide Mappings"],
f'{guide_data[0]["owner"]["first_name"]} {guide_data[0]["owner"]["last_name"]}',
guide_data[0]["owner"]["email"],
]
)
print("🙃 join complete!")