Skip to content
Permalink
Browse files

Update README and default route / to show top headlines

  • Loading branch information...
omgimanerd committed Mar 4, 2019
1 parent 2c125c5 commit ca43f2260b963a6a5f87e5efaef7ba241e8f6292
Showing with 57 additions and 30 deletions.
  1. +4 −4 README.md
  2. +53 −25 server.js
  3. +0 −1 server/formatter.js
@@ -38,14 +38,14 @@ omgimanerd:~$ curl getnews.tech/trump
│ https://github.com/omgimanerd/getnews.tech │
└────────────────────────────────────────────────────────────────────────┘
```
This is a web server that formats the news from various sources on the web for
display in the terminal. A link to each article is included if you want to read
the full article.
This is a web server that formats the news (primarily US based) from various
sources on the web for display in the terminal. A link to each article is
included if you want to read the full article.

## Usage
You can fetch the latest news simply by typing
```bash
curl getnews.tech/<topic>
curl getnews.tech
curl getnews.tech/trump
curl getnews.tech/world+cup
curl getnews.tech/russia+alien+invasion
@@ -52,24 +52,46 @@ app.use(loggers.devLoggerMiddleware)
// Write more specific log information to the server log file
app.use(loggers.analyticsLoggerMiddleware)

app.use((request, response, next) => {
app.use(async(request, response, next) => {
request.isCurl = (request.headers['user-agent'] || '').includes('curl')
try {
const locationData = await iplocate(request.headers['x-forwarded-for'])
request.timezone = locationData.time_zone
} catch (error) {
request.timezone = moment.tz.guess()
}
next()
})

app.get('/s/:short', async(request, response, next) => {
if (request.isCurl) {
/**
* Helper function to shorten the URLs in the articles returned from the
* News API.
* @param {Array<Object>} articles The articles with URLs to shorten
* @return {Array<Object>}
*/
const shortenArticleUrls = async articles => {
const sorted = articles.sort((a, b) => {
return moment(a.publishedAt).diff(moment(b.publishedAt))
})
const shortenedUrls = await Promise.all(sorted.map(article => {
return urlShortener.getShortenedUrl(client, article.url)
}))
return articles.map((article, i) => {
article.url = shortenedUrls[i]
return article
})
}

app.get('/', async(request, response, next) => {
if (!request.isCurl) {
next()
return
}
try {
const url = await urlShortener.getOriginalUrl(
client, request.params.short)
if (url === null) {
next()
} else {
response.redirect(url)
}
const result = await api.v2.topHeadlines({ country: 'us' })
const articles = await shortenArticleUrls(result.articles)
const output = formatter.formatArticles(articles, request.timezone)
response.send(output)
} catch (error) {
logError(error)
response.status(500).send(INTERNAL_ERROR)
@@ -84,28 +106,34 @@ app.get('/:query', async(request, response, next) => {
try {
const q = request.params.query.replace('+', ' ')
const result = await api.v2.everything({ q })
const articles = result.articles.sort((a, b) => {
return moment(a.publishedAt).diff(moment(b.publishedAt))
})
const shortenedUrls = await Promise.all(articles.map(article => {
return urlShortener.getShortenedUrl(client, article.url)
}))
articles.forEach((article, i) => { article.url = shortenedUrls[i] })
let timezone = null
try {
const locationData = await iplocate(request.headers['x-forwarded-for'])
timezone = locationData.time_zone
} catch (error) {
timezone = moment.tz.guess()
}
const output = formatter.formatArticles(articles, timezone)
const articles = await shortenArticleUrls(result.articles)
const output = formatter.formatArticles(articles, request.timezone)
response.send(output)
} catch (error) {
logError(error)
response.status(500).send(INTERNAL_ERROR)
}
})

app.get('/s/:short', async(request, response, next) => {
if (request.isCurl) {
next()
return
}
try {
const url = await urlShortener.getOriginalUrl(
client, request.params.short)
if (url === null) {
next()
} else {
response.redirect(url)
}
} catch (error) {
logError(error)
response.status(500).send(INTERNAL_ERROR)
}
})

app.use((request, response) => {
if (request.isCurl) {
response.status(404).send(formatter.formatMessage(INVALID_QUERY.red))
@@ -87,7 +87,6 @@ const formatMessage = message => {
* It assumes that the data has the fields outlined in the documentation
* on the News API developer documentation, and that the url to the article
* has also been shortened.
* https://newsapi.org/#documentation
* @param {Array<Object>} articles A list of articles returned by a query to
* the News API.
* @param {string} timezone The timezone of the requesting IP address

0 comments on commit ca43f22

Please sign in to comment.
You can’t perform that action at this time.