-
Notifications
You must be signed in to change notification settings - Fork 1
/
add_tag.py
82 lines (57 loc) · 2.08 KB
/
add_tag.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
import requests
import json
from math import ceil
from time import sleep
from configparser import ConfigParser
PRODUCT_TYPE_TO_TEMPLATE = {
"Photo": "regular-photo"
}
config_parser = ConfigParser()
config_parser.read('.credentials')
api_key = config_parser['aitw']['API_KEY']
password = config_parser['aitw']['PASSWORD']
store_url_prefix = config_parser['aitw']['STORE_URL_PREFIX']
url_prefix = 'https://%s:%s@%s.myshopify.com/admin/' % \
(api_key, password, store_url_prefix)
count_url = url_prefix + 'products/count.json'
session = requests.Session()
session.headers.update({'Content-Type': 'application/json'})
parameters = {
'product_type': 'Photo'
}
response = session.get(count_url, params=parameters)
if response.status_code != 200:
raise Exception('Status code: [%d] Unable to get URL: [%s]' %
(response.status_code, count_url))
sleep(0.5)
number_of_products = json.loads(response.text)['count']
num_pages = ceil(number_of_products / 250)
for i in range(num_pages):
products_url = url_prefix + 'products.json'
parameters = {
'page': i + 1,
'limit': 250,
'fields': 'id,tags',
'product_type': 'Photo',
'created_at_min': '2019-08-09T09:00:00-06:00'
}
response = session.get(products_url, params=parameters)
if response.status_code != 200:
raise Exception('Status code: [%d] Unable to get with URL: [%s]' %
(response.status_code, products_url))
sleep(0.5)
products = json.loads(response.text)['products']
for product in products:
product_id = product['id']
tags = product['tags']
payload = {
'product': {
'id': product_id,
'tags': tags + ', Web-Release-2019-08'
}
}
product_url = url_prefix + 'products/%d.json' % product_id
response = session.put(product_url, json=payload)
if response.status_code != 200:
raise Exception('Status code: [%d] Unable to put with URL: [%s]' % (response.status_code, product_url))
sleep(0.5)