djjd47130

Delphi News API Wrapper (In Progress)

Nov 3rd, 2017
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 10.33 KB | None | 0 0
  1. unit JD.News;
  2.  
  3. {
  4.   JD News API Client Wrapper
  5.     Encapsulates obtaining top news headlines from multiple news sources
  6.  
  7.   API Documentation:
  8.     https://newsapi.org/#documentation
  9.  
  10.   Instructions:
  11.     1) Create an instance of TJDNews
  12.     2) Populate properties
  13.       - ApiKey (Required): The key issued to you by newsapi.org
  14.       - Categories: Choose the categories you wish to include (None = all)
  15.       - Languages: Choose the languages you wish to include (None = all)
  16.       - Countries: Choose the countries you wish to include (None = all)
  17.     3) Call LoadSources to obtain a list of all possible news sources
  18.       - Will be filtered based on selection for Categories, Languages, and Countries
  19.       - Read result using SourceCount function and Sources property
  20.     4) Call LoadNews to obtain a list of all news stories for a given source
  21.       - Parameter ASource: The unique ID of the news source
  22.       - Read result using StoryCount function and Stories property
  23. }
  24.  
  25. interface
  26.  
  27. uses
  28.   System.Classes, System.SysUtils, System.Generics.Collections,
  29.   IdHTTP, XSuperObject, Winapi.Windows, Variants;
  30.  
  31. type
  32.   TJDNewsSource = class;
  33.   TJDNewsStory = class;
  34.   TJDNews = class;
  35.  
  36.  
  37.   TJDNewsCategory = (ncBusiness, ncEntertainment, ncGaming, ncGeneral, ncMusic,
  38.     ncPolitics, ncScienceAndNature, ncSports, ncTechnology);
  39.   TJDNewsCategories = set of TJDNewsCategory;
  40.  
  41.   TJDNewsLanguage = (nlEnglish, nlGerman, nlFrench);
  42.   TJDNewsLanguages = set of TJDNewsLanguage;
  43.  
  44.   TJDNewsCountry = (ncAustralia, ncGermany, ncGreatBrittain, ncIndia, ncItaly, ncUnitedStates);
  45.   TJDNewsCountries = set of TJDNewsCountry;
  46.  
  47.   TNewsSortType = (nsTop, nsLatest, nsPopular);
  48.   TNewsSortTypes = set of TNewsSortType;
  49.  
  50.  
  51.   TJDNewsSource = class(TObject)
  52.   private
  53.     FObj: ISuperObject;
  54.     function GetCategory: TJDNewsCategory;
  55.     function GetCountry: TJDNewsCountry;
  56.     function GetDescription: String;
  57.     function GetID: String;
  58.     function GetLanguage: TJDNewsLanguage;
  59.     function GetName: String;
  60.     function GetUrl: String;
  61.   public
  62.     constructor Create(AObj: ISuperObject);
  63.     destructor Destroy; override;
  64.     property ID: String read GetID;
  65.     property Name: String read GetName;
  66.     property Description: String read GetDescription;
  67.     property Url: String read GetUrl;
  68.     property Category: TJDNewsCategory read GetCategory;
  69.     property Language: TJDNewsLanguage read GetLanguage;
  70.     property Country: TJDNewsCountry read GetCountry;
  71.   end;
  72.  
  73.   TJDNewsStory = class(TObject)
  74.   private
  75.     FObj: ISuperObject;
  76.     function GetAuthor: String;
  77.     function GetDescription: String;
  78.     function GetPublishedAt: TDateTime;
  79.     function GetTitle: String;
  80.     function GetUrl: String;
  81.     function GetUrlToImage: String;
  82.   public
  83.     constructor Create(AObj: ISuperObject);
  84.     destructor Destroy; override;
  85.     property Author: String read GetAuthor;
  86.     property Title: String read GetTitle;
  87.     property Description: String read GetDescription;
  88.     property Url: String read GetUrl;
  89.     property UrlToImage: String read GetUrlToImage;
  90.     property PublishedAt: TDateTime read GetPublishedAt;
  91.   end;
  92.  
  93.   TJDNews = class(TObject)
  94.   private
  95.     FCli: TIdHTTP;
  96.     FApiKey: String;
  97.     FSources: TObjectList<TJDNewsSource>;
  98.     FStories: TObjectList<TJDNewsStory>;
  99.     procedure SetApiKey(const Value: String);
  100.     function GetStory(const Index: Integer): TJDNewsStory;
  101.     function GetSource(const Index: Integer): TJDNewsSource;
  102.   public
  103.     constructor Create;
  104.     destructor Destroy; override;
  105.     property ApiKey: String read FApiKey write SetApiKey;
  106.     procedure LoadSources;
  107.     procedure LoadNews(const ASource: String);
  108.     function StoryCount: Integer;
  109.     property Stories[const Index: Integer]: TJDNewsStory read GetStory; default;
  110.     function SourceCount: Integer;
  111.     property Sources[const Index: Integer]: TJDNewsSource read GetSource;
  112.   end;
  113.  
  114.  
  115. implementation
  116.  
  117. uses
  118.   System.DateUtils;
  119.  
  120.  
  121. function StrToNewsCategory(const Value: String): TJDNewsCategory;
  122.   procedure Chk(const S: String; const V: TJDNewsCategory);
  123.   begin
  124.     if SameText(S, Value) then
  125.       Result:= V;
  126.   end;
  127. begin
  128.   Result:= TJDNewsCategory.ncGeneral;
  129.   Chk('business', TJDNewsCategory.ncBusiness);
  130.   Chk('entertainment', TJDNewsCategory.ncEntertainment);
  131.   Chk('gaming', TJDNewsCategory.ncGaming);
  132.   Chk('general', TJDNewsCategory.ncGeneral);
  133.   Chk('music', TJDNewsCategory.ncMusic);
  134.   Chk('politics', TJDNewsCategory.ncPolitics);
  135.   Chk('science-and-nature', TJDNewsCategory.ncScienceAndNature);
  136.   Chk('sport', TJDNewsCategory.ncSports);
  137.   Chk('technology', TJDNewsCategory.ncTechnology);
  138. end;
  139.  
  140. function NewsCategoryToStr(const Value: TJDNewsCategory): String;
  141. begin
  142.   Result:= 'general';
  143.   case Value of
  144.     ncBusiness:         Result:= 'business';
  145.     ncEntertainment:    Result:= 'entertainment';
  146.     ncGaming:           Result:= 'gaming';
  147.     ncGeneral:          Result:= 'general';
  148.     ncMusic:            Result:= 'music';
  149.     ncPolitics:         Result:= 'politics';
  150.     ncScienceAndNature: Result:= 'science-and-nature';
  151.     ncSports:           Result:= 'sport';
  152.     ncTechnology:       Result:= 'technology';
  153.   end;
  154. end;
  155.  
  156. function StrToNewsCountry(const Value: String): TJDNewsCountry;
  157.   procedure Chk(const S: String; const V: TJDNewsCountry);
  158.   begin
  159.     if SameText(S, Value) then
  160.       Result:= V;
  161.   end;
  162. begin
  163.   Result:= TJDNewsCountry.ncUnitedStates;
  164.   Chk('au', TJDNewsCountry.ncAustralia);
  165.   Chk('de', TJDNewsCountry.ncGermany);
  166.   Chk('gb', TJDNewsCountry.ncGreatBrittain);
  167.   Chk('in', TJDNewsCountry.ncIndia);
  168.   Chk('it', TJDNewsCountry.ncItaly);
  169.   Chk('us', TJDNewsCountry.ncUnitedStates);
  170. end;
  171.  
  172. function NewsCountryToStr(const Value: TJDNewsCountry): String;
  173. begin
  174.   Result:= 'us';
  175.   case Value of
  176.     ncAustralia:      Result:= 'au';
  177.     ncGermany:        Result:= 'de';
  178.     ncGreatBrittain:  Result:= 'gb';
  179.     ncIndia:          Result:= 'in';
  180.     ncItaly:          Result:= 'it';
  181.     ncUnitedStates:   Result:= 'us';
  182.   end;
  183. end;
  184.  
  185. function StrToNewsLanguage(const Value: String): TJDNewsLanguage;
  186.   procedure Chk(const S: String; const V: TJDNewsLanguage);
  187.   begin
  188.     if SameText(S, Value) then
  189.       Result:= V;
  190.   end;
  191. begin
  192.   Result:= TJDNewsLanguage.nlEnglish;
  193.   Chk('en', TJDNewsLanguage.nlEnglish);
  194.   Chk('de', TJDNewsLanguage.nlGerman);
  195.   Chk('fr', TJDNewsLanguage.nlFrench);
  196. end;
  197.  
  198. function NewsLanguageToStr(const Value: TJDNewsLanguage): String;
  199. begin
  200.   Result:= 'en';
  201.   case Value of
  202.     nlEnglish:  Result:= 'en';
  203.     nlGerman:   Result:= 'de';
  204.     nlFrench:   Result:= 'fr';
  205.   end;
  206. end;
  207.  
  208. { TJDNewsStory }
  209.  
  210. constructor TJDNewsStory.Create(AObj: ISuperObject);
  211. begin
  212.   FObj:= AObj;
  213.   if FObj <> nil then
  214.     FObj._AddRef;
  215. end;
  216.  
  217. destructor TJDNewsStory.Destroy;
  218. begin
  219.   if FObj <> nil then
  220.     FObj._Release;
  221.   inherited;
  222. end;
  223.  
  224. function TJDNewsStory.GetAuthor: String;
  225. begin
  226.   Result:= FObj.S['author'];
  227. end;
  228.  
  229. function TJDNewsStory.GetDescription: String;
  230. begin
  231.   Result:= FObj.S['description'];
  232. end;
  233.  
  234. function TJDNewsStory.GetPublishedAt: TDateTime;
  235. var
  236.   T: String;
  237. begin
  238.   try
  239.     T:= FObj.S['publishedAt'];
  240.     Result:= System.DateUtils.ISO8601ToDate(T, True);
  241.   except
  242.     on E: Exception do begin
  243.       Result:= 0;
  244.     end;
  245.   end;
  246. end;
  247.  
  248. function TJDNewsStory.GetTitle: String;
  249. begin
  250.   Result:= FObj.S['title'];
  251. end;
  252.  
  253. function TJDNewsStory.GetUrl: String;
  254. begin
  255.   Result:= FObj.S['url'];
  256. end;
  257.  
  258. function TJDNewsStory.GetUrlToImage: String;
  259. begin
  260.   Result:= FObj.S['urlToImage'];
  261. end;
  262.  
  263. { TJDNewsSource }
  264.  
  265. constructor TJDNewsSource.Create(AObj: ISuperObject);
  266. begin
  267.   FObj:= AObj;
  268.   if FObj <> nil then
  269.     FObj._AddRef;
  270. end;
  271.  
  272. destructor TJDNewsSource.Destroy;
  273. begin
  274.   if FObj <> nil then
  275.     FObj._Release;
  276.   inherited;
  277. end;
  278.  
  279. function TJDNewsSource.GetCategory: TJDNewsCategory;
  280. begin
  281.   Result:= StrToNewsCategory(FObj.S['category']);
  282. end;
  283.  
  284. function TJDNewsSource.GetCountry: TJDNewsCountry;
  285. begin
  286.   Result:= StrToNewsCountry(FObj.S['country']);
  287. end;
  288.  
  289. function TJDNewsSource.GetDescription: String;
  290. begin
  291.   Result:= FObj.S['description'];
  292. end;
  293.  
  294. function TJDNewsSource.GetID: String;
  295. begin
  296.   Result:= FObj.S['id'];
  297. end;
  298.  
  299. function TJDNewsSource.GetLanguage: TJDNewsLanguage;
  300. begin
  301.   Result:= StrToNewsLanguage(FObj.S['language']);
  302. end;
  303.  
  304. function TJDNewsSource.GetName: String;
  305. begin
  306.   Result:= FObj.S['name'];
  307. end;
  308.  
  309. function TJDNewsSource.GetUrl: String;
  310. begin
  311.   Result:= FObj.S['url'];
  312. end;
  313.  
  314. { TJDNews }
  315.  
  316. constructor TJDNews.Create;
  317. begin
  318.   FCli:= TIdHTTP.Create(nil);
  319.   FCli.HandleRedirects:= True;
  320.   FStories:= TObjectList<TJDNewsStory>.Create(True);
  321.   FSources:= TObjectList<TJDNewsSource>.Create(True);
  322. end;
  323.  
  324. destructor TJDNews.Destroy;
  325. begin
  326.   FSources.Clear;
  327.   FStories.Clear;
  328.   FreeAndNil(FSources);
  329.   FreeAndNil(FStories);
  330.   FreeAndNil(FCli);
  331.   inherited;
  332. end;
  333.  
  334. function TJDNews.GetSource(const Index: Integer): TJDNewsSource;
  335. begin
  336.   Result:= FSources[Index];
  337. end;
  338.  
  339. function TJDNews.GetStory(const Index: Integer): TJDNewsStory;
  340. begin
  341.   Result:= FStories[Index];
  342. end;
  343.  
  344. procedure TJDNews.SetApiKey(const Value: String);
  345. begin
  346.   FApiKey := Value;
  347. end;
  348.  
  349. function TJDNews.StoryCount: Integer;
  350. begin
  351.   Result:= FStories.Count;
  352. end;
  353.  
  354. function TJDNews.SourceCount: Integer;
  355. begin
  356.   Result:= FSources.Count;
  357. end;
  358.  
  359. procedure TJDNews.LoadSources;
  360. var
  361.   Url: String;
  362.   Res: String;
  363.   Obj: ISuperObject;
  364.   Arr: ISuperArray;
  365.   X: Integer;
  366.   S: TJDNewsSource;
  367. begin
  368.   FSources.Clear;
  369.   Url:= 'https://newsapi.org/v1/sources?language=en'; //TODO
  370.   try
  371.     Res:= FCli.Get(Url);
  372.     Obj:= SO(Res);
  373.     Arr:= Obj.A['sources'];
  374.     for X := 0 to Arr.Length-1 do begin
  375.       S:= TJDNewsSource.Create(Arr.O[X]);
  376.       FSources.Add(S);
  377.     end;
  378.   except
  379.     on E: Exception do begin
  380.       //TODO
  381.     end;
  382.   end;
  383. end;
  384.  
  385. procedure TJDNews.LoadNews(const ASource: String);
  386. var
  387.   Url: String;
  388.   Res: String;
  389.   Obj: ISuperObject;
  390.   Arr: ISuperArray;
  391.   X: Integer;
  392.   S: TJDNewsStory;
  393. begin
  394.   FStories.Clear;
  395.   Url:= 'https://newsapi.org/v1/articles?source='+ASource+'&sortBy=top&apiKey='+FApiKey;
  396.   try
  397.     Res:= FCli.Get(Url);
  398.     Obj:= SO(Res);
  399.     Arr:= Obj.A['articles'];
  400.     for X := 0 to Arr.Length-1 do begin
  401.       S:= TJDNewsStory.Create(Arr.O[X]);
  402.       FStories.Add(S);
  403.     end;
  404.   except
  405.     on E: Exception do begin
  406.       //TODO
  407.     end;
  408.   end;
  409. end;
  410.  
  411. end.
Advertisement
Add Comment
Please, Sign In to add comment