Skip to content

Commit

Permalink
feat(API): add vice indonesia api (#19)
Browse files Browse the repository at this point in the history
* add vice indonesia api

* add vice to documentation list
  • Loading branch information
danarn17 authored Oct 7, 2021
1 parent b6809ad commit a5ef51a
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Berita Indo API (or in English is Indonesian News API) is an API to display many
- `/v1/tribun-news/:zone/:type` : Get specific news data by zone and type news of Tribun News
- `/v1/jawa-pos/:type`: Get all news data of Jawa Pos News
- `/v1/jawa-pos/:type`: Get specific news data by type news of Jawa Pos News
- `/v1/vice/`: Get all news data of Vice Indonesia

> Each API Endpoint have a query paramaters named 'title', and this query parameters will be usefull if you want to search the API data by the title.
Expand All @@ -53,6 +54,7 @@ Berita Indo API (or in English is Indonesian News API) is an API to display many
- [x] BBC News
- [x] Tribun News
- [x] Jawa Pos News
- [x] Vice
- Improve API
- [x] Search data news
- [ ] Paginate data
Expand Down
4 changes: 4 additions & 0 deletions api/controllers/graphql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ const listsApi: ListsApi[] = [
label: 'Liputan 6 News',
all: 'https://berita-indo-api.vercel.app/v1/liputan6-news',
},
{
label: 'Vice',
all: 'https://berita-indo-api.vercel.app/v1/vice',
},
];

export const resolvers = {
Expand Down
2 changes: 2 additions & 0 deletions api/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Liputan6News from './newshandler/Liputan6News'
import BbcNews from './newshandler/BbcNews'
import TribunNews from './newshandler/TribunNews'
import JawaPosNews from './newshandler/JawaPosNews'
import ViceNews from './newshandler/ViceNews'
import notFound from './404'

export default {
Expand All @@ -23,5 +24,6 @@ export default {
BbcNews,
TribunNews,
JawaPosNews,
ViceNews,
notFound
}
54 changes: 54 additions & 0 deletions api/controllers/newshandler/ViceNews.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Response, Request } from 'express'
import { parserRss } from '../../../utils/parser'
import { RSS_VICE } from '../../../const'
import { DataResponse } from '../../../types/common'

interface Params {
page?: string
}

const limitString = (str: string, limit: number):string => {
const string: string = str
const length: number = limit
const result: string = string.length > length ? string.substring(0, length) + '....' : string;
return result
}

class ViceNews {
static async getAllNews(req: Request, res: Response) {
try {
const { page }: Partial<Params> = req.params
const url = RSS_VICE.replace('{page}', `&page=${page}` ?? "")
const result = await parserRss(url)
const data = result.items.map((items) => {
items.image = {
small: items.enclosure.url
}
items.description = limitString(items.contentSnippet, 300)
delete items.contentSnippet
delete items.pubDate
delete items.enclosure
delete items.description
delete items.guid
delete items['content:encoded']
delete items['content:encodedSnippet']
delete items['dc:creator']
return items
})
const dataResponse: DataResponse = {
code: 200,
status: "OK",
messages: `Result of all news in Vice Indonesia`,
total: data.length,
data: data
}
return res.status(200).send(dataResponse)
} catch (e) {
return res.status(500).send({
message: `${e.message}`
})
}
}
}

export default ViceNews
6 changes: 5 additions & 1 deletion api/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ router.get('/', (_, res: Response) => {
all: "https://berita-indo-api.vercel.app/v1/jawa-pos",
listType: ["nasional", "entertainment", "pendidikan", "hukum-kriminal", "pemilihan", "sepak-bola", "jabodetabek", "internasional", "lifestyle", "kesehatan", "infrastruktur", "features", "oto-dan-tekno", "arsitektur-dan-desain", "art-space", "opini", "wisata-dan-kuliner", "hoax-atau-bukan"],
example: "https://berita-indo-api.vercel.app/v1/jawa-pos/nasional"
}
},
"Vice": {
all: "https://berita-indo-api.vercel.app/v1/vice",
},
},
author: "Satya Wikananda",
source: "https://github.com/satyawikananda/berita-indo-api"
Expand All @@ -91,6 +94,7 @@ router.get('/v1/bbc-news/', BeritaIndo.BbcNews.getAllNews)
router.get('/v1/tribun-news/:zone/:type', BeritaIndo.TribunNews.getNews)
router.get('/v1/tribun-news/:zone?', BeritaIndo.TribunNews.getAllNews)
router.get('/v1/jawa-pos/:type?', BeritaIndo.JawaPosNews.getAllNews)
router.get('/v1/vice/:page?', BeritaIndo.ViceNews.getAllNews)

router.all('*', BeritaIndo.notFound)

Expand Down
3 changes: 2 additions & 1 deletion const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ export const RSS_OKEZONE = {
export const RSS_LIPUTAN6: string = 'https://feed.liputan6.com/rss'
export const RSS_BBC: string = 'https://feeds.bbci.co.uk/indonesia/{type}/rss.xml'
export const RSS_TRIBUN: string = 'https://{zone}.tribunnews.com/rss/'
export const RSS_JAWAPOS: string = 'https://www.jawapos.com/{type}/feed/'
export const RSS_JAWAPOS: string = 'https://www.jawapos.com/{type}/feed/'
export const RSS_VICE: string = 'https://www.vice.com/id/rss?locale=id_id{page}'

1 comment on commit a5ef51a

@vercel
Copy link

@vercel vercel bot commented on a5ef51a Oct 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.