Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add zone to tribun news api #12

Merged
merged 2 commits into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 27 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,12 +47,17 @@ 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
}
return res.status(200).send(dataResponse)
} catch(e) {
if(e.message.includes('404')){
return res.status(404).send({
message: "Type not found for this zone"
})
}
return res.status(500).send({
message: `${e.message}`
})
Expand All @@ -59,17 +66,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 +90,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 +100,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
}