traxxx/assets/js/curate.js

123 lines
3.7 KiB
JavaScript
Raw Normal View History

import dayjs from 'dayjs';
2020-05-26 23:40:10 +00:00
function curateActor(actor, release) {
2020-05-17 23:22:56 +00:00
if (!actor) {
return null;
}
const curatedActor = {
...actor,
2020-05-17 23:22:56 +00:00
height: actor.heightMetric && {
metric: actor.heightMetric,
imperial: actor.heightImperial,
},
weight: actor.weightMetric && {
metric: actor.weightMetric,
imperial: actor.weightImperial,
},
origin: actor.birthCountry && {
city: actor.birthCity,
state: actor.birthState,
country: actor.birthCountry,
},
residence: actor.residenceCountry && {
city: actor.residenceCity,
state: actor.residenceState,
country: actor.residenceCountry,
},
2020-05-17 23:22:56 +00:00
scrapedAt: new Date(actor.createdAt),
updatedAt: new Date(actor.updatedAt),
};
2020-05-17 23:22:56 +00:00
if (actor.profiles && actor.profiles.length > 0) {
const photos = actor.profiles
.map(profile => ({ entity: profile.entity, ...profile.avatar }))
.filter(avatar => avatar.id && (!curatedActor.avatar || avatar.hash !== curatedActor.avatar.hash));
2020-05-17 23:22:56 +00:00
const descriptions = actor.profiles.reduce((acc, profile) => ({
...acc,
...(profile.description && {
[profile.descriptionHash]: {
text: profile.description,
2020-06-28 02:22:19 +00:00
entity: profile.entity,
},
}),
}), {});
2020-05-17 23:22:56 +00:00
curatedActor.photos = Object.values(photos.reduce((acc, photo) => ({ ...acc, [photo.hash]: photo }), {}));
curatedActor.descriptions = Object.values(descriptions);
2020-05-17 23:22:56 +00:00
}
if (release && release.date && curatedActor.birthdate) {
curatedActor.ageThen = dayjs(release.date).diff(actor.birthdate, 'year');
}
2020-05-26 02:11:29 +00:00
if (actor.aliasFor) {
curatedActor.aliasFor = curateActor(curatedActor.aliasFor);
}
return curatedActor;
}
function curateRelease(release) {
const curatedRelease = {
...release,
actors: [],
poster: release.poster && release.poster.media,
tags: release.tags ? release.tags.map(({ tag }) => tag) : [],
};
if (release.scenes) curatedRelease.scenes = release.scenes.map(({ scene }) => curateRelease(scene));
if (release.movies) curatedRelease.movies = release.movies.map(({ movie }) => curateRelease(movie));
if (release.photos) curatedRelease.photos = release.photos.map(({ media }) => media);
if (release.covers) curatedRelease.covers = release.covers.map(({ media }) => media);
if (release.trailer) curatedRelease.trailer = release.trailer.media;
if (release.teaser) curatedRelease.teaser = release.teaser.media;
if (release.actors) curatedRelease.actors = release.actors.map(({ actor }) => curateActor(actor, curatedRelease));
if (release.movieTags && release.movieTags.length > 0) curatedRelease.tags = release.movieTags.map(({ tag }) => tag);
if (release.movieActors && release.movieActors.length > 0) curatedRelease.actors = release.movieActors.map(({ actor }) => curateActor(actor, curatedRelease));
return curatedRelease;
}
function curateEntity(entity, parent, releases) {
const curatedEntity = {
...entity,
children: [],
};
if (entity.tags) curatedEntity.tags = entity.tags.map(({ tag }) => tag);
if (entity.children) {
if (entity.children.nodes) {
curatedEntity.children = entity.children.nodes.map(childEntity => curateEntity(childEntity, curatedEntity));
}
curatedEntity.childrenTotal = entity.children.totalCount;
}
if (entity.parent || parent) curatedEntity.parent = curateEntity(entity.parent || parent);
if (releases) curatedEntity.releases = releases.map(release => curateRelease(release));
return curatedEntity;
}
function curateTag(tag) {
const curatedTag = {
...tag,
};
if (tag.releases) curatedTag.releases = tag.releases.map(({ release }) => curateRelease(release));
if (tag.photos) curatedTag.photos = tag.photos.map(({ media }) => media);
if (tag.poster) curatedTag.poster = tag.poster.media;
return curatedTag;
}
export {
curateActor,
curateEntity,
curateRelease,
curateTag,
};