Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit JD.News;
- {
- JD News API Client Wrapper
- Encapsulates obtaining top news headlines from multiple news sources
- API Documentation:
- https://newsapi.org/#documentation
- Instructions:
- 1) Create an instance of TJDNews
- 2) Populate properties
- - ApiKey (Required): The key issued to you by newsapi.org
- - Categories: Choose the categories you wish to include (None = all)
- - Languages: Choose the languages you wish to include (None = all)
- - Countries: Choose the countries you wish to include (None = all)
- 3) Call LoadSources to obtain a list of all possible news sources
- - Will be filtered based on selection for Categories, Languages, and Countries
- - Read result using SourceCount function and Sources property
- 4) Call LoadNews to obtain a list of all news stories for a given source
- - Parameter ASource: The unique ID of the news source
- - Read result using StoryCount function and Stories property
- }
- interface
- uses
- System.Classes, System.SysUtils, System.Generics.Collections,
- IdHTTP, XSuperObject, Winapi.Windows, Variants;
- type
- TJDNewsSource = class;
- TJDNewsStory = class;
- TJDNews = class;
- TJDNewsCategory = (ncBusiness, ncEntertainment, ncGaming, ncGeneral, ncMusic,
- ncPolitics, ncScienceAndNature, ncSports, ncTechnology);
- TJDNewsCategories = set of TJDNewsCategory;
- TJDNewsLanguage = (nlEnglish, nlGerman, nlFrench);
- TJDNewsLanguages = set of TJDNewsLanguage;
- TJDNewsCountry = (ncAustralia, ncGermany, ncGreatBrittain, ncIndia, ncItaly, ncUnitedStates);
- TJDNewsCountries = set of TJDNewsCountry;
- TNewsSortType = (nsTop, nsLatest, nsPopular);
- TNewsSortTypes = set of TNewsSortType;
- TJDNewsSource = class(TObject)
- private
- FObj: ISuperObject;
- function GetCategory: TJDNewsCategory;
- function GetCountry: TJDNewsCountry;
- function GetDescription: String;
- function GetID: String;
- function GetLanguage: TJDNewsLanguage;
- function GetName: String;
- function GetUrl: String;
- public
- constructor Create(AObj: ISuperObject);
- destructor Destroy; override;
- property ID: String read GetID;
- property Name: String read GetName;
- property Description: String read GetDescription;
- property Url: String read GetUrl;
- property Category: TJDNewsCategory read GetCategory;
- property Language: TJDNewsLanguage read GetLanguage;
- property Country: TJDNewsCountry read GetCountry;
- end;
- TJDNewsStory = class(TObject)
- private
- FObj: ISuperObject;
- function GetAuthor: String;
- function GetDescription: String;
- function GetPublishedAt: TDateTime;
- function GetTitle: String;
- function GetUrl: String;
- function GetUrlToImage: String;
- public
- constructor Create(AObj: ISuperObject);
- destructor Destroy; override;
- property Author: String read GetAuthor;
- property Title: String read GetTitle;
- property Description: String read GetDescription;
- property Url: String read GetUrl;
- property UrlToImage: String read GetUrlToImage;
- property PublishedAt: TDateTime read GetPublishedAt;
- end;
- TJDNews = class(TObject)
- private
- FCli: TIdHTTP;
- FApiKey: String;
- FSources: TObjectList<TJDNewsSource>;
- FStories: TObjectList<TJDNewsStory>;
- procedure SetApiKey(const Value: String);
- function GetStory(const Index: Integer): TJDNewsStory;
- function GetSource(const Index: Integer): TJDNewsSource;
- public
- constructor Create;
- destructor Destroy; override;
- property ApiKey: String read FApiKey write SetApiKey;
- procedure LoadSources;
- procedure LoadNews(const ASource: String);
- function StoryCount: Integer;
- property Stories[const Index: Integer]: TJDNewsStory read GetStory; default;
- function SourceCount: Integer;
- property Sources[const Index: Integer]: TJDNewsSource read GetSource;
- end;
- implementation
- uses
- System.DateUtils;
- function StrToNewsCategory(const Value: String): TJDNewsCategory;
- procedure Chk(const S: String; const V: TJDNewsCategory);
- begin
- if SameText(S, Value) then
- Result:= V;
- end;
- begin
- Result:= TJDNewsCategory.ncGeneral;
- Chk('business', TJDNewsCategory.ncBusiness);
- Chk('entertainment', TJDNewsCategory.ncEntertainment);
- Chk('gaming', TJDNewsCategory.ncGaming);
- Chk('general', TJDNewsCategory.ncGeneral);
- Chk('music', TJDNewsCategory.ncMusic);
- Chk('politics', TJDNewsCategory.ncPolitics);
- Chk('science-and-nature', TJDNewsCategory.ncScienceAndNature);
- Chk('sport', TJDNewsCategory.ncSports);
- Chk('technology', TJDNewsCategory.ncTechnology);
- end;
- function NewsCategoryToStr(const Value: TJDNewsCategory): String;
- begin
- Result:= 'general';
- case Value of
- ncBusiness: Result:= 'business';
- ncEntertainment: Result:= 'entertainment';
- ncGaming: Result:= 'gaming';
- ncGeneral: Result:= 'general';
- ncMusic: Result:= 'music';
- ncPolitics: Result:= 'politics';
- ncScienceAndNature: Result:= 'science-and-nature';
- ncSports: Result:= 'sport';
- ncTechnology: Result:= 'technology';
- end;
- end;
- function StrToNewsCountry(const Value: String): TJDNewsCountry;
- procedure Chk(const S: String; const V: TJDNewsCountry);
- begin
- if SameText(S, Value) then
- Result:= V;
- end;
- begin
- Result:= TJDNewsCountry.ncUnitedStates;
- Chk('au', TJDNewsCountry.ncAustralia);
- Chk('de', TJDNewsCountry.ncGermany);
- Chk('gb', TJDNewsCountry.ncGreatBrittain);
- Chk('in', TJDNewsCountry.ncIndia);
- Chk('it', TJDNewsCountry.ncItaly);
- Chk('us', TJDNewsCountry.ncUnitedStates);
- end;
- function NewsCountryToStr(const Value: TJDNewsCountry): String;
- begin
- Result:= 'us';
- case Value of
- ncAustralia: Result:= 'au';
- ncGermany: Result:= 'de';
- ncGreatBrittain: Result:= 'gb';
- ncIndia: Result:= 'in';
- ncItaly: Result:= 'it';
- ncUnitedStates: Result:= 'us';
- end;
- end;
- function StrToNewsLanguage(const Value: String): TJDNewsLanguage;
- procedure Chk(const S: String; const V: TJDNewsLanguage);
- begin
- if SameText(S, Value) then
- Result:= V;
- end;
- begin
- Result:= TJDNewsLanguage.nlEnglish;
- Chk('en', TJDNewsLanguage.nlEnglish);
- Chk('de', TJDNewsLanguage.nlGerman);
- Chk('fr', TJDNewsLanguage.nlFrench);
- end;
- function NewsLanguageToStr(const Value: TJDNewsLanguage): String;
- begin
- Result:= 'en';
- case Value of
- nlEnglish: Result:= 'en';
- nlGerman: Result:= 'de';
- nlFrench: Result:= 'fr';
- end;
- end;
- { TJDNewsStory }
- constructor TJDNewsStory.Create(AObj: ISuperObject);
- begin
- FObj:= AObj;
- if FObj <> nil then
- FObj._AddRef;
- end;
- destructor TJDNewsStory.Destroy;
- begin
- if FObj <> nil then
- FObj._Release;
- inherited;
- end;
- function TJDNewsStory.GetAuthor: String;
- begin
- Result:= FObj.S['author'];
- end;
- function TJDNewsStory.GetDescription: String;
- begin
- Result:= FObj.S['description'];
- end;
- function TJDNewsStory.GetPublishedAt: TDateTime;
- var
- T: String;
- begin
- try
- T:= FObj.S['publishedAt'];
- Result:= System.DateUtils.ISO8601ToDate(T, True);
- except
- on E: Exception do begin
- Result:= 0;
- end;
- end;
- end;
- function TJDNewsStory.GetTitle: String;
- begin
- Result:= FObj.S['title'];
- end;
- function TJDNewsStory.GetUrl: String;
- begin
- Result:= FObj.S['url'];
- end;
- function TJDNewsStory.GetUrlToImage: String;
- begin
- Result:= FObj.S['urlToImage'];
- end;
- { TJDNewsSource }
- constructor TJDNewsSource.Create(AObj: ISuperObject);
- begin
- FObj:= AObj;
- if FObj <> nil then
- FObj._AddRef;
- end;
- destructor TJDNewsSource.Destroy;
- begin
- if FObj <> nil then
- FObj._Release;
- inherited;
- end;
- function TJDNewsSource.GetCategory: TJDNewsCategory;
- begin
- Result:= StrToNewsCategory(FObj.S['category']);
- end;
- function TJDNewsSource.GetCountry: TJDNewsCountry;
- begin
- Result:= StrToNewsCountry(FObj.S['country']);
- end;
- function TJDNewsSource.GetDescription: String;
- begin
- Result:= FObj.S['description'];
- end;
- function TJDNewsSource.GetID: String;
- begin
- Result:= FObj.S['id'];
- end;
- function TJDNewsSource.GetLanguage: TJDNewsLanguage;
- begin
- Result:= StrToNewsLanguage(FObj.S['language']);
- end;
- function TJDNewsSource.GetName: String;
- begin
- Result:= FObj.S['name'];
- end;
- function TJDNewsSource.GetUrl: String;
- begin
- Result:= FObj.S['url'];
- end;
- { TJDNews }
- constructor TJDNews.Create;
- begin
- FCli:= TIdHTTP.Create(nil);
- FCli.HandleRedirects:= True;
- FStories:= TObjectList<TJDNewsStory>.Create(True);
- FSources:= TObjectList<TJDNewsSource>.Create(True);
- end;
- destructor TJDNews.Destroy;
- begin
- FSources.Clear;
- FStories.Clear;
- FreeAndNil(FSources);
- FreeAndNil(FStories);
- FreeAndNil(FCli);
- inherited;
- end;
- function TJDNews.GetSource(const Index: Integer): TJDNewsSource;
- begin
- Result:= FSources[Index];
- end;
- function TJDNews.GetStory(const Index: Integer): TJDNewsStory;
- begin
- Result:= FStories[Index];
- end;
- procedure TJDNews.SetApiKey(const Value: String);
- begin
- FApiKey := Value;
- end;
- function TJDNews.StoryCount: Integer;
- begin
- Result:= FStories.Count;
- end;
- function TJDNews.SourceCount: Integer;
- begin
- Result:= FSources.Count;
- end;
- procedure TJDNews.LoadSources;
- var
- Url: String;
- Res: String;
- Obj: ISuperObject;
- Arr: ISuperArray;
- X: Integer;
- S: TJDNewsSource;
- begin
- FSources.Clear;
- Url:= 'https://newsapi.org/v1/sources?language=en'; //TODO
- try
- Res:= FCli.Get(Url);
- Obj:= SO(Res);
- Arr:= Obj.A['sources'];
- for X := 0 to Arr.Length-1 do begin
- S:= TJDNewsSource.Create(Arr.O[X]);
- FSources.Add(S);
- end;
- except
- on E: Exception do begin
- //TODO
- end;
- end;
- end;
- procedure TJDNews.LoadNews(const ASource: String);
- var
- Url: String;
- Res: String;
- Obj: ISuperObject;
- Arr: ISuperArray;
- X: Integer;
- S: TJDNewsStory;
- begin
- FStories.Clear;
- Url:= 'https://newsapi.org/v1/articles?source='+ASource+'&sortBy=top&apiKey='+FApiKey;
- try
- Res:= FCli.Get(Url);
- Obj:= SO(Res);
- Arr:= Obj.A['articles'];
- for X := 0 to Arr.Length-1 do begin
- S:= TJDNewsStory.Create(Arr.O[X]);
- FStories.Add(S);
- end;
- except
- on E: Exception do begin
- //TODO
- end;
- end;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment