-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserializer.js
More file actions
76 lines (55 loc) · 2.8 KB
/
serializer.js
File metadata and controls
76 lines (55 loc) · 2.8 KB
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
var fs = require('fs');
/*
Checking for crossposts wasn't enough because that reddit
endpoint isn't so "random" and the same URL may get fetched
after an hour or even less! So I decided to add every used URL
to a hash table. Initially, I used javascript's Map but then
discovered that JSON.stringify doesn't work on Maps so I ended
up with using a normal object and wrote the hash table functions
myself. The hash table exports and imports all of its data from
a json file so data isn't lost when I close the app.
*/
if(fs.existsSync('./data/data.json')==false)
fs.appendFileSync('./data/data.json','{}');
var map = JSON.parse(fs.readFileSync('./data/data.json', 'utf8'));
module.exports={
//example of how a url looks like: 'https://i.redditmedia.com/QOOy-WZ3OO0yWJaFkjmKJ0AzT2F4NGlkLnLEzMQHqfY.jpg?s=454a0b04a2b06661680aa05e12f9c40b'
//UPDATED URL: https://i.redd.it/gakoldunusr11.jpg
//I don't really care about the url so I will just put 0/1 in the value field of the map. Also, the whole links to every image are alreaddy logged in separate files
//takes the string after the last slash and before the first and only query in the URL and returns it as the hash key
generateKey : function(url){
return url.slice(url.lastIndexOf('/')+1 , url.lastIndexOf('.'));
},
//checks if the URL already exists in the hash table to prevent duplication
exists : function(url){
if(map[this.generateKey(url)])
return true;
return false;
},
//after updating the image URL format I still have to check for the existing old URL format in the table
existsOld : function(url){
if(map[url.slice(url.lastIndexOf('/')+1 , url.lastIndexOf('?'))])
return true;
return false;
},
/*
This function is a piece of hack. I figured that after some time the hash table
will contain so much data it would be stupid to overwrite the whole json file
every time I fetch a URL so I thought that instead I should append to the file.
To do that I had to first delete the closing brace (}) and then add a comma (,)
before appending the key and value of the URL, I searched for too long for a function
that does just that and couldn't find a solution that doesn't involve importing
a whole other npm package. However, I figured out that with FileSystem's truncate
function I could pass a smaller file size so it would shrink the json file by deleting
by a certain amount of bytes from the end and that's exactly what I want! So I took the
file's size and subtracted from it 1 byte which is the space taken by a single character (})
*/
add : function(url){
if(this.exists(url))
return;
map[this.generateKey(url)]=1;
let stat = fs.statSync('./data/data.json');
fs.truncateSync('./data/data.json',stat.size-1);
fs.appendFileSync('./data/data.json',`${stat.size==2 ? '' : ','}"${this.generateKey(url)}":1}`);
}
}