Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix): Dodanie sufiksu Youtube do nazw i tytułów blogów #142

Merged
merged 1 commit into from
Feb 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions api-helpers/contentCreatorFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,30 @@ type Feed = {
};

export const addContentCreator = async (url: string, email: string, prisma: PrismaClient) => {
const blogData = await getBlogData(url);
const { data, type } = await getBlogData(url);
const name = type === 'youtube' ? `${data.name} YouTube` : data.name;
const slug = Slugify(name, { lower: true });

return prisma.blog.create({
data: {
...blogData,
...data,
name,
slug,
lastUpdateDate: NEVER,
slug: Slugify(blogData.name, { lower: true }),
creatorEmail: email,
},
});
};

const getBlogData = (url: string): Promise<BlogData> => {
type BlogType = 'youtube' | 'other';
const getBlogData = async (
url: string,
): Promise<{ readonly data: BlogData; readonly type: BlogType }> => {
const youtubeRss = getYouTubeRss(url);
if (youtubeRss) {
return getBlogDataForYouTubeRss(url, youtubeRss);
return { data: await getBlogDataForYouTubeRss(url, youtubeRss), type: 'youtube' };
}
return getBlogDataForUrl(url);
return { data: await getBlogDataForUrl(url), type: 'other' };
};

export const getYouTubeRss = (url: string) => {
Expand Down
9 changes: 6 additions & 3 deletions api-helpers/feedFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ const getUpdatedInfoFor = (blog: Blog) => {
return EMPTY;
}),
mergeMap((res) => from(res.text())),
map(getBlogInfoFromRss),
map((text) => getBlogInfoFromRss(text, blog)),
map((updatedInfo) => {
logger.debug(`Got updated info for blog: ${updatedInfo.name || blog.name}`);
return {
Expand All @@ -177,11 +177,14 @@ const getUpdatedInfoFor = (blog: Blog) => {
);
};

const getBlogInfoFromRss = (text: string) => {
const getBlogInfoFromRss = (text: string, blog: Blog) => {
const $ = Cheerio.load(text, { xmlMode: true, decodeEntities: true });
const type = blog.rss.includes('youtube.com') ? 'youtube' : 'other';

const name = getBlogName($) || undefined;
const blogName = getBlogName($) || undefined;
const favicon = getFavicon($) || undefined;

const name = blogName ? (type === 'youtube' ? `${blogName} YouTube` : blogName) : undefined;
const slug = name ? Slugify(name, { lower: true }) : undefined;

return {
Expand Down