0
0
mirror of https://github.com/Maks1mS/free-ozon-dpr.git synced 2025-10-19 08:28:40 +03:00

add auto update ozon_wb_dpr

This commit is contained in:
2024-06-06 19:38:01 +03:00
parent 1093d592ca
commit da2fed1fd2
8 changed files with 321 additions and 79 deletions

View File

@@ -2,6 +2,7 @@ import fs from "node:fs/promises";
import { asyncMap } from "modern-async";
import { JSDOM } from "jsdom";
import { getFinalURL } from "./utils.js";
import ozonWbDpr from "./update/ozon-wb-dpr.js";
async function woyag() {
const apiResponse = await fetch("https://login.woyag.ru/ajax/pvz-list");
@@ -84,6 +85,7 @@ async function sevenDostavka() {
async function main() {
await woyag();
await sevenDostavka();
await ozonWbDpr();
}
main();

0
scripts/update/index.js Normal file
View File

View File

@@ -0,0 +1,124 @@
import { distance } from "fastest-levenshtein";
import { getFinalURL, getTelegramMessage } from "../utils.js";
import { JSDOM } from "jsdom";
import { asyncMap } from "modern-async";
import fs from "node:fs/promises";
const MAIN_URL = "https://t.me/ozon_wb_dpr/627";
const QR_FOR_PVZ_STRING = "QR для ПВЗ";
const closestObj = (str, arr, key) => {
let min_distance = Infinity;
let min_index = 0;
for (let i = 0; i < arr.length; i++) {
const dist = distance(str, arr[i][key]);
if (dist < min_distance) {
min_distance = dist;
min_index = i;
}
}
return arr[min_index];
};
async function getPVZFromPost(post) {
const message = await getTelegramMessage(post);
const dom = new JSDOM(message.text);
const document = dom.window.document;
const linkElement = document.querySelector(
'a[href*="vk.cc"],a[href*="ozon.ru"]'
);
const link = await getFinalURL(linkElement["href"]).then((u) => {
const final = new URL(u);
final.search = "";
return final.toString();
});
return link;
}
async function getFromTelegram() {
const message = await getTelegramMessage(MAIN_URL);
const dom = new JSDOM(message.text);
const document = dom.window.document;
const links = document.querySelectorAll("a");
return (
await asyncMap(links, async (link) => {
const textContent = link.textContent;
if (textContent.startsWith(QR_FOR_PVZ_STRING)) {
const address = textContent.replace(QR_FOR_PVZ_STRING, "").trim();
let postLink = link["href"];
// TEMP FIX
if (address === "г.Донецк, ул.Университетская, 76") {
postLink = "https://t.me/ozon_wb_dpr/774";
}
return {
name: `ПВЗ ${address}`,
address,
link: await getPVZFromPost(postLink),
operationTime: "пн-вс с 9:00 до 17:45",
};
}
})
).filter(Boolean);
}
async function getFromSite() {
const res = await fetch("https://ozon-wb-dpr.ru/");
const html = await res.text();
const dom = new JSDOM(html);
const document = dom.window.document;
const scripts = document.querySelectorAll("script");
for (let script of scripts) {
if (script.textContent.includes('descr: "Бесплатный озон')) {
let x = script.textContent.split("[\n{\n").pop().split("\n},\n]")[0];
if (x) {
const points = new Function(`return [{${x}}]`)();
return points;
}
}
}
}
async function ozonWbDpr() {
const telegramPoints = await getFromTelegram();
let points = (await getFromSite()).map((point) => ({
coordinates: [parseFloat(point["lng"]), parseFloat(point["lat"])],
name: point.title,
}));
points = points.map((point) => {
const obj = closestObj(point.name, telegramPoints, "name");
return {
coordinates: point.coordinates,
...obj,
};
});
await fs.writeFile(
"data/03_ozon-wb-dpr.json",
JSON.stringify(
{
name: "ПВЗ ДНР",
source: MAIN_URL,
points,
},
undefined,
4
)
);
}
export default ozonWbDpr;

View File

@@ -1,23 +1,65 @@
import { JSDOM } from "jsdom";
async function resolveVKcc(url) {
const response = await fetch(url);
const text = await response.text();
const match = new RegExp(/value=["']([^"']+)["']/).exec(text);
if (match && match.length > 1) {
return match[1];
} else {
return;
}
}
export async function getFinalURL(url) {
let response = await fetch(url, {
if (url?.startsWith('https://vk.cc')) {
url = await resolveVKcc(url);
}
if (!url) {
return;
}
let response = await fetch(url, {
method: "HEAD",
redirect: "manual",
});
while (
response.status >= 300 &&
response.status < 400 &&
response.headers.get("location") &&
!url.startsWith('https://ozon.ru/point/')
) {
url = response.headers.get("location");
if (!url.startsWith("http")) {
const baseUrl = new URL(response.url);
url = `${baseUrl.protocol}//${baseUrl.host}${url}`;
}
response = await fetch(url, {
method: "HEAD",
redirect: "manual",
});
while (
response.status >= 300 &&
response.status < 400 &&
response.headers.get("location") &&
new URL(url).hostname !== 'ozon.ru'
) {
url = response.headers.get("location");
if (!url.startsWith("http")) {
const baseUrl = new URL(response.url);
url = `${baseUrl.protocol}//${baseUrl.host}${url}`;
}
response = await fetch(url, {
method: "HEAD",
redirect: "manual",
});
}
return url;
}
}
return url;
}
export async function getTelegramMessage(input) {
const url = new URL(input);
url.search = "?embed=1&mode=tme";
const res = await fetch(url);
const htmlText = await res.text();
const dom = new JSDOM(htmlText);
const document = dom.window.document;
const message = document.querySelector(".tgme_widget_message");
return {
text: message.querySelector("div.tgme_widget_message_text.js-message_text")
.innerHTML,
};
}