-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.js
188 lines (156 loc) · 6.01 KB
/
main.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import ProxyManager from "./src/utils/proxy_manager.js";
import { VintedItem } from "./src/entities/vinted_item.js";
import { filterItemsByUrl } from "./src/services/url_service.js";
import { Preference, buildCategoryMapFromRoots } from "./src/database.js";
import client from "./src/client.js";
import ConfigurationManager from "./src/utils/config_manager.js";
import { postMessageToChannel, checkVintedChannelInactivity } from "./src/services/discord_service.js";
import { createVintedItemEmbed, createVintedItemActionRow } from "./src/bot/components/item_embed.js";
import { fetchCookie } from "./src/api/fetchCookie.js";
import { fetchCatalogInitializer } from "./src/api/fetchCatalogInitializers.js";
import crud from "./src/crud.js";
import Logger from "./src/utils/logger.js";
import CatalogService from "./src/services/catalog_service.js";
var cookie = null;
try {
await ProxyManager.init();
} catch (error) {
Logger.error(`Failed to initialize proxies: ${error.message}`);
Logger.info('Continuing without proxies...');
}
const algorithmSettings = ConfigurationManager.getAlgorithmSetting
CatalogService.initializeConcurrency(algorithmSettings.concurrent_requests);
const getCookie = async () => {
const c = await fetchCookie();
return c.cookie;
};
const refreshCookie = async () => {
let found = false;
while (!found) {
try {
const cookie = await getCookie();
if (cookie) {
found = true;
Logger.info('Fetched cookie from Vinted');
return cookie;
}
} catch (error) {
Logger.debug('Error fetching cookie');
await new Promise(resolve => setTimeout(resolve, 200));
}
setTimeout(() => {
Logger.debug('Retrying to fetch cookie');
}, 1000);
}
};
const discordConfig = ConfigurationManager.getDiscordConfig
const token = discordConfig.token;
Logger.info('Starting Vinted Bot');
Logger.info('Fetching cookie from Vinted');
cookie = await refreshCookie();
setInterval(async () => {
try {
cookie = await refreshCookie();
} catch (error) {
Logger.debug('Error refreshing cookie');
}
}, 60000); // 60 seconds
const getCatalogRoots = async (cookie) => {
let found = false;
while (!found) {
try {
const roots = await fetchCatalogInitializer( { cookie });
if (roots) {
buildCategoryMapFromRoots(roots);
found = true;
Logger.info('Fetched catalog roots from Vinted');
}
} catch (error) {
Logger.debug('Error fetching catalog roots');
console.error(error);
await new Promise(resolve => setTimeout(resolve, 200));
}
}
}
Logger.info('Fetching catalog roots from Vinted');
await getCatalogRoots(cookie);
const sendToChannel = async (item, user, vintedChannel) => {
// get the domain from the URL between vinted. and the next /
const domain = vintedChannel.url.match(/vinted\.(.*?)\//)[1];
const { embed, photosEmbeds } = await createVintedItemEmbed(item, domain);
const actionRow = await createVintedItemActionRow(item, domain);
const doMentionUser = user && vintedChannel.preferences.get(Preference.Mention);
const mentionString = doMentionUser ? `<@${user.discordId}>` : '';
try {
await postMessageToChannel(
token,
vintedChannel.channelId,
`${mentionString} `,
[embed, ...photosEmbeds],
[actionRow]
);
}
catch (error) {
Logger.debug('Error posting message to channel');
Logger.debug(error);
}
};
Logger.info('Fetching monitored channels');
let allMonitoringChannels = await crud.getAllMonitoredVintedChannels();
let allMonitoringChannelsBrandMap = await crud.getAllMonitoredVintedChannelsBrandMap();
// Print the number of monitored channels
Logger.info(`Monitoring ${allMonitoringChannels.length} Vinted channels`);
crud.eventEmitter.on('updated', async () => {
allMonitoringChannels = await crud.getAllMonitoredVintedChannels();
allMonitoringChannelsBrandMap = await crud.getAllMonitoredVintedChannelsBrandMap();
Logger.debug('Updated vinted channels');
});
const monitorChannels = () => {
const handleItem = async (rawItem) => {
Logger.debug('Handling item');
const item = new VintedItem(rawItem);
if (item.getNumericStars() === 0 && algorithmSettings.filter_zero_stars_profiles) {
return;
}
let rawItemBrandId = item.brandId;
rawItemBrandId = rawItemBrandId ? rawItemBrandId.toString() : null;
if (allMonitoringChannelsBrandMap.has(rawItemBrandId)) {
const brandChannels = allMonitoringChannelsBrandMap.get(rawItemBrandId);
for (const brandChannel of brandChannels) {
try {
const user = brandChannel.user;
const matchingItems = filterItemsByUrl(
[item],
brandChannel.url,
brandChannel.bannedKeywords,
brandChannel.preferences.get(Preference.Countries) || []
);
if (matchingItems.length > 0) {
sendToChannel(item, user, brandChannel);
}
} catch(error) {
Logger.debug('Error sending to channel');
Logger.debug(error);
}
}
}
};
(async () => {
await CatalogService.findHighestIDUntilSuccessful(cookie);
while (true) {
try {
await CatalogService.fetchUntilCurrentAutomatic(cookie, handleItem);
} catch (error) {
console.error(error);
}
}
})();
};
Logger.info('Starting monitoring channels');
monitorChannels();
if (discordConfig.channel_inactivity_enabled) {
//every 30 minutes
setInterval(() => {
checkVintedChannelInactivity(client)
}, 1000 * 60 * 30);
}