|
| 1 | +import axios from 'axios'; |
| 2 | +import * as fs from 'fs'; |
| 3 | +import * as path from 'path'; |
| 4 | + |
| 5 | +// Define the structure of the API response based on the AdminPage usage |
| 6 | +interface ApartmentData { |
| 7 | + buildingData: { |
| 8 | + id: string; |
| 9 | + name: string; |
| 10 | + address: string; |
| 11 | + area: string; |
| 12 | + numBeds: number; |
| 13 | + numBaths: number; |
| 14 | + landlordId: string; |
| 15 | + photos: string[]; |
| 16 | + urlName: string; |
| 17 | + latitude: number; |
| 18 | + longitude: number; |
| 19 | + distanceToCampus: number; |
| 20 | + }; |
| 21 | + numReviews: number; |
| 22 | + company: string; |
| 23 | + avgRating: number; |
| 24 | + avgPrice: number; |
| 25 | +} |
| 26 | + |
| 27 | +interface ApiResponse { |
| 28 | + buildingData: ApartmentData[]; |
| 29 | + isEnded: boolean; |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Script to export apartment names to a CSV file |
| 34 | + * |
| 35 | + * This script fetches all apartment data from the API endpoint used in AdminPage |
| 36 | + * and exports just the apartment names to a CSV file. |
| 37 | + */ |
| 38 | +const exportApartmentNames = async () => { |
| 39 | + try { |
| 40 | + console.log('Fetching apartment data...'); |
| 41 | + |
| 42 | + // Use the same API endpoint as AdminPage |
| 43 | + const response = await axios.get<ApiResponse>( |
| 44 | + 'http://localhost:8080/api/page-data/home/1000/numReviews' |
| 45 | + ); |
| 46 | + |
| 47 | + if (!response.data || !response.data.buildingData) { |
| 48 | + throw new Error('No apartment data received from API'); |
| 49 | + } |
| 50 | + |
| 51 | + const apartments = response.data.buildingData; |
| 52 | + console.log(`Found ${apartments.length} apartments`); |
| 53 | + |
| 54 | + // Extract apartment names |
| 55 | + const apartmentNames = apartments.map((apt) => apt.buildingData.name); |
| 56 | + |
| 57 | + // Create CSV content |
| 58 | + const csvHeader = 'Apartment Name\n'; |
| 59 | + const csvContent = apartmentNames.map((name) => `"${name}"`).join('\n'); |
| 60 | + const fullCsvContent = csvHeader + csvContent; |
| 61 | + |
| 62 | + // Write to CSV file |
| 63 | + const outputPath = path.join(__dirname, 'apartment_names.csv'); |
| 64 | + fs.writeFileSync(outputPath, fullCsvContent, 'utf8'); |
| 65 | + |
| 66 | + console.log(`Successfully exported ${apartmentNames.length} apartment names to: ${outputPath}`); |
| 67 | + console.log('First few apartment names:'); |
| 68 | + apartmentNames.slice(0, 5).forEach((name, index) => { |
| 69 | + console.log(`${index + 1}. ${name}`); |
| 70 | + }); |
| 71 | + } catch (error) { |
| 72 | + console.error('Error exporting apartment names:', error); |
| 73 | + |
| 74 | + if (axios.isAxiosError(error)) { |
| 75 | + if (error.code === 'ECONNREFUSED') { |
| 76 | + console.error( |
| 77 | + 'Could not connect to the server. Make sure the backend server is running on localhost:3000' |
| 78 | + ); |
| 79 | + } else { |
| 80 | + console.error('API request failed:', error.message); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + process.exit(1); |
| 85 | + } |
| 86 | +}; |
| 87 | + |
| 88 | +// Run the script |
| 89 | +exportApartmentNames(); |
0 commit comments