Skip to content
Merged
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
52 changes: 37 additions & 15 deletions src/server/shows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { left_pad } from '$utilities/left_pad';
import { error } from '@sveltejs/kit';
import matter from 'gray-matter';
import slug from 'speakingurl';
import * as Sentry from '@sentry/sveltekit';
import { prisma_client as prisma } from '$/server/prisma-client';
import { cache } from './cache/cache';

Expand All @@ -23,14 +24,19 @@ export async function import_or_update_all_shows() {

// Read and process each .md file
for (const md_file_path in md_files) {
const { number, hash, md_file_contents } = await get_show_data_from_glob(
md_files[md_file_path],
md_file_path
);
await parse_and_save_show_notes(md_file_contents, hash, number, md_file_path);
try {
const { number, hash, md_file_contents } = await get_show_data_from_glob(
md_files[md_file_path],
md_file_path
);
await parse_and_save_show_notes(md_file_contents, hash, number, md_file_path);
} catch (err) {
throw new Error(`Failed to import ${md_file_path}`, { cause: err });
}
}
} catch (err) {
console.error('❌ Pod Sync Error:', err);
Sentry.captureException(err);
error(500, 'Error Importing Shows');
}
console.log('🤖 Pod Sync Complete ✅');
Expand All @@ -46,10 +52,17 @@ export async function import_or_update_all_changed_shows() {

// Read and process each .md file
for (const md_file_path in md_files) {
const { number, hash, md_file_contents } = await get_show_data_from_glob(
md_files[md_file_path],
md_file_path
);
let number: number;
let hash: string;
let md_file_contents: string;
try {
({ number, hash, md_file_contents } = await get_show_data_from_glob(
md_files[md_file_path],
md_file_path
));
} catch (err) {
throw new Error(`Failed to import ${md_file_path}`, { cause: err });
}

const existing_show = await prisma.show.findUnique({
where: { number: number }
Expand All @@ -71,6 +84,7 @@ export async function import_or_update_all_changed_shows() {
}
} catch (err) {
console.error('❌ Pod Sync Error:', err);
Sentry.captureException(err);
error(500, 'Error Importing Shows');
}
cache.shows.drop_shows_list_cache();
Expand All @@ -80,8 +94,11 @@ export async function import_or_update_all_changed_shows() {

async function get_show_data_from_glob(md_file_contents: string, md_file_path: string) {
const hash = await get_hash_from_content(md_file_contents);
const cleaned_path = md_file_path.replace('/shows/', '');
const number = parseInt(cleaned_path.split(' - ')[0]);
const filename = md_file_path.split('/').pop() ?? '';
const number = parseInt(filename, 10);
if (Number.isNaN(number)) {
throw new Error(`Could not determine show number from ${md_file_path}`);
}
return { number, hash, md_file_contents };
}

Expand All @@ -93,7 +110,9 @@ export async function parse_and_save_show_notes(
md_file: string
) {
// Parse the front matter
const { data, content } = matter(notes);
// YAML treats an unquoted @ as invalid, even though it is a common handle format.
const parseable_notes = notes.replace(/^(\s*twitter:\s*)@([^\s#]+)(\s*(?:#.*)?)$/gm, '$1$2$3');
const { data, content } = matter(parseable_notes);

const date = new Date(data.date); // Parse the date string into a Date object

Expand All @@ -104,13 +123,13 @@ export async function parse_and_save_show_notes(
DAYS_OF_WEEK_TYPES[dayOfWeek] || 'SPECIAL';
// Save or update the show
try {
// Prepare the hosts connection if hosts exist in the frontmatter
const hosts = data.hosts;
let hostsConnection = {};
if (data.hosts && Array.isArray(data.hosts)) {
if (hosts && Array.isArray(hosts)) {
const hostUsers = await prisma.user.findMany({
where: {
username: {
in: data.hosts
in: hosts
}
}
});
Expand Down Expand Up @@ -180,6 +199,9 @@ export async function parse_and_save_show_notes(
async function add_or_update_guest(guest: FrontMatterGuest, show_id: string) {
try {
const { social, name, ...guest_without_socials } = guest;
if (typeof guest_without_socials.twitter === 'string') {
guest_without_socials.twitter = guest_without_socials.twitter.replace(/^@/, '');
}
const name_slug = slug(name);

const guest_record = await prisma.$transaction(async (prisma) => {
Expand Down
Loading