aboutsummaryrefslogtreecommitdiff
path: root/vnext/server/sape.js
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2022-10-28 00:14:01 +0300
committerGravatar Vitaly Takmazov2023-01-13 10:37:58 +0300
commit40d411e516efee5531404725b45ab89d97172ce8 (patch)
treed675e93fc52ef50a40343219e1b992867964d3bf /vnext/server/sape.js
parent2146a98bd98b7e275a0ee7bc7a243981b597f34c (diff)
Initial SSR
Diffstat (limited to 'vnext/server/sape.js')
-rw-r--r--vnext/server/sape.js38
1 files changed, 38 insertions, 0 deletions
diff --git a/vnext/server/sape.js b/vnext/server/sape.js
new file mode 100644
index 00000000..d8c9e2ed
--- /dev/null
+++ b/vnext/server/sape.js
@@ -0,0 +1,38 @@
+import { parseStringPromise } from 'xml2js';
+import axios from 'axios';
+import { setupCache } from 'axios-cache-interceptor';
+
+const token = process.env.SAPE_TOKEN;
+
+/** @external Promise */
+
+/**
+ * @param { string } uri
+ * @param { string } sapeCookie
+ * @returns { Promise<string[]>} links
+ */
+export const getLinks = async (uri, sapeCookie) => {
+ if (!token) {
+ console.warn('Sape is not configured');
+ return [];
+ }
+ const response = await sape.get(`http://dispencer-01.sape.ru/code.php?user=${token}&host=juick.com&charset=UTF-8&as_xml=true`);
+ const data = await parseStringPromise(response.data);
+ const showCode = token === sapeCookie;
+ const requestURI = showCode ? '*' : uri;
+ const page = data.sape.page.filter(page => {
+ const uri = page['$']['uri'];
+ return uri === requestURI;
+ });
+ return page.length > 0 ? showCode ? [page[0]._] : page[0].link : [];
+};
+
+/** @type { import('axios-cache-interceptor').AxiosCacheInstance } */
+let sape = setupCache(
+ axios.create({
+ headers: {
+ 'User-Agent': 'SAPE_Client PHP 1.0.3'
+ }
+ }),
+ { ttl: 3600 * 1000 }
+);