Skip to content

Commit

Permalink
Youtube suffix (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
Michał Miszczyszyn committed Feb 23, 2021
1 parent b7b9b18 commit fb37d80
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
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

0 comments on commit fb37d80

Please sign in to comment.