-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
79 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { RescueEvent } from "../types"; | ||
|
||
const events: { | ||
[hash: string]: RescueEvent; | ||
} = {}; | ||
|
||
const eventService = { | ||
async findAll(): Promise<RescueEvent[]> { | ||
return Object.values(events); | ||
}, | ||
async add(event: RescueEvent): Promise<void> { | ||
if (!(event.hash in events)) { | ||
events[event.hash] = { ...event }; | ||
} | ||
}, | ||
}; | ||
|
||
export default eventService; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import axios from "axios"; | ||
import iconv from "iconv-lite"; | ||
import hash from "object-hash"; | ||
import Parser from "rss-parser"; | ||
import { RescueEvent } from "../types"; | ||
|
||
const feedService = { | ||
async decodeFeed(): Promise<string> { | ||
const response = await axios.get<ArrayBuffer>( | ||
"http://www.peto-media.fi/tiedotteet/rss.xml", | ||
{ | ||
responseType: "arraybuffer", | ||
} | ||
); | ||
return iconv.decode(Buffer.from(response.data), "iso-8859-1"); | ||
}, | ||
async mapFeedToEvents(rawFeed: string): Promise<RescueEvent[]> { | ||
const parser = new Parser({ | ||
headers: { Accept: "application/rss+xml, text/xml; q=0.1" }, | ||
}); | ||
const feed = await parser.parseString(rawFeed); | ||
return feed.items.map((item) => { | ||
if (item.title === undefined) { | ||
throw new Error(`Failed to parse ${item}`); | ||
} | ||
const [location, type] = item.title?.split(","); | ||
const finnishLocation = location.split("/")[0].trim(); | ||
const event = { | ||
location: finnishLocation.trim(), | ||
type: type.trim(), | ||
time: new Date(item.isoDate ?? new Date()), | ||
}; | ||
return { | ||
...event, | ||
hash: hash(event), | ||
}; | ||
}); | ||
}, | ||
}; | ||
|
||
export default feedService; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import eventService from "./eventService"; | ||
import feedService from "./feedService"; | ||
|
||
export { eventService, feedService }; |