-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathnyt.html
85 lines (85 loc) · 2.57 KB
/
nyt.html
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
<!doctype html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, minimum-scale=0.25">
<title>NY Times</title>
<style>
body {
padding: 1em;
}
a:link {
color: #000000;
text-decoration: none;
}
a:visited {
color: #828282;
text-decoration: none;
}
img {
width: 100%;
}
img, .title {
font-weight: bold;
padding: 1em 0;
}
#more {
width: 100%;
background-color: white;
}
.item {
border-bottom: 1px solid silver;
margin-bottom: 1em;
}
.item:last-of-type {
border-bottom: none;
}
@media (prefers-color-scheme: dark) {
body {
background-color: #121212;
color: #888;
}
}
</style>
</head>
<body>
<p>NY Times</p>
<b>Sections</b>
<div id=links></div>
<div id=info></div>
<p>
<div id=results></div>
<div id=info2></div>
<button id=more>More</button>
<script>
// see https://developer.nytimes.com to register, get api key, read docs, etc.
const apiKey = localStorage.apiKey || (localStorage.apiKey = prompt('api key') || '');
const sections = ['home', 'arts', 'automobiles', 'books', 'business', 'food', 'health', 'insider', 'magazine', 'movies',
'nyregion', 'opinion', 'politics', 'realestate', 'science', 'sundayreview', 'technology', 'theater', 't-magazine', 'travel',
'upshot', 'us', 'world'];
// misc sections: ['fashion', 'obituaries', 'sports']
var s = 0;
onload = more.onclick = async function () {
if (!apiKey) {
info.innerHTML = 'see <a href="https://developer.nytimes.com">developer.nytimes.com</a> to register, get api key, read docs, etc.';
return;
}
info.innerHTML = info2.innerHTML = 'Loading...';
do {
const section = sections[s];
const r = await fetch(`https://api.nytimes.com/svc/topstories/v2/${section}.json?api-key=${apiKey}`);
const rs = (await r.json()).results;
links.innerHTML += `<div><a href='#${section}'>${section} - ${rs.length}</a></div>`;
results.innerHTML += `<a id='${section}'>${section} - ${rs.length}</a>`;
rs.forEach(r => results.innerHTML += `<p class=item><a href='${r.url}' target='_blank' rel='noopener'><div class=title>${r.title}</div>${r.abstract}<br>${r.multimedia ? `<img src='${r.multimedia[1].url}'>` : ''}</a></p>`);
s++;
if (s == sections.length) {
more.style.display = 'none';
info.innerHTML = info2.innerHTML = '';
return;
}
} while (s % 10); // "there are two rate limits per API: 4,000 requests per day and 10 requests per minute" https://developer.nytimes.com/faq#a11
info.innerHTML = info2.innerHTML = '...';
};
</script>
</body>
</html>