-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
72 lines (66 loc) · 3.18 KB
/
main.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
$(document).ready(function() {
// variables
// var api = "https://newsapi.org/v1/articles?source=techcrunch&apiKey=d647eb7abbc7414592db03051c5b2b5e";
var leftArr = ["independent", "the-guardian-uk", "the-guardian-au", "the-huffington-post"];
var cenLeftArr = ["associated-press", "bbc-news", "bloomberg", "business-insider", "buzzfeed", "newsweek", "the-new-york-times", "the-washington-post", "time", "mirror"];
var cenRightArr = ["cnbc", "cnn", "financial-times", "fortune", "reuters", "the-economist", "the-times-of-india", "the-wall-street-journal"];
var rightArr = ["daily-mail", "metro", "the-telegraph"];
// variable for my 'hidden API'
var myKey = config.MY_KEY;
// Hover description - to appear and disappear on title hover
$("#title").hover(function(){
$("ul").append("<li id='test'>Social media is incresingly used as a news source benefiting 'click bait headlines' over quality journalism. At <span style ='color:white; font-size:0.9em;font-family:Lalezar'>News Balance</span> you can view the top news stories from a range of papers and a variety of opinions.</li>");},
function(){$("#test").remove()}
);
// FUNCTIONS
// Functions for random array selection
// Return a random integer in the range 0 through n - 1
function randomInt(n) {
return Math.floor(Math.random() * n);
}
// Return a random element from an array
function randomElement(array) {
return array[randomInt(array.length)];
}
// function to create API
function api(array) {
return "https://newsapi.org/v1/articles?source=" + randomElement(array) + "&apiKey="+myKey;
}
// name formatting - capitalises all acroyms
function removeFormatting(name) {
return name.replace(/-/gi, " ").replace(/\b\w/g, l => l.toUpperCase()).replace(/uk/gi, "UK").replace(/au/gi, "AU").replace(/cnn/gi, "CNN").replace(/cnbc/gi, "CNBC").replace(/bbc/gi, "BBC");
}
// some links from api were followed by a backslash. This removes them if they are present.
function removeSlash(url) {
if (url[url.length - 1] == "/") {
url = url.slice(0, -1);
}
return url;
}
// function for creating articles
function createArticle(data, paperID, articleID) {
var paperName = removeFormatting(data.source);
$(paperID).text(paperName);
$(articleID).html()
for (i = 0; i < 5; i++) {
var articleUrl = removeSlash(data.articles[i].url)
$(articleID).append("<article><a href =" + articleUrl + "><h4>" + data.articles[i].title + "</h4>" + "<p>" + data.articles[i].description + "</p></a></article>")
}
}
// left data
$.getJSON(api(leftArr), function(data) {
createArticle(data, "#leftPaper", "#leftArt");
});
// Center left data
$.getJSON(api(cenLeftArr), function(data) {
createArticle(data, "#cenLeftPaper", "#cenLeftArt");
});
// Center Right data
$.getJSON(api(cenRightArr), function(data) {
createArticle(data, "#cenRightPaper", "#cenRightArt");
});
// Right data
$.getJSON(api(rightArr), function(data) {
createArticle(data, "#rightPaper", "#rightArt");
});
})