-
Notifications
You must be signed in to change notification settings - Fork 0
/
libguides_api_assets_id_proxy.py
45 lines (36 loc) · 1.19 KB
/
libguides_api_assets_id_proxy.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
# file: libguides_api_assets_id_proxy.py
# USAGE: python libguides_api_assets_id_proxy.py
# This script will update the save the IDs and proxy status of assets in a CSV
# file named `_outputs/assets_id_proxy.csv`. The file will have the columns:
# `id`, and `enable_proxy`.
import csv
import json
import requests
from decouple import config # pypi python-decouple
# 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"),
},
)
# print(json.dumps(response, indent=4, sort_keys=True))
# Parse response
data = json.loads(response.text)
# Extract necessary data
rows = []
for item in data:
id = item["id"]
if "meta" in item and item["meta"]:
enable_proxy = item["meta"].get("enable_proxy", "")
else:
enable_proxy = ""
rows.append({"id": id, "enable_proxy": enable_proxy})
# Save data to CSV file
with open("_outputs/assets_id_proxy.csv", "w", newline="") as csvfile:
fieldnames = ["id", "enable_proxy"]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for row in rows:
writer.writerow(row)