Skip to content

Commit

Permalink
feat: add zone to tribun news api
Browse files Browse the repository at this point in the history
  • Loading branch information
axmad386 committed Oct 4, 2021
1 parent 6aa2063 commit 87c02a5
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 21 deletions.
38 changes: 22 additions & 16 deletions api/controllers/newshandler/TribunNews.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Request, Response } from 'express'
import { parserRss } from '../../../utils/parser'
import { RSS_TRIBUN } from '../../../const'
import { TypeTribun, DataResponse } from '../../../types/common'
import { TypeTribun, ZoneTribun, DataResponse } from '../../../types/common'
import { useSearch } from '../../../utils/useSearch'
interface Params {
type?: TypeTribun
zone?: ZoneTribun
}

interface Title {
Expand All @@ -14,18 +15,19 @@ interface Title {
class TribunNews {
static async getNews(req: Request, res: Response){
try{
const { type }: Partial<Params> = req.params
const { type, zone }: Partial<Params> = req.params
const { title }: Partial<Title> = req.query
const url = `${RSS_TRIBUN}${type}`
const url = `${RSS_TRIBUN.replace('{zone}', zone)}/${type}`
const result = await parserRss(url)
const data = result.items.map((items) => {
items.image = items.enclosure.url.replace('thumbnails2', 'images')
delete items.pubDate
delete items.content
delete items.guid
delete items.enclosure
return items
})
// some item doesnt have image
items.image = items?.enclosure.url.replace('thumbnails2', 'images')
delete items.pubDate
delete items.content
delete items.guid
delete items.enclosure
return items
})
if(title !== undefined) {
const search = useSearch(data, title)
let dataSearch:any = []
Expand All @@ -35,7 +37,7 @@ class TribunNews {
const dataResponse: DataResponse = {
code: 200,
status: "OK",
messages: `Result of type ${type} news in Tribun News with title search: ${title}`,
messages: `Result of type ${type} news in Tribun News ${zone} with title search: ${title}`,
total: search.length,
data: dataSearch
}
Expand All @@ -45,7 +47,7 @@ class TribunNews {
const dataResponse: DataResponse = {
code: 200,
status: "OK",
messages: `Result of type ${type} news in Tribun News`,
messages: `Result of type ${type} news in Tribun News ${zone}`,
total: data.length,
data: data
}
Expand All @@ -59,17 +61,21 @@ class TribunNews {

static async getAllNews(req: Request, res: Response) {
try{
const { zone }: Partial<Params> = req.params
const { title }: Partial<Title> = req.query
const url = RSS_TRIBUN
const url = `${RSS_TRIBUN.replace('{zone}', zone||'api')}`
const result = await parserRss(url)
const data = result.items.map((items) => {
items.image = items.enclosure.url.replace('thumbnails2', 'images')
// some item doesnt have image
items.image = items.enclosure?.url.replace('thumbnails2', 'images')
delete items.pubDate
delete items.content
delete items.guid
delete items.enclosure
return items
})

const Zone = zone?' '+zone:''
if(title !== undefined) {
const search = useSearch(data, title)
let dataSearch:any = []
Expand All @@ -79,7 +85,7 @@ class TribunNews {
const dataResponse: DataResponse = {
code: 200,
status: "OK",
messages: `Result of all news in Tribun News with title: ${title}`,
messages: `Result of all news in Tribun News${Zone} with title: ${title}`,
total: search.length,
data: dataSearch
}
Expand All @@ -89,7 +95,7 @@ class TribunNews {
const dataResponse: DataResponse = {
code: 200,
status: "OK",
messages: `Result of all news in Tribun News`,
messages: `Result of all news in Tribun News${Zone}`,
total: data.length,
data: data
}
Expand Down
11 changes: 8 additions & 3 deletions api/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,13 @@ router.get('/', (_, res: Response) => {
},
"Tribun News": {
all: "https://berita-indo-api.vercel.app/v1/tribun-news",
zone: ["jakarta", "jabar", "mataram", "mataraman", "medan", "padang", "flores", "sulbar", "ambon",
"wartakota", "bogor", "pantura", "madura", "palembang", "pekanbaru", "banjarmasin", "pontianak", "papua", "bekasi",
"cirebon", "jogja", "bali", "bangka", "jambi", "kaltim", "palu", "papuabarat", "banten", "jateng", "jatim", "aceh",
"batam", "sumsel", "kalteng", "makassar", "tangerang", "solo", "surabaya", "prohaba", "belitung", "lampung", "kaltara",
"lombok", "depok", "banyumas", "suryamalang", "sultra", "babel", "kupang", "manado", "ternate"],
listType: ["bisnis", "superskor", "sport", "seleb", "lifestyle", "travel", "parapuan", "otomotif", "techno", "ramadan"],
example: "https://berita-indo-api.vercel.app/v1/tribun-news/techno",
example: "https://berita-indo-api.vercel.app/v1/tribun-news/jakarta/techno",
},
},
author: "Satya Wikananda",
Expand All @@ -78,8 +83,8 @@ router.get('/v1/okezone-news/:type', BeritaIndo.OkezoneNews.getNews)
router.get('/v1/liputan6-news/', BeritaIndo.Liputan6News.getAllNews)
router.get('/v1/bbc-news/:type', BeritaIndo.BbcNews.getNews)
router.get('/v1/bbc-news/', BeritaIndo.BbcNews.getAllNews)
router.get('/v1/tribun-news/:type', BeritaIndo.TribunNews.getNews)
router.get('/v1/tribun-news/', BeritaIndo.TribunNews.getAllNews)
router.get('/v1/tribun-news/:zone/:type', BeritaIndo.TribunNews.getNews)
router.get('/v1/tribun-news/:zone?', BeritaIndo.TribunNews.getAllNews)

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

Expand Down
2 changes: 1 addition & 1 deletion const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ 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://api.tribunnews.com/rss/'
export const RSS_TRIBUN: string = 'https://{zone}.tribunnews.com/rss/'
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "An api to display news in Indonesia",
"main": "index.js",
"scripts": {
"dev": "ts-node ./api/server.ts",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
Expand Down
7 changes: 6 additions & 1 deletion types/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ export type TypeBbc = "dunia" | "berita_indonesia" | "olahraga" | "majalah" | "m

export type TypeTribun = "bisnis" | "superskor" | "sport" | "seleb" | "lifestyle" | "travel" | "parapuan" | "otomotif" | "techno" | "ramadan"

export type ZoneTribun = "jakarta" | "jabar" | "mataram" | "mataraman" | "medan" | "padang" | "flores" | "sulbar" | "ambon" |
"wartakota" | "bogor" | "pantura" | "madura" | "palembang" | "pekanbaru" | "banjarmasin" | "pontianak" | "papua" | "bekasi" |
"cirebon" | "jogja" | "bali" | "bangka" | "jambi" | "kaltim" | "palu" | "papuabarat" | "banten" | "jateng" | "jatim" | "aceh" |
"batam" | "sumsel" | "kalteng" | "makassar" | "tangerang" | "solo" | "surabaya" | "prohaba" | "belitung" | "lampung" | "kaltara" |
"lombok" | "depok" | "banyumas" | "suryamalang" | "sultra" | "babel" | "kupang" | "manado" | "ternate"
export interface DataResponse {
code: number
status?: string
Expand All @@ -27,5 +32,5 @@ export interface ListsApi {
all?: string
type?: string
listType?: string[]
example?:string
example?: string
}

0 comments on commit 87c02a5

Please sign in to comment.