Skip to content

Commit

Permalink
Changed all kinds of things
Browse files Browse the repository at this point in the history
  • Loading branch information
james-tyner committed Feb 25, 2019
1 parent 683b7a1 commit f8d43ee
Show file tree
Hide file tree
Showing 39 changed files with 5,121 additions and 1,926 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -2,3 +2,6 @@ tester.py
.DS_STORE .DS_STORE
1031552_MasterZipCodes* 1031552_MasterZipCodes*
factfinder-data factfinder-data
node_modules
permit_data.db
.env
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -5,11 +5,11 @@
### Next steps ### Next steps
- [x] Connect to Socrata API - [x] Connect to Socrata API
- [x] Calculate residential construction totals by ZIP code and year - [x] Calculate residential construction totals by ZIP code and year
- [ ] Pair ZIP code data with other statistics (demographics, economics, etc.) - [x] Pair ZIP code data with other statistics (demographics, economics, etc.)
- [ ] Get expert opinions - [ ] Get expert opinions
- [ ] Create website to view data for a given ZIP code in the city - [ ] Create website to view data for a given ZIP code in the city
- [ ] Connect to SendGrid API - [x] Connect to SendGrid API
- [ ] Format and send newsletters - [x] Format and send newsletters


## Project goals ## Project goals
* Illustrate connection between demographic data/socioeconomic factors and the construction of housing in LA amid the city's housing crisis * Illustrate connection between demographic data/socioeconomic factors and the construction of housing in LA amid the city's housing crisis
Expand Down
171 changes: 171 additions & 0 deletions app.js
@@ -0,0 +1,171 @@
const sqlite3 = require('sqlite3').verbose();
const axios = require('axios');

let express = require("express");
let app = express();
var bodyParser = require("body-parser");
app.use(bodyParser.urlencoded());

require('dotenv').load();

app.use(express.static("public"));


// Returns array of ZIP objects containing neighborhood descriptions
app.get('/api/neighborhoods', function(request, response){
let db = new sqlite3.Database('permit_data.db', sqlite3.OPEN_READONLY, (err) => {
if (err) {
return console.error(err.message);
}
});

let sql = `SELECT ZIP, Description FROM attributes ORDER BY ZIP`;

db.all(sql,[],(err, rows ) => {
if (err) {
throw err;
}
zipHolder = [];
rows.forEach((row) => {
zipHolder.push({
"zip-code":row.ZIP,
"description":row.Description
});
});

response.json(zipHolder);
});

db.close((err) => {
if (err) {
return console.error(err.message);
}
});
});


// Returns array of information about a ZIP using the param in the request
app.get('/api/neighborhoods/:zip', function(request, response){
let db = new sqlite3.Database('permit_data.db', sqlite3.OPEN_READONLY, (err) => {
if (err) {
return console.error(err.message);
}
});

let sql = `SELECT * FROM attributes, demographics, costs WHERE attributes.ZIP = ${request.params.zip} AND demographics.ZIP = ${request.params.zip} AND costs.ZIP = ${request.params.zip}`;

db.get(sql,[],(err, row) => {
if (err) {
throw err;
}
zipHolder = [];
zipHolder.push({
"zip-code":row.ZIP,
"description":row.Description,
"units":{
"units-all":row.UnitsAll,
"units-2019":row.Units2019,
"units-2018":row.Units2018,
"units-2017":row.Units2017,
"units-2016":row.Units2016,
"units-2015":row.Units2015,
"units-2014":row.Units2014,
"units-2013":row.Units2013
},
"demographics":{
"population":row.Population,
"white":row.White,
"white-non-hispanic":row.White_Non_Hispanic,
"black":row.Black,
"native-american":row.Native_American,
"asian":row.Asian,
"hawaiian-pacific":row.Hawaiian_Pacific,
"other":row.Other,
"two_or_more":row.Two_Or_More,
"two_or_more_other":row.Two_Or_More_Other,
"two_or_more_not_other":row.Two_Or_More_Not_Other,
"percent-white":row.Percent_White_Non_Hispanic,
"percent-minority":row.Percent_Minority
},
"costs":{
"median-income":row.MedIncome,
"median-housing-cost":row.MedHousingCost,
"percentage":row.Percentage
}
});

response.json(zipHolder);
});

db.close((err) => {
if (err) {
return console.error(err.message);
}
});
});


// Return all projects for one ZIP code
// Here you set it to return 50 at a time. The offset parameter will tell the Socrata API which 50 to pull. In your page, use ?page=NUMBER to return the proper page, or leave it blank to get the most recent 50
app.get('/api/projects/:zip', function(req, res){
axios.get('/resource/75vw-v4fk.json', {
baseURL: 'https://data.lacity.org',
headers: {"X-App-Token": process.env.SOCRATA_API_KEY},
params: {
$limit:50,
$offset:(req.query.page || 0),
$order:"issue_date DESC",
$where:`permit_type = 'Bldg-New' AND permit_sub_type not in ('Commercial') AND zip_code = ${req.params.zip}`
}
})
.then(function (response) {
res.send(response.data);
})
.catch(function (error) {
console.log(error);
});
});


app.post('/email/subscribe', function(request, response){
console.log(request.body.email);
console.log(request.body.zip);

let db = new sqlite3.Database('email.db', (err) => {
if (err) {
return console.error(err.message);
} else {
console.log("Connected to database successfully.")
}
});

// Add to both email address table and table of emails and ZIP codes
db.serialize(() => {
db.run(`INSERT OR IGNORE INTO emails(Email_Address) VALUES('${request.body.email}')`, [], function(error){
if(error){
return console.log(error.message);
} else {
console.log("emails table worked")
}
});

db.run(`INSERT OR REPLACE INTO email_zip_match(email, ZIP) VALUES('${request.body.email}', ${request.body.zip})`, [], function(error){
if(error){
return console.log(error.message);
} else {
console.log("email_zip_match table worked")
}
});
});

db.close((err) => {
if (err) {
return console.error(err.message);
}
});

response.redirect("../subscribe-success.html");
});


app.listen(8000);

0 comments on commit f8d43ee

Please sign in to comment.