-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.js
153 lines (125 loc) · 4.77 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
import ProxyManager from "./src/utils/http_utils.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 } 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;
ProxyManager.init();
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));
}
}
};
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 * 5); // 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();
// Print the number of monitored channels
Logger.info(`Monitoring ${allMonitoringChannels.length} Vinted channels`);
crud.eventEmitter.on('updated', async () => {
allMonitoringChannels = await crud.getAllMonitoredVintedChannels();
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;
}
for (const vintedChannel of allMonitoringChannels) {
const user = vintedChannel.user;
const matchingItems = filterItemsByUrl([item], vintedChannel.url, vintedChannel.preferences.get(Preference.Countries) || []);
if (matchingItems.length > 0) {
sendToChannel(item, user, vintedChannel);
}
}
};
(async () => {
await CatalogService.findHighestIDUntilSuccessful(cookie);
while (true) {
try {
await CatalogService.fetchUntilCurrentAutomatic(cookie, handleItem);
} catch (error) {
console.error(error);
}
}
})();
};
Logger.info('Starting monitoring channels');
monitorChannels();