Skip to content

Commit

Permalink
modified: .gitignore
Browse files Browse the repository at this point in the history
	modified:   crime/index.html
  • Loading branch information
maxiwee69 committed Jan 27, 2024
1 parent 36d189a commit a56e645
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 2 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
/push.sh
.git/
.DS_Store
*.py
*.py
crimes\server.js
crimes\scripts.js
crimes/*.js
crimes/server.js
10 changes: 10 additions & 0 deletions crime/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>
1 change: 0 additions & 1 deletion crime/scripts.js
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
sudo -u postgres psql -c "SELECT version();"
79 changes: 79 additions & 0 deletions crime/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const express = require('express');
const { Pool } = require('pg');
const axios = require('axios');
const cheerio = require('cheerio');
const Sentiment = require('sentiment');
const path = require('path');

const app = express();
const port = 3000;
const sentiment = new Sentiment();

const pool = new Pool({
user: 'maxiwee',
host: 'localhost',
database: 'mcrimes',
password: 'Leonie<3!',
port: 5432, // PostgreSQL default port is 5432
});

app.use(express.static(path.join(__dirname)));

app.get('/recentCrimes', async (req, res) => {
const { rows } = await pool.query('SELECT * FROM crimes ORDER BY date DESC LIMIT 10');
res.json(rows);
});

app.get('/weirdestCrimes', async (req, res) => {
const { rows } = await pool.query('SELECT * FROM crimes WHERE weirdness > 10 ORDER BY date DESC LIMIT 10');
res.json(rows);
});

app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});

app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});

function calculateWeirdness(description) {
const { score } = sentiment.analyze(description);

// Normalize the score to a scale of 0-10
const weirdness = Math.max(0, Math.min(10, Math.round((score + 5) * 10 / 11)));

return weirdness;
}

const urls = [
'https://www.cbsnews.com/',
'https://www.statista.com/',
'https://www.standard.co.uk/news/crime',
'https://allthatsinteresting.com/tag/crime',
'https://apnews.com/hub/crime'
];

async function scrapeCrimes() {
for (const url of urls) {
const { data } = await axios.get(url);
const $ = cheerio.load(data);

// Use the Cheerio selectors to find the data you want on the page
// This is just a placeholder and you would need to replace it with your own logic
$('div.crime').each((i, elem) => {
const crime = {
date: $(elem).find('span.date').text(),
description: $(elem).find('p.description').text(),
};

crime.weirdness = calculateWeirdness(crime.description);

// Insert the crime data into the database
pool.query('INSERT INTO crimes (date, description, weirdness) VALUES ($1, $2, $3)', [crime.date, crime.description, crime.weirdness]);
});
}
}

// Run the scraper every hour
setInterval(scrapeCrimes, 60 * 60 * 1000);

0 comments on commit a56e645

Please sign in to comment.