Skip to content

Commit 93fbe86

Browse files
committed
Merge branch 'admin-data-edit' of https://github.com/cornell-dti/cu-apts into web_scraping
2 parents 0ad5c6f + e2839fe commit 93fbe86

19 files changed

Lines changed: 2649 additions & 147 deletions

backend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"add_reviews": "ts-node scripts/add_reviews_nodups.ts",
1010
"export_apartments": "env-cmd -f ../.env.prod ts-node scripts/export_apartments.ts",
1111
"update_apartments": "env-cmd -f ../.env.prod ts-node scripts/update_apartments_from_csv.ts",
12+
"export_apartment_names": "ts-node scripts/export_apartment_names.ts",
1213
"build": "tsc",
1314
"tsc": "tsc",
1415
"start": "node dist/backend/src/server.js",

backend/scripts/add_buildings.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,11 @@ const formatBuilding = ({
4343
name,
4444
address,
4545
landlordId: landlordId.toString(),
46-
numBaths: 0,
47-
numBeds: 0,
46+
roomTypes: [], // Initialize with empty room types
4847
photos: [],
4948
area: getAreaType(area),
5049
latitude,
5150
longitude,
52-
price: 0,
5351
distanceToCampus: 0,
5452
});
5553

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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

Comments
 (0)