-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrss-reader.service.ts
More file actions
75 lines (68 loc) · 2.4 KB
/
rss-reader.service.ts
File metadata and controls
75 lines (68 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { Injectable } from '@angular/core';
import * as encodeurl from 'encodeurl';
import { HttpClient } from '@angular/common/http';
import { Observable, from, throwError } from 'rxjs';
import { map, mergeMap, catchError } from 'rxjs/operators';
import { SettingsService } from '../services/settings.service';
import { Feed } from './feed';
@Injectable({
providedIn: 'root'
})
export class RssReaderService {
constructor(
private settings: SettingsService,
private http: HttpClient
) { if (this.settings.debug) console.log("RssReaderService") }
get debug(): boolean {
return this.settings.debug;
}
public getFeedContent(url?: string): Observable<Feed> {
if (this.settings.debug) console.log("getFeedContent", url);
if (!url) {
let urls = this.settings.feedUrls;
if (this.settings.debug) console.log("URL não informado, usando settings:", urls);
return from(urls)
.pipe(
mergeMap((url) => {
if (this.settings.debug) console.log("getFeedContent.mergeMap:", url);
if (url)
return this.getFeedContent(url);
})
);
}
if (this.settings.debug) {
console.log("encodeURL: ", url);
}
// console.log(encodeurl(url));
// url = encodeurl(url);
// if (this.settings.debug) {
// console.log("Url Encoded: ", url);
// }
if (this.settings.rssToJsonConverterType == "rss2json") {
return this.http.get<Feed>(this.settings.rssToJsonConverterBaseUrl + url)
.pipe(
map((res) => {
if (this.settings.debug) {
console.log("extractFeeds");
console.log(res.feed.url);
console.log(res.feed.title);
console.log(res.feed.description);
console.log(res);
}
return res;
}),
catchError((error: any) => {
// In a real world app, we might use a remote logging infrastructure
// We'd also dig deeper into the error to get a better message
if (this.settings.debug) console.error(error);
let errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
console.error(errMsg); // log to console instead
return throwError(errMsg);
})
);
} else {
throw new Error("Não implementado ainda");
}
}
}