-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
140 lines (129 loc) · 4.57 KB
/
index.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
140
if (process.env["APIURL"] == undefined) {
require("dotenv").config({path:".env"})
}
const express = require("express")
const app = express()
app.use(require("cookie-parser")())
app.get("/", (req, res) => {
var date = new Date()
res.cookie("authused", "0", {maxAge: date.getTime()+315532800000})
res.cookie("authkey", "", {maxAge: date.getTime()+315532800000})
res.sendFile(__dirname + "/index.html")
})
app.get("/search", (req,res) => {
let main = ""
const start = `
<!DOCTYPE html>
<html>
<head>
<title>Search</title>
</head>
<body>
<header>
<a href="/"><img src="/logo"></a><br>
</header>
<h1>Search</h1>
<div>
`
const end = `
</div><br><br>
<footer>Made by Stoppedwumm</footer>
</body>
<script>
window.va = window.va || function () { (window.vaq = window.vaq || []).push(arguments); };
</script>
<script defer src="/_vercel/insights/script.js"></script>
</html>`
main = main + start
if (req.cookies["authused"] == "0" || req.cookies["authused"] == undefined) {
fetch(process.env["APIURL"] + "?apikey=" + process.env["APIKEY"] + "&q=" + encodeURIComponent(req.query["term"]) + "&language=en").then((value) => {
value.json().then((jsonvalue) => {
console.log(jsonvalue)
main = main + `
<p>${jsonvalue.totalResults} Results found</p>
`
jsonvalue.results.forEach((ivalue) => {
let loadedValue = `
<div>
<h2>${ivalue.title}</h2>
<img src="${ivalue.image_url}" width="500"><br>
<p>${ivalue.description}</p>
<a href="${ivalue.link}" target="_blank" rel="noopener noreferrer">Zum Artikel</a><br>
<a href="${ivalue.source_url}" target="_blank" rel="noopener noreferrer">Fetched from ${ivalue.source_id}</a><br>
</div>
`
main = main + loadedValue
})
main = main + end
res.send(main)
})
})
} else {
fetch(process.env["APIURL"] + "?apikey=" + req.cookies.authkey + "&q=" + encodeURIComponent(req.query["term"]) + "&language=en").then((value) => {
value.json().then((jsonvalue) => {
console.log(jsonvalue)
main = main + `
<p>${jsonvalue.totalResults} Results found</p>
`
jsonvalue.results.forEach((ivalue) => {
let loadedValue = `
<div>
<h2>${ivalue.title}</h2>
<img src="${ivalue.image_url}" width="500"><br>
<p>${ivalue.description}</p>
<a href="${ivalue.link}" target="_blank" rel="noopener noreferrer">Zum Artikel</a><br>
<a href="${ivalue.source_url}" target="_blank" rel="noopener noreferrer">Fetched from ${ivalue.source_id}</a><br>
</div>
`
main = main + loadedValue
})
main = main + end
res.send(main)
}).catch((error) => {
console.log("ERROR:", error)
res.status(400).send(`
ERROR: Api Key not avaible<br>
<a href="/api/reset">Reset to normal values</a>
`)
})
}).catch((error) => {
console.log("ERROR:", error)
})
}
})
app.get("/set", (req,res) => {
res.send(`
<!DOCTYPE html>
<html>
<body>
<h1>Set API Key</h1>
(You need a newsdata.io API Key)
<form method="get" action="/api/set">
<input type="text" name="key" placeholder="API Key">
<input type="submit" value="Set Key">
</form>
</body>
<script>
window.va = window.va || function () { (window.vaq = window.vaq || []).push(arguments); };
</script>
<script defer src="/_vercel/insights/script.js"></script>
</html>
`)
})
app.get("/api/set", (req, res) => {
const apikey = req.query["key"]
res.cookie("authused", "1")
res.cookie("authkey", apikey)
res.redirect("about:blank")
})
app.get("/logo", (req, res) => {
res.type("png")
res.sendFile(__dirname + "/assets/logo.png")
})
app.get("/api/reset", (req, res) => {
var date = new Date()
res.cookie("authused", "0", {maxAge: date.getTime()+315532800000})
res.cookie("authkey", "", {maxAge: date.getTime()+315532800000})
res.redirect("/")
})
app.listen(3000)