/src/app/services/knowledgeapi.service.ts

https://github.com/fossasia/susper.com · TypeScript · 80 lines · 68 code · 11 blank · 1 comment · 4 complexity · b8c88de936e9f44f305b8353bfa9f724 MD5 · raw file

  1. import { Injectable } from '@angular/core';
  2. import { Http, URLSearchParams, Jsonp, Response, Headers, RequestOptions } from '@angular/http';
  3. import 'rxjs/add/operator/map';
  4. import 'rxjs/add/operator/toPromise';
  5. import { Store } from '@ngrx/store';
  6. import * as fromRoot from '../reducers';
  7. import { Observable } from 'rxjs';
  8. import { url } from '../../assets/url_configuration';
  9. @Injectable()
  10. export class KnowledgeapiService {
  11. server = 'https://en.wikipedia.org';
  12. searchURL = this.server + '/w/api.php?';
  13. descriptionURL = 'https://www.wikidata.org/w/api.php?';
  14. homepage = 'http://' + url.susper.site;
  15. logo = '../images/susper.svg';
  16. imgUrl = 'https://api.duckduckgo.com/?';
  17. constructor(private http: Http,
  18. private jsonp: Jsonp,
  19. private store: Store<fromRoot.State>) {
  20. }
  21. getSearchResults(searchquery) {
  22. const params = new URLSearchParams();
  23. params.set('origin', '*');
  24. params.set('format', 'json');
  25. params.set('action', 'query');
  26. params.set('prop', 'extracts');
  27. params.set('exintro', '');
  28. params.set('explaintext', '');
  29. params.set('titles', searchquery);
  30. const headers = new Headers({ 'Accept': 'application/json' });
  31. const options = new RequestOptions({ headers: headers, search: params });
  32. return this.http
  33. .get(this.searchURL, options).map(res =>
  34. res.json()
  35. ).catch(this.handleError);
  36. }
  37. getImage(searchquery) {
  38. const params = new URLSearchParams();
  39. params.set('origin', '*');
  40. params.set('q', searchquery);
  41. params.set('format', 'json');
  42. params.set('pretty', '1');
  43. const headers = new Headers({ 'Accept': 'application/json' });
  44. const options = new RequestOptions({ headers: headers, search: params });
  45. return this.http.get(this.imgUrl, options).map(res =>
  46. res.json()
  47. ).catch(this.handleError);
  48. }
  49. getQueryDescription(searchquery) {
  50. const params = new URLSearchParams();
  51. params.set('origin', '*');
  52. params.set('action', 'wbsearchentities');
  53. params.set('search', searchquery);
  54. params.set('language', 'en');
  55. params.set('format', 'json');
  56. const headers = new Headers({ 'Accept': 'application/json' });
  57. const options = new RequestOptions({ headers: headers, search: params });
  58. return this.http.get(this.descriptionURL, options).map(res =>
  59. res.json()
  60. ).catch(this.handleError);
  61. }
  62. private handleError (error: any) {
  63. // In some advance version we can include a remote logging of errors
  64. const errMsg = (error.message) ? error.message :
  65. error.status ? `${error.status} - ${error.statusText}` : 'Server error';
  66. console.error(errMsg); // Right now we are logging to console itself
  67. return Observable.throw(errMsg);
  68. }
  69. }