Skip to content

Commit

Permalink
Add NWS discussion
Browse files Browse the repository at this point in the history
  • Loading branch information
pbaer committed Dec 3, 2023
1 parent c4667eb commit c99f51b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 30 deletions.
19 changes: 19 additions & 0 deletions api/src/wx.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { getCachedData, putCachedData } from "./cache.mjs";

export const wxDiscussion = async () => {
let wxdisc = getCachedData('wxdisc');
if (!wxdisc) {
const response = await fetch('https://a.atmos.washington.edu/data/disc_report.html');
const text = await response.text();
const start = 'Area Forecast Discussion\nNational Weather Service Seattle WA';
const startIndex = text.indexOf(start);
const end = '$$';
const endIndex = text.indexOf(end, startIndex);
wxdisc = {
text: text.substring(startIndex, endIndex)
};
putCachedData('wxdisc', wxdisc);
}
let text = wxdisc.text.replace(/(?<!\n)\n(?!\n)/g, ' '); // Remove all newlines, except for those that start a new paragraph (two consecutive newlines)
return text;
};
21 changes: 15 additions & 6 deletions api/today/index.mjs
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
import { printToday } from '../src/index.mjs';
import { wxDiscussion } from '../src/wx.mjs';

export default async function (context, req) {
context.log('Starting execution');
try {
const utcOffset = req.query && req.query.utcOffset;
context.res = {
status: 200,
body: await printToday(utcOffset)
};
const source = req.query?.source ?? '';
if (source === 'wxdisc') {
context.res = {
status: 200,
body: await wxDiscussion()
};
} else {
const utcOffset = req.query && req.query.utcOffset;
context.res = {
status: 200,
body: await printToday(utcOffset)
};
}
} catch (err) {
context.res = {
status: 500,
body: 'Error'
};
context.log(`Error: ${err}`);
context.log(`Error: ${err}\n${err.stack}`);
}
}
23 changes: 3 additions & 20 deletions web/src/App.css
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
.App {
text-align: center;
}

.App-logo {
height: 40vmin;
pointer-events: none;
}

@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
text-align: left;
}

.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
Expand All @@ -28,11 +16,6 @@
color: #61dafb;
}

@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
.Wx-Disc {
padding: 2vh;
}
13 changes: 12 additions & 1 deletion web/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,29 @@ import './App.css';

function App() {
const [apiResponse, setApiResponse] = useState('');
const [wxDiscussionResponse, setWxDiscussionResponse] = useState('');

useEffect(() => {
fetch('/api/today')
.then(response => response.text())
.then(data => setApiResponse(data));
}, []);

useEffect(() => {
fetch('/api/today?source=wxdisc')
.then(response => response.text())
.then(data => setWxDiscussionResponse(data.split('\n\n').map((paragraph, index) => (
<p key={index}>{paragraph}</p>
))));
}, []);

return (
<div className="App">
<header className="App-header">
<pre>{apiResponse}</pre>
MyMinimums
</header>
<div className="Wx-Disc">{wxDiscussionResponse}</div>
<pre>{apiResponse}</pre>
</div>
);
}
Expand Down
6 changes: 4 additions & 2 deletions web/src/App.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import App from './App';

test('renders learn react link', () => {
render(<App />);
const linkElement = screen.getByText(/learn react/i);
expect(linkElement).toBeInTheDocument();
setTimeout(() => {
const linkElement = screen.getByText(/S43/i);
expect(linkElement).toBeInTheDocument();
}, 5000);
});
1 change: 0 additions & 1 deletion web/src/logo.svg

This file was deleted.

0 comments on commit c99f51b

Please sign in to comment.