-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
140 lines (125 loc) · 4.64 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
const http = require('http');
const https = require('https');
const fs = require('fs');
const url = require('url');
const queryString = require('querystring');
const crypto = require("crypto");
require('dotenv').config();
const client_id = process.env.client_id;
const client_secret = process.env.client_secret;
const port = 3000;
const server = http.createServer();
let fox_Image = '';
server.on("request", handle_connection);
server.on("listening", listening_handler);
server.listen(port);
function listening_handler(){
console.log(`Now Listening on Port ${port}`);
}
function handle_connection(req, res){
console.log(`New Request for ${req.url} from ${req.socket.remoteAddress}`);
if(req.url === '/'){
const main = fs.createReadStream('html/main.html');
res.writeHead(200, {'Content-Type': 'text/html'});
main.pipe(res);
}
else if(req.url === '/favicon.ico'){
res.writeHead(200, {'Content-Type': 'image/x-icon'});
const favi = fs.createReadStream('images/favicon.ico');
favi.pipe(res);
}
else if(req.url === '/images/runningFoxGif.gif'){
res.writeHead(200, {'Content-Type': 'image/gif'});
const aGif = fs.createReadStream('images/runningFoxGif.gif');
aGif.pipe(res);
}
else if(req.url === '/images/back.jpg'){
res.writeHead(200, {'Content-Type': 'image/jpg'});
const pic = fs.createReadStream('images/back.jpg');
pic.pipe(res);
}
else if(req.url.startsWith("/submit")){
https.get("https://randomfox.ca/floof/" , (resp) =>{
let siteData = '';
resp.on('data', (chunk)=>{
siteData += chunk;
});
resp.on('end', () => {
fox_Image = JSON.parse(siteData).image;
console.log(fox_Image);
let newFileName = JSON.parse(siteData).link;
let parsedURL = url.parse(newFileName, true).query;
if(fs.existsSync(`/foxLinks/${parsedURL.i}`)){
console.log("We already used this image");
res.writeHead(302, {Location: "/"})
.end();
}
else{
fs.writeFile(`./foxLinks/${parsedURL.i}.txt`, "Got a new fox", ()=> console.log("We made a new file"));
}
const state = crypto.randomBytes(20).toString("hex");
redirect_To_Imgur(state, res, fox_Image);
});
resp.on("error", (err) => {
console.log("There was an error: " + err.message);
});
});
}
else if(req.url.startsWith("/redirection1")) {
console.log("First redirection");
//const {access_token, refresh_token, state} = url.parse(req.url, true).query;
const main2 = fs.createReadStream('html/main2.html');
res.writeHead(200, {'Content-Type': 'text/html'});
main2.pipe(res);
//console.log(access_token);
}
else if(req.url.startsWith("/redirection_part2")){
const {access_token, refresh_token, expires_in} = url.parse(req.url, true).query;
console.log("Token:" + access_token);
const tokens_request_time = new Date();
request_To_Upload(access_token, fox_Image, res);
}
else{
res.writeHead(404, {"Content-Type" : "text/plain"});
res.write("404 Not Found", () => res.end());
}
}
function redirect_To_Imgur(state, res, fox_Image){
const authorization_endpoint = "https://api.imgur.com/oauth2/authorize";
let uri = queryString.stringify({response_type: "token", client_id, state});
console.log(uri);
res.writeHead(302, {Location: `${authorization_endpoint}?${uri}`})
.end();
}
function request_To_Upload(access_token, fox_Image, res){
const img_endpoint = "https://api.imgur.com/3/upload";
console.log(fox_Image);
const url_type = "url";
const img_Title = "A random fox";
const img_Description = "This picture is from RandomFox API";
const post_data = queryString.stringify({image: fox_Image, type:url_type, title: img_Title, desciption: img_Description } );
const options = {
method: 'POST',
headers: {
Authorization: `Bearer ${access_token}`,
"Content-Type": 'application/x-www-form-urlencoded'
}
}
https.request(
img_endpoint,
options,
(img_stream) => process_stream(img_stream, receive_img_response, res)
).end(post_data);
}
function receive_img_response(body, res){
const results = JSON.parse(body);
console.log(results);
let username = results.data.account_url;
res.writeHead(302, {Location: `https://${username}.imgur.com/all`})
.end();
}
function process_stream (stream, callback , ...args){
let body = "";
stream.on("data", chunk => body += chunk);
stream.on("end", () => callback(body, ...args));
}