Skip to content

Commit

Permalink
Add method to get featured content by date
Browse files Browse the repository at this point in the history
resolves #34
  • Loading branch information
friendofdog committed Jan 8, 2022
1 parent 0529be1 commit 585d4f0
Show file tree
Hide file tree
Showing 7 changed files with 272 additions and 4 deletions.
7 changes: 7 additions & 0 deletions source/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ export class eventsError extends wikiError {
}
}

export class fcError extends wikiError {
constructor(message: string) {
super(message);
this.name = 'featuredContentError';
}
}

export class pdfError extends wikiError {
constructor(message: string) {
super(message);
Expand Down
30 changes: 26 additions & 4 deletions source/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import request, { makeRestRequest, setAPIUrl } from './request'
import { pageOptions, searchOptions, geoOptions, listOptions, eventOptions, randomFormats, pdfOptions, citationFormat } from './optionTypes';
import { pageOptions, searchOptions, geoOptions, listOptions, eventOptions, fcOptions, randomFormats, pdfOptions, citationFormat } from './optionTypes';
import Page, {
intro, images, html, content, categories, links, coordinates, langLinks,
references, infobox, tables, summary, related, media, mobileHtml, pdf, citation
} from './page';
import { coordinatesResult, eventResult, geoSearchResult, imageResult, langLinksResult, languageResult,
import { coordinatesResult, eventResult, featuredContentResult, geoSearchResult, imageResult, langLinksResult, languageResult,
mobileSections, relatedResult,
title, wikiMediaResult, wikiSearchResult, wikiSummary, notFound } from './resultTypes';
import {
categoriesError,
contentError, coordinatesError, eventsError, geoSearchError, htmlError, imageError, infoboxError,
contentError, coordinatesError, eventsError, fcError, geoSearchError, htmlError, imageError, infoboxError,
introError, linksError, mediaError, pageError, relatedError, searchError, summaryError, wikiError,
pdfError,
citationError
} from './errors';
import { MSGS } from './messages';
import { getCurrentDay, getCurrentMonth, setPageId, setPageIdOrTitleParam, setTitleForPage } from './utils';
import { getCurrentDay, getCurrentMonth, getCurrentYear, setPageId, setPageIdOrTitleParam, setTitleForPage } from './utils';

/**
* The default wiki export
Expand Down Expand Up @@ -529,6 +529,28 @@ wiki.onThisDay = async (eventOptions: eventOptions = {}): Promise<eventResult> =
}
}

/**
* Returns featured content for a given day
*
* @remarks
* The api returns content featured at a particular date
*
* @param fcOptions - the year/month/day of featured content by {@link fcOptions | eventOptions}
* @returns Returns the results as array of {@link fcResult | fcResult}
*/
wiki.featuredContent = async (fcOptions: fcOptions = {}): Promise<featuredContentResult> => {
try {
const yyyy = (fcOptions.year || getCurrentYear()).toString()
const mm = (fcOptions.month || getCurrentMonth()).toString().padStart(2, "0")
const dd = (fcOptions.day || getCurrentDay()).toString().padStart(2, "0")
const path = `feed/featured/${yyyy}/${mm}/${dd}`;
const result = await makeRestRequest(path, true);
return result;
} catch (error) {
throw new fcError(error);
}
}

/**
* Returns a random page
*
Expand Down
6 changes: 6 additions & 0 deletions source/optionTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ export interface eventOptions {
day?: string
}

export interface fcOptions {
year?: string,
month?: string,
day?: string
}

export type eventTypes =
'all' | 'selected' | 'births' | 'deaths' | 'events' | 'holidays'

Expand Down
68 changes: 68 additions & 0 deletions source/resultTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,17 @@ export interface wikiSummary {
}
}

export interface MostRead extends wikiSummary {
views: number;
rank: number;
view_history: [
{
date: string;
views: number;
}
]
}

export interface wikiMediaResult {
revision: string,
tid: string,
Expand Down Expand Up @@ -239,10 +250,67 @@ export interface htmlText {
text: string
}

export interface Artist extends htmlText {
name: string;
user_page?: string;
}

export interface Description extends htmlText {
lang: string;
}

export interface notFound {
type: string,
title: string,
method: string,
detail: string,
uri: string
}

export interface featuredContentResult {
tfa: wikiSummary;
mostread: {
date: string;
articles: Array<wikiSummary>
};
image: {
title: string;
thumbnail: {
source: string;
width: number;
height: number;
};
image: {
source: string;
width: number;
height: number;
};
file_page: string;
artist: Artist;
credit: htmlText;
license: {
type: string;
code: string;
};
description: Description;
wb_entity_id: string;
structured: {
captions: {
[key: string]: string;
}
};
};
news: [
{
links: Array<wikiSummary>;
story: string;
}
];
onthisday: [
{
text: string;
pages: Array<wikiSummary>;
year: number;
}
]
}
7 changes: 7 additions & 0 deletions source/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ export function setPageId(params: any, results: any): number {
return pageId;
}

//Get current year
export function getCurrentYear(): number {
const date = new Date();
const year = date.getFullYear();
return (year);
}

//Get current month
export function getCurrentMonth(): number {
const date = new Date();
Expand Down
39 changes: 39 additions & 0 deletions test/featuredContent.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { fcError } from '../source/errors';
import * as request from '../source/request';
import wiki from "../source/index";
import { fcData } from './samples';
const requestMock = jest.spyOn(request, "makeRestRequest");

const fcMock = fcData;

afterAll(() => {
requestMock.mockRestore();
})

test('Throws featured content error if response is error', async () => {
requestMock.mockImplementation(async () => { throw new Error("This is an error") });
const result = async () => {
await wiki.featuredContent({})
};
expect(result).rejects.toThrowError(fcError);
});

test('Returns with results as fcResult', async () => {
requestMock.mockImplementation(async () => { return fcMock });
const result = await wiki.featuredContent();
expect(result).toStrictEqual(fcMock);
});

test('Featured content method call with params passed and no padding', async () => {
requestMock.mockImplementation(async () => { return fcMock });
const result = await wiki.featuredContent({ year: '2020', month: '1', day: '1' });
expect(result).toStrictEqual(fcMock);
expect(requestMock).toBeCalledWith(`feed/featured/2020/01/01`, true);
});

test('Featured content method call with params passed', async () => {
requestMock.mockImplementation(async () => { return fcMock });
const result = await wiki.featuredContent({ year: '2020', month: '01', day: '01' });
expect(result).toStrictEqual(fcMock);
expect(requestMock).toBeCalledWith(`feed/featured/2020/01/01`, true);
});
119 changes: 119 additions & 0 deletions test/samples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,122 @@ export const citationData = {
}
]
}

const sampleSummary = {
"type": "standard",
"title": "Jared_Lee_Loughner",
"displaytitle": "Jared Lee Loughner",
"namespace": {
"id": 0,
"text": ""
},
"wikibase_item": "Q4267485",
"titles": {
"canonical": "Jared_Lee_Loughner",
"normalized": "Jared Lee Loughner",
"display": "Jared Lee Loughner"
},
"pageid": 30372228,
"thumbnail": {
"source": "https://upload.wikimedia.org/wikipedia/commons/thumb/8/82/Jared_Loughner_USMS.jpg/256px-Jared_Loughner_USMS.jpg",
"width": 256,
"height": 320
},
"originalimage": {
"source": "https://upload.wikimedia.org/wikipedia/commons/8/82/Jared_Loughner_USMS.jpg",
"width": 480,
"height": 600
},
"lang": "en",
"dir": "ltr",
"revision": "1063449101",
"tid": "fa00d540-6d07-11ec-b627-0543a4912f8c",
"timestamp": "2022-01-03T04:11:55Z",
"description": "American mass murderer",
"description_source": "local",
"content_urls": {
"desktop": {
"page": "https://en.wikipedia.org/wiki/Jared_Lee_Loughner",
"revisions": "https://en.wikipedia.org/wiki/Jared_Lee_Loughner?action=history",
"edit": "https://en.wikipedia.org/wiki/Jared_Lee_Loughner?action=edit",
"talk": "https://en.wikipedia.org/wiki/Talk:Jared_Lee_Loughner"
},
"mobile": {
"page": "https://en.m.wikipedia.org/wiki/Jared_Lee_Loughner",
"revisions": "https://en.m.wikipedia.org/wiki/Special:History/Jared_Lee_Loughner",
"edit": "https://en.m.wikipedia.org/wiki/Jared_Lee_Loughner?action=edit",
"talk": "https://en.m.wikipedia.org/wiki/Talk:Jared_Lee_Loughner"
}
},
"rxtract": "abc",
"extract_html": "<p>abc</p>",
"normalizedtitle": "abc"
}

export const fcData = {
"tfa": sampleSummary,
"mostread": {
"date": "2022-01-07Z",
"articles": [
{
"views": 1064940,
"rank": 3,
"view_history": [
{
"date": "2022-01-07Z",
"views": 1064940
}
],
...sampleSummary
},
]
},
"image": {
"title": "File:1930s Japan Travel Poster - 01.jpg",
"thumbnail": {
"source": "https://upload.wikimedia.org/wikipedia/commons/thumb/5/54/1930s_Japan_Travel_Poster_-_01.jpg/640px-1930s_Japan_Travel_Poster_-_01.jpg",
"width": 640,
"height": 934
},
"image": {
"source": "https://upload.wikimedia.org/wikipedia/commons/5/54/1930s_Japan_Travel_Poster_-_01.jpg",
"width": 2056,
"height": 3000
},
"file_page": "https://commons.wikimedia.org/wiki/File:1930s_Japan_Travel_Poster_-_01.jpg",
"artist": {
"html": "Japanese Government Railways",
"text": "Japanese Government Railways",
"name": "Japanese Government Railways"
},
"credit": {
"html": "Heritage Auctions",
"text": "Heritage Auctions"
},
"license": {
"type": "Public domain",
"code": "pd"
},
"description": {
"html": "Sea bathing in Obama",
"text": "Sea bathing in Obama",
"lang": "en"
},
"wb_entity_id": "M31526367",
"structured": {
"captions": {}
}
},
"news": [
{
"links": [ sampleSummary ],
"story": "<!--Jan 7-->Bahamian-American actor."
},
],
"onthisday": [
{
"text": "abc",
"pages": [ sampleSummary ]
}
],
}

0 comments on commit 585d4f0

Please sign in to comment.