File: /var/www/vhost/disk-apps/pwa.sports-crowd.com/src/app/pages/news/news.page.ts
import { Component, OnInit } from "@angular/core";
import { UserService } from "../../services/user.service";
import { OrderService } from "../../services/order.service";
import { HomeService } from "../../services/home.service";
import { CartService } from "../../services/cart.service";
import { UtilsService } from "../../services/utils.service";
import { ParameterService } from "../../services/parameter.service";
import { HttpClient } from "@angular/common/http";
@Component({
selector: "app-news",
templateUrl: "./news.page.html",
styleUrls: ["./news.page.scss"],
})
export class NewsPage implements OnInit {
news: any;
arr = [];
showDetails: boolean;
targetUrl: string = "https://dimoficial.com/feed/eclectica104_rss";
constructor(
public homeProvider: HomeService,
public userProvider: UserService,
public cartProvider: CartService,
public orderProvider: OrderService,
public utilsService: UtilsService,
public http: HttpClient,
public parameterService: ParameterService
) { }
ngOnInit() {
this.loadXML();
}
loadXML() {
this.http
.get(this.targetUrl, {
responseType: "text",
})
.subscribe((data) => {
let xmlDOM = new DOMParser().parseFromString(data, "text/xml");
this.news = this.xmlToJson(xmlDOM);
for (let k in this.news.rss.channel.item) {
let item = this.news.rss.channel.item[k];
let dt = item.pubDate;
let publicationDate = dt.substring(0, 4) + '/' + dt.substring(4, 6) + '/' + dt.substring(6, 8) +
' ' + dt.substring(8, 10) + ':' + dt.substring(10, 12) + ':' + dt.substring(12, 14);
let weeks = this.utilsService.getWeeksDiff(new Date(publicationDate), new Date());
this.arr.push({
imgUrl: item.imgUrl,
category: item.category,
title: item.title,
description: item.description.substring(0, item.description.indexOf("<p>La entrada <a rel=")),
publicationDate,
showDetails: false,
icon: 'add-circle',
new: weeks == 0
});
}
});
}
xmlToJson(xml) {
// Create the return object
let obj = {};
if (xml.nodeType == 1) {
// element
// do attributes
if (xml.attributes.length > 0) {
obj["@attributes"] = {};
for (let j = 0; j < xml.attributes.length; j++) {
let attribute = xml.attributes.item(j);
obj["@attributes"][attribute.nodeName] = attribute.nodeValue;
}
}
} else if (xml.nodeType == 3) {
// text
obj = xml.nodeValue;
}
// do children
// If all text nodes inside, get concatenated text from them.
let textNodes = [].slice.call(xml.childNodes).filter(function (node) {
return node.nodeType === 3;
});
if (xml.hasChildNodes() && xml.childNodes.length === textNodes.length) {
obj = [].slice.call(xml.childNodes).reduce(function (text, node) {
return text + node.nodeValue;
}, "");
} else if (xml.hasChildNodes()) {
for (let i = 0; i < xml.childNodes.length; i++) {
let item = xml.childNodes.item(i);
let nodeName = item.nodeName;
if (typeof obj[nodeName] == "undefined") {
obj[nodeName] = this.xmlToJson(item);
} else {
if (typeof obj[nodeName].push == "undefined") {
let old = obj[nodeName];
obj[nodeName] = [];
obj[nodeName].push(old);
}
obj[nodeName].push(this.xmlToJson(item));
}
}
}
return obj;
}
toggleDetails(arr) {
if (arr.showDetails) {
arr.showDetails = false;
arr.icon = 'add-circle';
} else {
arr.showDetails = true;
arr.icon = 'close-circle';
}
}
}