Skip to content

Commit

Permalink
feat(API): Add search title API feature (#7)
Browse files Browse the repository at this point in the history
* feat(API): Add search title API feature

* docs: Update docs
  • Loading branch information
satyawikananda authored May 14, 2021
1 parent b436692 commit 3b0f57c
Show file tree
Hide file tree
Showing 15 changed files with 921 additions and 681 deletions.
2 changes: 1 addition & 1 deletion README-ID.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Berita Indo API adalah sebuah API yang menampilkan banyak berita di Indonesia se
- [x] Berita Antara
- [x] Berita Kumparan
- [x] Berita Okezone
- [x] Berita Liputan 6
- [x] ~~Berita Liputan 6~~ (API berita ini mungkin tidak tersedia lagi, dikarenakan ada isu RSS dari Liputan6)
- [x] Berita BBC
- [x] Berita Tribun
- Tingkatkan API
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ Berita Indo API (or in English is Indonesian News API) is an API to display many
- `/v1/tribun-news`: Get all news data of Tribun News
- `/v1/tribun-news/:type` : Get specific news data by type news of Tribun News

> 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.
## To-do List

- News
Expand All @@ -44,11 +46,11 @@ Berita Indo API (or in English is Indonesian News API) is an API to display many
- [x] Antara News
- [x] Kumparan News
- [x] Okezone News
- [x] Liputan 6 News
- [x] ~~Liputan 6 News~~ (This RSS feed maybe isn't available again)
- [x] BBC News
- [x] Tribun News
- Improve API
- [ ] Search data news
- [x] Search data news
- [ ] Paginate data

## Installation
Expand Down
26 changes: 23 additions & 3 deletions api/controllers/newshandler/AntaraNews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ import { Request, Response } from 'express'
import { parserRss } from '../../../utils/parser'
import { RSS_ANTARA_NEWS } from '../../../const'
import { TypeAntara, DataResponse } from '../../../types/common'

import { useSearch } from '../../../utils/useSearch'
interface Params {
type?: TypeAntara
}

interface Title {
title: string
}

class AntaraNews {
static async getNews(req: Request, res: Response) {
try {
const { type }: Partial<Params> = req.params
let url = `${RSS_ANTARA_NEWS}${type}`

const { title }: Partial<Title> = req.query
const url = `${RSS_ANTARA_NEWS}${type}`
const result = await parserRss(url)
const data = result.items.map((items) => {
const image = items.content.match(/\<img.+src\=(?:\"|\')(.+?)(?:\"|\')(?:.+?)\>/)[1]
Expand All @@ -24,6 +28,22 @@ class AntaraNews {
delete items.content
return items
})
if(title !== undefined) {
const search = useSearch(data, title)
let dataSearch:any = []
search.map((items) => {
dataSearch.push(items.item)
})
const dataResponse: DataResponse = {
code: 200,
status: "OK",
messages: `Result of type ${type} news in Antara News with title search: ${title}`,
total: search.length,
data: dataSearch
}

return res.status(200).send(dataResponse)
}
const dataResponse: DataResponse = {
code: 200,
status: "OK",
Expand Down
39 changes: 38 additions & 1 deletion api/controllers/newshandler/BbcNews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@ import { Request, Response } from 'express'
import { parserRss } from '../../../utils/parser'
import { RSS_BBC } from '../../../const'
import { TypeBbc, DataResponse } from '../../../types/common'
import { useSearch } from '../../../utils/useSearch'

interface Params {
type?: TypeBbc
}

interface Title {
title: string
}

class BbcNews {
static async getNews(req: Request, res: Response) {
try {
const { type }: Partial<Params> = req.params
const { title }: Partial<Title> = req.query
const url = RSS_BBC.replace('{type}', type)
const result = await parserRss(url, {
item: ['description']
Expand All @@ -22,6 +28,21 @@ class BbcNews {
delete items.guid
return items
})
if(title !== undefined) {
const search = useSearch(data, title)
let dataSearch:any = []
search.map((items) => {
dataSearch.push(items.item)
})
const dataResponse: DataResponse = {
code: 200,
status: "OK",
messages: `Result of type ${type} news in BBC News with title search: ${title}`,
total: search.length,
data: dataSearch
}
return res.status(200).send(dataResponse)
}
const dataResponse: DataResponse = {
code: 200,
status: "OK",
Expand All @@ -37,8 +58,9 @@ class BbcNews {
}
}

static async getAllNews(_, res: Response) {
static async getAllNews(req: Request, res: Response) {
try {
const { title }: Partial<Title> = req.query
const url = RSS_BBC.replace('{type}', '')
const result = await parserRss(url, {
item: ['description']
Expand All @@ -50,6 +72,21 @@ class BbcNews {
delete items.guid
return items
})
if(title !== undefined) {
const search = useSearch(data, title)
let dataSearch:any = []
search.map((items) => {
dataSearch.push(items.item)
})
const dataResponse: DataResponse = {
code: 200,
status: "OK",
messages: `Result of all news in BBC News with title: ${title}`,
total: search.length,
data: dataSearch
}
return res.status(200).send(dataResponse)
}
const dataResponse: DataResponse = {
code: 200,
status: "OK",
Expand Down
44 changes: 40 additions & 4 deletions api/controllers/newshandler/CnbcNews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@ import { parserRss } from '../../../utils/parser'
import { replaceQueryParams } from '../../../utils/replaceQueryParams'
import { RSS_CNBC_NEWS } from '../../../const'
import { TypeCnbc, DataResponse } from '../../../types/common'
import { useSearch } from '../../../utils/useSearch'

interface Params {
type?: TypeCnbc
}

interface Title {
title: string
}

class CnbcNews {
static async getNews(req: Request, res: Response) {
try {
const { type }: Partial<Params> = req.params
const { title }: Partial<Title> = req.query
let url = RSS_CNBC_NEWS.replace('{type}', type)

const result = await parserRss(url)
const data = result.items.map((items) => {
const image = replaceQueryParams(items.enclosure.url, 'q', '100')
Expand All @@ -29,6 +34,21 @@ class CnbcNews {
delete items.enclosure
return items
})
if(title !== undefined) {
const search = useSearch(data, title)
let dataSearch:any = []
search.map((items) => {
dataSearch.push(items.item)
})
const dataResponse: DataResponse = {
code: 200,
status: "OK",
messages: `Result of type ${type} news in CNBC News with title search: ${title}`,
total: search.length,
data: dataSearch
}
return res.status(200).send(dataResponse)
}
const dataResponse: DataResponse = {
code: 200,
status: "OK",
Expand All @@ -44,10 +64,10 @@ class CnbcNews {
}
}

static async getAllNews(_, res: Response) {
static async getAllNews(req: Request, res: Response) {
try {
let url = RSS_CNBC_NEWS.replace('/{type}', '')

const url = RSS_CNBC_NEWS.replace('/{type}', '')
const { title }: Partial<Title> = req.query
const result = await parserRss(url)
const data = result.items.map((items) => {
const image = replaceQueryParams(items.enclosure.url, 'q', '100')
Expand All @@ -63,6 +83,22 @@ class CnbcNews {
delete items.enclosure
return items
})
if(title !== undefined) {
const search = useSearch(data, title)
let dataSearch:any = []
search.map((items) => {
dataSearch.push(items.item)
})
const dataResponse: DataResponse = {
code: 200,
status: "OK",
messages: `Result of all news in CNBC News with title search: ${title}`,
total: search.length,
data: dataSearch
}

return res.status(200).send(dataResponse)
}
const dataResponse: DataResponse = {
code: 200,
status: "OK",
Expand Down
47 changes: 42 additions & 5 deletions api/controllers/newshandler/CnnNews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@ import { parserRss } from '../../../utils/parser'
import { replaceQueryParams } from '../../../utils/replaceQueryParams'
import { RSS_CNN_NEWS } from '../../../const'
import { TypeCnn, DataResponse } from '../../../types/common'
import { useSearch } from '../../../utils/useSearch'

interface Params {
type?: TypeCnn
}

interface Title {
title: string
}
class CnnNews {
static async getNews(req: Request, res: Response) {
try {
const { type }: Partial<Params> = req.params
let url = RSS_CNN_NEWS.replace('{type}', type)

const { title }: Partial<Title> = req.query
const url = RSS_CNN_NEWS.replace('{type}', type)
const result = await parserRss(url)
const data = result.items.map((items) => {
const image = replaceQueryParams(items.enclosure.url, 'q', '100')
Expand All @@ -28,6 +33,22 @@ class CnnNews {
delete items.enclosure
return items
})
if(title !== undefined) {
const search = useSearch(data, title)
let dataSearch:any = []
search.map((items) => {
dataSearch.push(items.item)
})
const dataResponse: DataResponse = {
code: 200,
status: "OK",
messages: `Result of type ${type} news in CNN News with title search: ${title}`,
total: search.length,
data: dataSearch
}

return res.status(200).send(dataResponse)
}
const dataResponse: DataResponse = {
code: 200,
status: "OK",
Expand All @@ -43,10 +64,10 @@ class CnnNews {
}
}

static async getAllNews(_, res: Response) {
static async getAllNews(req: Request, res: Response) {
try {
let url = RSS_CNN_NEWS.replace('/{type}', '')

const { title }: Partial<Title> = req.query
const url = RSS_CNN_NEWS.replace('/{type}', '')
const result = await parserRss(url)
const data = result.items.map((items) => {
const image = replaceQueryParams(items.enclosure.url, 'q', '100')
Expand All @@ -62,6 +83,22 @@ class CnnNews {
delete items.enclosure
return items
})
if(title !== undefined) {
const search = useSearch(data, title)
let dataSearch:any = []
search.map((items) => {
dataSearch.push(items.item)
})
const dataResponse: DataResponse = {
code: 200,
status: "OK",
messages: `Result of all news in CNN News with title search: ${title}`,
total: search.length,
data: dataSearch
}

return res.status(200).send(dataResponse)
}
const dataResponse: DataResponse = {
code: 200,
status: "OK",
Expand Down
27 changes: 24 additions & 3 deletions api/controllers/newshandler/KumparanNews.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { Response } from 'express'
import { Response, Request } from 'express'
import { parserRss } from '../../../utils/parser'
import { RSS_KUMPARAN_NEWS } from '../../../const'
import { DataResponse } from '../../../types/common'
import { useSearch } from '../../../utils/useSearch'

interface Title {
title: string
}
class KumparanNews {
static async getAllNews(_, res: Response) {
static async getAllNews(req: Request, res: Response) {
try {
let url = RSS_KUMPARAN_NEWS
const { title }: Partial<Title> = req.query
const url = RSS_KUMPARAN_NEWS
const result = await parserRss(url)
const data = result.items.map((items) => {
items.description = items.contentSnippet
Expand All @@ -27,6 +32,22 @@ class KumparanNews {
delete items.enclosure
return items
})
if(title !== undefined) {
const search = useSearch(data, title)
let dataSearch:any = []
search.map((items) => {
dataSearch.push(items.item)
})
const dataResponse: DataResponse = {
code: 200,
status: "OK",
messages: `Result of all news in Kumaparan News with title search: ${title}`,
total: search.length,
data: dataSearch
}

return res.status(200).send(dataResponse)
}
const dataResponse: DataResponse = {
code: 200,
status: "OK",
Expand Down
5 changes: 5 additions & 0 deletions api/controllers/newshandler/Liputan6News.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
This RSS feed maybe isn't available again
and this API will be unused
*/

import { Response } from 'express'
import { parserRss } from '../../../utils/parser'
import { RSS_LIPUTAN6 } from '../../../const'
Expand Down
Loading

1 comment on commit 3b0f57c

@vercel
Copy link

@vercel vercel bot commented on 3b0f57c May 14, 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.