Skip to content

Instantly share code, notes, and snippets.

@quanengineering
Last active December 18, 2022 17:46
Show Gist options
  • Save quanengineering/a7bb073bf4fdd0b3e3fd3f28ba6053ac to your computer and use it in GitHub Desktop.
Save quanengineering/a7bb073bf4fdd0b3e3fd3f28ba6053ac to your computer and use it in GitHub Desktop.
Generate outfits from twelve70.com main closet then add to favourite

Scripts to automate twelve70.com

How to run the script: You should copy and paste it into the browser console.

Generate outfits from main closet then add to favourite

You should close the site when there are no outfits generated anymore.

image

Count outfits of each closet item

Thanks to this script, you should notice which clothes you could use only for a few outfits.

image

Delete all outfits from favourite list

async function get_headers() {
return {
Authorization: 'Bearer ' + localStorage.getItem('token'),
'Content-Type': 'application/json',
};
}
async function get_closets_items() {
let response = await fetch('https://api.twelve70.com/api/users/getUserData', {
method: 'GET',
headers: await get_headers(),
});
let data = await response.json();
let closets = data.closets;
return Object.keys(closets).reduce(function (r, k) {
return r.concat(closets[k]);
}, []);
}
async function get_outfits() {
let response = await fetch('https://api.twelve70.com/api/getfavourites', {
method: 'GET',
headers: await get_headers(),
});
let data = await response.json();
let outfits = data.favouriteList;
outfits.forEach(function (o) {
delete o.props;
});
return outfits.map((outfit) => {
return Object.keys(outfit).reduce(function (r, k) {
outfit[k]['category'] = k;
return r.concat(outfit[k]);
}, []);
});
}
function get_categories(closets_items) {
return [
...new Set(
closets_items.map((item) => {
return item['category'];
})
),
];
}
function get_categories_items(closets_items, category) {
return closets_items.filter((item) => {
return item['category'] === category;
});
}
function n_choose_k_combinations(arr, k, prefix = []) {
if (k == 0) return [prefix];
return arr.flatMap((v, i) =>
n_choose_k_combinations(arr.slice(i + 1), k - 1, [...prefix, v])
);
}
function cartesian(...a) {
return a.reduce((a, b) => a.flatMap((d) => b.map((e) => [d, e].flat())));
}
function count_outfits_of_closets_items_combination(outfits, item_comb) {
let no_matched_outfits = 0;
outfits.forEach((outfit) => {
let item_1_comb = item_comb[0];
let item_2_comb = item_comb[0];
let is_match_item_1_comb = false;
let is_match_item_2_comb = false;
outfit.forEach((outfit_item) => {
if (
outfit_item.category === item_1_comb.category &&
outfit_item.garmentName === item_1_comb.garmentName &&
outfit_item.colorName === item_1_comb.colorName &&
outfit_item.hexCode === item_1_comb.hexCode &&
outfit_item.inCloset == true
) {
is_match_item_1_comb = true;
}
if (
outfit_item.category === item_2_comb.category &&
outfit_item.garmentName === item_2_comb.garmentName &&
outfit_item.colorName === item_2_comb.colorName &&
outfit_item.hexCode === item_2_comb.hexCode &&
outfit_item.inCloset == true
) {
is_match_item_2_comb = true;
}
});
if (is_match_item_1_comb && is_match_item_2_comb) {
no_matched_outfits++;
}
});
return no_matched_outfits;
}
async function count_closet_item_combination() {
let closets_items = await get_closets_items();
let outfits = await get_outfits();
let closets_categories = get_categories(closets_items);
let categories_combinations = n_choose_k_combinations(closets_categories, 2);
categories_combinations.forEach((comb) => {
let cat_1 = comb[0];
let cat_2 = comb[1];
console.log(`CHECKING ${cat_1} and ${cat_2}`);
let cat_1_items = get_categories_items(closets_items, cat_1);
let cat_2_items = get_categories_items(closets_items, cat_2);
let closets_items_combination = cartesian(cat_1_items, cat_2_items);
closets_items_combination.forEach((item_comb) => {
console.log(
`${item_comb[0]['colorDisplay']} ${
item_comb[0]['garmentDisplay']
} GOES WITH ${item_comb[1]['colorDisplay']} ${
item_comb[1]['garmentDisplay']
} in ${count_outfits_of_closets_items_combination(
outfits,
item_comb
)} outfits`
);
});
});
}
count_closet_item_combination();
async function getHeaders() {
return {
Authorization: 'Bearer ' + localStorage.getItem('token'),
'Content-Type': 'application/json',
};
}
async function getClosets() {
let response = await fetch('https://api.twelve70.com/api/users/getUserData', {
method: 'GET',
headers: await getHeaders(),
});
let data = await response.json();
let closets = data.closets
return Object.keys(closets).reduce(function (r, k) {
return r.concat(closets[k]);
}, []);
}
async function getOutfits() {
let response = await fetch('https://api.twelve70.com/api/getfavourites', {
method: 'GET',
headers: await getHeaders(),
});
let data = await response.json();
let outfits = data.favouriteList;
outfits.forEach(function (o) {
delete o.props;
});
return outfits
.map((outfit) => {
return Object.keys(outfit).reduce(function (r, k) {
outfit[k]['category'] = k;
return r.concat(outfit[k]);
}, []);
})
.flat();
}
async function count_outfits_of_each_closet_item() {
let closets = await getClosets();
let outfits = await getOutfits();
closets.forEach((item) => {
let no_of_outfit = 0;
outfits.forEach((outfit) => {
if (
outfit.category === item.category &&
outfit.garmentName === item.garmentName &&
outfit.colorName === item.colorName &&
outfit.hexCode === item.hexCode
) {
no_of_outfit++;
}
});
item['no_of_outfit'] = no_of_outfit;
});
let sortedCloset = closets.sort((a, b) =>
b.no_of_outfit > a.no_of_outfit ? -1 : 0
);
console.log(sortedCloset);
}
count_outfits_of_each_closet_item();
function generate_outfits_from_main_closet_then_add_to_favourite() {
var headers = {
Authorization: 'Bearer ' + localStorage.getItem('token'),
'Content-Type': 'application/json',
};
fetch(
'https://api.twelve70.com/api/getOutfits?justGenerate=true&closetOnly=true&formality=all&closet=Main',
{
method: 'GET',
headers,
}
)
.then(function (response) {
return response.json();
})
.then(function (body) {
var outfits = body.comboList;
outfits.forEach((outfit) => {
if (!outfit.props.isFavourite) {
fetch('https://api.twelve70.com/api/users/favourites', {
method: 'POST',
headers,
body: JSON.stringify({ data: outfit.props.outfitId }),
}).then(function () {
console.log(outfit);
});
}
});
generate_outfits_from_main_closet_then_add_to_favourite();
});
}
generate_outfits_from_main_closet_then_add_to_favourite();
function remove_all_outfits_from_favourite_list() {
var headers = {
Authorization: 'Bearer ' + localStorage.getItem('token'),
'Content-Type': 'application/json',
};
fetch('https://api.twelve70.com/api/getfavourites', {
method: 'GET',
headers,
})
.then(function (response) {
return response.json();
})
.then(function (body) {
body.favouriteList.forEach((outfit) => {
fetch(
'https://api.twelve70.com/api/users/favourites/' + outfit.props.id,
{
method: 'DELETE',
headers,
}
).then(function () {
console.log(outfit);
});
});
});
}
remove_all_outfits_from_favourite_list();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment