forked from mbesemann/Trendinator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
newsApi.js
139 lines (126 loc) · 5.08 KB
/
newsApi.js
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const apiKey = "09a51a1a7002430abd69b82f52b2eaf3";
const baseUrl = "https://newsapi.org/v2/top-headlines";
const proxy = "https://highlycaffeinated.ca:5001/"
var DateTime = luxon.DateTime;
var sidenav;
$(document).ready(function() {
sidenav = M.Sidenav.getInstance($(".sidenav"));
loadBookmarks();
getNews();
})
// Category options are business, entertainment, general, health, science, sports, technology
function getNews(category='', topics=-1, country='ca') {
sidenav.close();
$(".news-articles-content").empty();
// Check for existing preferences
if(category == '') {
var currentCategory = localStorage.getItem("currentCategory");
if (currentCategory) {
category = currentCategory;
setCategory(`#${currentCategory}-btn`);
}
else {
setCategory(`#top-stories-btn`);
}
}
if(topics == -1) {
var numberOfTopics = localStorage.getItem("numberOfTopics");
if (numberOfTopics) {
topics = numberOfTopics;
//setDropdownText(numberOfTopics);
}
else
topics = 10
}
savedCountry = localStorage.getItem("currentCountry");
if(savedCountry)
country = convertCountry(savedCountry);
localStorage.setItem("currentCategory", category);
//localStorage.setItem("numberOfTopics", topics);
$.ajax({
url: `${proxy}${baseUrl}?language=en&apiKey=${apiKey}&country=${country}&category=${category.replace("top-stories", "")}&pageSize=${topics}`,
method: 'GET'
}).then(function(response) {
//console.log(response);
response.articles.forEach(story => {
var articleDiv = $("<div>").addClass("card blue-grey darken-1");
var cardContent = $("<div>").addClass("card-content white-text");
var favicon = $("<img>").prop("src", "https://www.google.com/s2/favicons?domain=" + story.url);
var articleUrl = $("<span>").addClass("card-title").append(favicon, " ", ($("<a>").prop("href", story.url).text(story.title).prop("target", "_blank")));
var pinImage = $("<img>").prop("src", "https://img.icons8.com/color/48/000000/pin.png").prop("width", 20).prop("height", 20);
var saveBtn = $("<a>").addClass("saveBtn").append(pinImage).on("click", function() {
saveBookmark(story.title, story.url);
sidenav.open();
});
var description = $("<p>").text(story.description);
cardContent.append(articleUrl, description);
var cardAction = $("<div>").addClass("card-action white-text");
var storyDate = DateTime.fromISO(story.publishedAt);
var datePublished = $("<span>").text(storyDate.toLocaleString());
var source = $("<span>").text(story.source.name);
cardAction.append(source, " ", datePublished, " ", saveBtn);
articleDiv.append(cardContent, cardAction);
$(".news-articles-content").append(articleDiv);
});
});
}
$("#top-stories-btn").on("click", function(){getNews("top-stories");});
$("#top-stories-btn-side").on("click", function(){setCategory("#top-stories-btn"); getNews("top-stories");});
$("#sports-btn").on("click", function(){getNews("sports");});
$("#sports-btn-side").on("click", function(){setCategory("#sports-btn"); getNews("sports");});
$("#entertainment-btn").on("click", function(){getNews("entertainment");});
$("#entertainment-btn-side").on("click", function(){setCategory("#entertainment-btn"); getNews("entertainment");});
$("#technology-btn").on("click", function(){getNews("technology");});
$("#technology-btn-side").on("click", function(){setCategory("#technology-btn"); getNews("technology");});
$('.category').on("click", function() {
$('.category').removeClass("teal");
$('.category').addClass("coral");
$(this).addClass("teal");
});
function setCategory(id) {
$('.category').removeClass("teal");
$('.category').addClass("coral");
$(id).addClass("teal");
}
$(".ddl-item").on("click", function() {
getNews(localStorage.getItem("currentCategory"), $(this).text());
});
function convertCountry(country) {
var abbrev = '';
switch(country) {
case 'Australia':
abbrev = 'au';
break;
case 'Canada':
abbrev = 'ca';
break;
case 'United Kingdom':
abbrev = 'gb';
break;
case 'United States':
abbrev = 'us';
break;
case 'Japan':
abbrev = 'jp';
break;
case 'India':
abbrev = 'in';
break;
case 'Brazil':
abbrev = 'br';
break;
case 'Turkey':
abbrev = 'tr';
break;
default:
if(country.includes("Current")) {
var ipcountry = country.split("(")[1].replace(")","");
abbrev = convertCountry(ipcountry);
}
}
return abbrev;
}
$(".country-item").on("click", function() {
var country = $(this).first().text();
getNews(undefined,undefined,convertCountry(country));
})