-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSheets.py
247 lines (209 loc) · 8.78 KB
/
Sheets.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
238
239
240
241
242
243
244
245
246
247
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 12 00:00:00 2021
@author: Arthur Thouvenin
contact: athouvenin [at] outlook.com
"""
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import os
import sys
import pandas as pd
import json
from datetime import datetime
scope = ["https://spreadsheets.google.com/feeds",
"https://www.googleapis.com/auth/spreadsheets",
"https://www.googleapis.com/auth/drive.file",
"https://www.googleapis.com/auth/drive"]
creds = ServiceAccountCredentials.from_json_keyfile_name("creds.json", scope)
client = gspread.authorize(creds)
spreadsheet = client.open("Articles")
spreadsheetPublic = client.open("Vinted_Data")
sheet_Articles = spreadsheet.worksheet("Articles")
sheet_Chiffres = spreadsheet.worksheet("Chiffres")
def updateVintedSheets(reload_data=False):
"""
This function will update the Google spreadsheets containing all of the data found within Vinted.fr temporarly stored in ./DATA/
Parameters
----------
reload_data : BOOLEAN, optional
This parameter indicate if the alogrithm need to collect Vinted data before updating the sheets.
Returns
-------
None.
"""
def unPack_Catalogs(json_f):
"""
This function will return a flatten JSON normalized as a pandas array from the JSON object within the json file (json_f). Thus an embbed JSON will be flatten to a single level layer.
Catalogs within Vinted are embedded, for example there is a Catalog Accessories corresponding to Men and one for Women and within those Accessories catalogs are mutliple sub-catalogs such as Jewels, etc.
This function will bring all sub-catalogs and parents catalogs to the same level. Some fields containing a list of ids will also be removed or url to photos.
Parameters
----------
json_f : FILE OBJECT
The JSON file in which the Catalogs data are stored.
Returns
-------
DATA : A pandas DataFrame.
"""
def recursive_Catalog(json_obj):
"""
This function wil flaten the JSON object provided as parameter, some fields will be deleted based on / field_lists.
Catalogs within Vinted are embedded, for example there is a Catalog Accessories corresponding to Men and one for Women and within those Accessories catalogs are mutliple sub-catalogs such as Jewels, etc.
This function will bring all sub-catalogs and parents catalogs to the same level.
Parameters
----------
json_obj : JSON OBJECT
The JSON object corresponding to the Catalogs found within Vinted.
Returns
-------
DATA : LIST
A list of dictionaries,corresponding of all catalogs brought to the same level.
"""
field_lists = ["photo","material_group_ids","size_group_ids","package_size_ids"]
DATA = []
for catalog in json_obj:
for field in field_lists:
if field in catalog:
del catalog[field]
if len(catalog["catalogs"])!=0:
unpack = recursive_Catalog(catalog["catalogs"])
DATA = DATA + unpack
del catalog["catalogs"]
DATA.append(catalog)
return DATA
json_f = json.load(json_f)
DATA = pd.json_normalize(recursive_Catalog(json_f))
return DATA
def unPack_Materials(json_f):
"""
This function will return a flatten JSON normalized as a pandas array from the JSON object within the json file (json_f). Thus an embbed JSON will be flatten to a single level layer.
Materials within Vinted are embedded, this function will bring all sub-materials and parents materials to the same level.
Parameters
----------
json_f : FILE OBJECT
The JSON object corresponding to the Catalogs found within Vinted.
Returns
-------
DATA : A pandas DataFrame, a list of dictionaries corresponding to all materials found within Vinted.
"""
json_f = json.load(json_f)
DATA = []
for material in json_f:
title = material["title"]
for mat in material["materials"]:
mat["parent"] = title
DATA.append(mat)
del material["materials"]
material["parent"] = title
DATA.append(material)
DATA = pd.json_normalize(DATA)
return DATA
def unPack_Sizes(json_f):
"""
This function will return a flatten JSON normalized as a pandas array from the JSON object within the json file (json_f). Thus an embbed JSON will be flatten to a single level layer.
Sizes within Vinted are embedded, this function will bring all sub-materials and parents materials to the same level. Some fields containing a list of ids will also be removed.
Parameters
----------
json_f : FILE OBJECT
The JSON object corresponding to the Sizes found within Vinted.
Returns
-------
DATA : A pandas DataFrame, a list of dictionaries corresponding to all sizes found within Vinted.
"""
json_f = json.load(json_f)
field_lists = [
"size_ids"]
DATA = []
for size in json_f:
for field in field_lists:
if field in size:
del size[field]
parent_title = size["description"]
parent_id = size["id"]
if len(size["sizes"]) != 0:
for s in size["sizes"]:
s["parent_id"] = parent_id
s["parent_title"] = parent_title
DATA.append(s)
del size["sizes"]
DATA.append(size)
DATA = pd.json_normalize(DATA)
return DATA
# def unPack_Countries(json_f):
# """
# This function will return a flatten JSON normalized as a pandas array from the JSON object within the json file (json_f). Thus an embbed JSON will be flatten to a single level layer.
# Materials within Vinted are embedded, this function will bring all sub-materials and parents materials to the same level.
# Parameters
# ----------
# json_f : FILE OBJECT
# The JSON object corresponding to the Catalogs found within Vinted.
# Returns
# -------
# DATA : A pandas DataFrame, a list of dictionaries corresponding to all materials found within Vinted.
# """
# json_f = json.load(json_f)
# field_lists = ["postal_code_constraints"]
# for item in json_f:
# for field in field_lists:
# if field in item:
# del item[field]
# return pd.json_normalize(json_f)
def readJSON(json_f):
"""
This function will return a pandas dataframe from the JSON File provided (json_f).
Parameters
----------
json_f : FILE OBJECT
The JSON object corresponding to the Catalogs found within Vinted.
Returns
-------
pd.read_json(json_f) : A pandas DataFrame.
"""
return pd.read_json(json_f)
id_files = {
"Brand":{
'function':readJSON
},
"Catalog":{
'function':unPack_Catalogs
},
"Color":{
'function':readJSON
},
# "Country":{
# 'function':unPack_Countries
# },
"Material":{
'function':unPack_Materials
},
"Size":{
'function':unPack_Sizes
},
"Status":{
'function':readJSON
}
}
folder = "./DATA/"
if "JSONfromID" not in sys.modules:
from collect_data import JSONfromID
if reload_data:
JSONfromID(id_range=range(0,15000),per_page=110,save=True)
for file in os.listdir(folder):
file_name = file.split(".")[0].capitalize()
print(file_name)
with open(folder+file,"r") as json_file:
if file_name in id_files:
data = id_files[file_name]['function'](json_file)
else:
continue
data.fillna('', inplace=True)
sheet = spreadsheet.worksheet(file_name)
sheet.update([data.columns.values.tolist()] + data.values.tolist())
public_sheet = spreadsheetPublic.worksheet(file_name)
public_sheet.update([data.columns.values.tolist()] + data.values.tolist())
now = datetime.now()
public_sheet = spreadsheetPublic.worksheet("Vinted_Summary")
public_sheet.update('E12',str(now.strftime("%d-%m-%Y %H:%M")))
return
if __name__ == '__main__':
updateVintedSheets(reload_data=True)