mirror of
https://github.com/FranP-code/format_twitter_projects_accounts_tweets.git
synced 2025-10-13 00:32:19 +00:00
Added package-lock.json
This commit is contained in:
committed by
GitHub
parent
d0299ca1a1
commit
5872e1b686
51
astro-app/src/lib/csv-loader.ts
Normal file
51
astro-app/src/lib/csv-loader.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { parse } from 'csv-parse/sync';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
export interface TwitterProject {
|
||||
id: string;
|
||||
created_at: string;
|
||||
project_description: string;
|
||||
project_url: string | null;
|
||||
media_type: string | null;
|
||||
media_thumbnail: string | null;
|
||||
media_original: string | null;
|
||||
author_screen_name: string;
|
||||
author_name: string;
|
||||
favorite_count: number;
|
||||
retweet_count: number;
|
||||
reply_count: number;
|
||||
views_count: number;
|
||||
category?: string;
|
||||
}
|
||||
|
||||
export async function loadTwitterProjects(): Promise<TwitterProject[]> {
|
||||
try {
|
||||
const csvPath = path.join(process.cwd(), '..', 'unified_projects.csv');
|
||||
const csvContent = fs.readFileSync(csvPath, 'utf-8');
|
||||
|
||||
const records = parse(csvContent, {
|
||||
columns: true,
|
||||
skip_empty_lines: true,
|
||||
cast: (value, context) => {
|
||||
// Convert numeric fields
|
||||
if (['favorite_count', 'retweet_count', 'reply_count', 'views_count'].includes(context.column as string)) {
|
||||
return parseInt(value) || 0;
|
||||
}
|
||||
// Handle null/empty values
|
||||
if (value === '' || value === 'null') {
|
||||
return null;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
});
|
||||
|
||||
return records.map((record: any) => ({
|
||||
...record,
|
||||
category: record.category || 'Uncategorized'
|
||||
}));
|
||||
} catch (error) {
|
||||
console.error('Error loading CSV:', error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user