-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.py
138 lines (106 loc) · 4.18 KB
/
init.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
from requests import request, exceptions
from dotenv import dotenv_values
from argparse import ArgumentParser
from json import load
from time import sleep
config = dotenv_values(".env")
BUTTERCMS_BASE_URL = "https://api.buttercms.com/v2"
parser = ArgumentParser(
prog="ButterCMS Migration",
description="A utility script for migrating your content into ButterCMS"
)
parser.add_argument(
'-u',
'--update',
help="Update either existing pages or collections on ButterCMS",
choices=['pages', 'collection']
)
parser.add_argument(
'-s',
'--status',
default="draft",
help="Set status of the content being created on ButterCMS",
)
parser.add_argument(
'-v',
'--verbose',
default=False,
action='store_true',
help="Instruct script to print output at every step along the way",
)
parser.add_argument(
'-f',
'--file',
help="Specify the path to the data source file containing the migration data",
)
args = parser.parse_args()
class DataMigrator:
def __init__(self, file_path):
if args.verbose:
print('Reading data from {0} input source'.format(file_path))
file = open(file_path)
data = load(file)
if 'pages' not in data or 'collection' not in data:
raise Exception("Input data from {0} does not contain pages or collection keys".format(file_path))
self.content = data
self.page_slugs = {"product_collections": [], "customer_reviews": []}
@staticmethod
def api_request(route, data, method="POST"):
try:
# print("ENDPOINT =>", "{0}/{1}".format(BUTTERCMS_BASE_URL, route))
req = request(
url="{0}/{1}".format(BUTTERCMS_BASE_URL, route),
json=data,
method=method,
headers={"Authorization": "Token {0}".format(config['BUTTERCMS_WRITE_API_KEY'])}
)
return req
except exceptions.HTTPError as error:
raise Exception("Connection Error: {0}".format(error))
def create_pages(self):
for index, page in enumerate(self.content['pages']):
page['status'] = args.status
req = self.api_request('pages', page)
if req.status_code in [200, 202]:
self.page_slugs[page['type']].append(page['slug'])
if args.verbose:
print("Created page {0} with slug: {1}".format(index, page['slug']))
if args.verbose:
print("{0} items processed".format(len(self.content['pages'])))
def create_collection(self):
self.create_pages()
sleep(20)
for item in self.content['collection']['fields'][0].values():
item['product_collections'] = self.page_slugs['product_collections']
item['customer_reviews'] = self.page_slugs['customer_reviews']
self.content['collection']['status'] = args.status
req = self.api_request('content/', self.content['collection'])
# print(req.json())
if req.status_code in [200, 202]:
if args.verbose:
print("Created collection with key: {0}".format(self.content['collection']['key']))
def update_content(self):
if args.update == "collection":
collection = self.content['collection']
req = self.api_request('content/{0}/'.format(collection['key']), collection, "PATCH")
if req.status_code in [200, 202]:
if args.verbose:
print("Updated collection with key: {0}".format(self.content['collection']['key']))
return
if args.update == "pages":
for index, page in enumerate(self.content['pages']):
page['status'] = args.status
if args.verbose:
print("Updating content for slug: {0}".format(page['slug']))
req = self.api_request(
'pages/*/{0}/'.format(page["slug"]),
page,
method="PATCH"
)
if req.status_code in [200, 202]:
print('Data updated')
Migrator = DataMigrator(args.file)
if args.update:
Migrator.update_content()
else:
Migrator.create_collection()