forked from mifi/instauto
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
122 lines (101 loc) · 3.08 KB
/
db.js
File metadata and controls
122 lines (101 loc) · 3.08 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
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
'use strict';
const fs = require('fs-extra');
const keyBy = require('lodash/keyBy');
module.exports = async ({
followedDbPath,
unfollowedDbPath,
likedPhotosDbPath,
logger = console,
}) => {
let prevFollowedUsers = {};
let prevUnfollowedUsers = {};
let prevLikedPhotos = [];
async function trySaveDb() {
try {
await fs.writeFile(followedDbPath, JSON.stringify(Object.values(prevFollowedUsers)));
await fs.writeFile(unfollowedDbPath, JSON.stringify(Object.values(prevUnfollowedUsers)));
await fs.writeFile(likedPhotosDbPath, JSON.stringify(prevLikedPhotos));
} catch (err) {
logger.error('Failed to save database');
}
}
async function tryLoadDb() {
try {
prevFollowedUsers = keyBy(JSON.parse(await fs.readFile(followedDbPath)), 'username');
} catch (err) {
logger.warn('No followed database found');
}
try {
prevUnfollowedUsers = keyBy(JSON.parse(await fs.readFile(unfollowedDbPath)), 'username');
} catch (err) {
logger.warn('No unfollowed database found');
}
try {
prevLikedPhotos = JSON.parse(await fs.readFile(likedPhotosDbPath));
} catch (err) {
logger.warn('No likes database found');
}
}
function getPrevLikedPhotos() {
return prevLikedPhotos;
}
function getTotalLikedPhotos() {
return getPrevLikedPhotos().length; // TODO performance
}
function getLikedPhotosLastTimeUnit(timeUnit) {
const now = new Date().getTime();
return getPrevLikedPhotos().filter(u => now - u.time < timeUnit);
}
async function addLikedPhoto({ username, href, time }) {
prevLikedPhotos.push({ username, href, time });
await trySaveDb();
}
function getPrevFollowedUsers() {
return Object.values(prevFollowedUsers);
}
function getTotalFollowedUsers() {
return getPrevFollowedUsers().length; // TODO performance
}
function getFollowedLastTimeUnit(timeUnit) {
const now = new Date().getTime();
return getPrevFollowedUsers().filter(u => now - u.time < timeUnit);
}
function getPrevFollowedUser(username) {
return prevFollowedUsers[username];
}
async function addPrevFollowedUser(user) {
prevFollowedUsers[user.username] = user;
await trySaveDb();
}
function getPrevUnfollowedUsers() {
return Object.values(prevUnfollowedUsers);
}
function getTotalUnfollowedUsers() {
return getPrevUnfollowedUsers().length; // TODO performance
}
function getUnfollowedLastTimeUnit(timeUnit) {
const now = new Date().getTime();
return getPrevUnfollowedUsers().filter(u => now - u.time < timeUnit);
}
async function addPrevUnfollowedUser(user) {
prevUnfollowedUsers[user.username] = user;
await trySaveDb();
}
await tryLoadDb();
return {
save: trySaveDb,
addPrevFollowedUser,
getPrevFollowedUser,
addPrevUnfollowedUser,
getPrevFollowedUsers,
getFollowedLastTimeUnit,
getPrevUnfollowedUsers,
getUnfollowedLastTimeUnit,
getPrevLikedPhotos,
getLikedPhotosLastTimeUnit,
addLikedPhoto,
getTotalFollowedUsers,
getTotalUnfollowedUsers,
getTotalLikedPhotos,
};
};