forked from DebaucheryLibrarian/traxxx
Updated OSM API path, added redis caching.
This commit is contained in:
15
src/redis.js
Normal file
15
src/redis.js
Normal file
@@ -0,0 +1,15 @@
|
||||
'use strict';
|
||||
|
||||
const config = require('config');
|
||||
const redis = require('redis');
|
||||
const logger = require('./logger')(__filename);
|
||||
|
||||
logger.verbose('Redis module initialized');
|
||||
|
||||
const redisClient = redis.createClient({
|
||||
socket: config.redis,
|
||||
});
|
||||
|
||||
redisClient.connect();
|
||||
|
||||
module.exports = redisClient;
|
||||
@@ -14,7 +14,7 @@ function scrapeAll(scenes) {
|
||||
const { origin, pathname, searchParams } = new URL(url);
|
||||
|
||||
release.url = `${origin}${pathname}`;
|
||||
release.shootId = pathname.match(/((LA)|(LT)|(MA)|(MD)|(MM)|(MS)|(MT)|(RR))\w*-\w+((EP)?\d+)?/)?.[0]; // pathname sometimes contains other text, match at least two letters to prevent false positives
|
||||
release.shootId = pathname.match(/((HP)|(LA)|(LT)|(MA)|(MD)|(MM)|(MS)|(MT)|(RR))\w*-\w+((EP)?\d+)?/)?.[0]; // pathname sometimes contains other text, match at least two letters to prevent false positives
|
||||
|
||||
release.actors = searchParams.get('models_name')?.split(',').map((actor) => {
|
||||
const [han, english] = actor.split('/').map((name) => name.trim());
|
||||
|
||||
@@ -5,12 +5,26 @@ const config = require('config');
|
||||
const knex = require('../knex');
|
||||
const logger = require('../logger')(__filename);
|
||||
const http = require('./http');
|
||||
const slugify = require('./slugify');
|
||||
const argv = require('../argv');
|
||||
const redis = require('../redis');
|
||||
|
||||
async function resolvePlace(query) {
|
||||
if (!query) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const cacheKey = `place-${slugify(query)}`;
|
||||
const cachedPlace = await redis.hGetAll(cacheKey);
|
||||
|
||||
if (cachedPlace && argv.placeCache !== false) {
|
||||
await redis.expire(cacheKey, 3600 * 24 * 30);
|
||||
|
||||
logger.debug(`Using cached place '${cacheKey}' for query '${query}': ${JSON.stringify(cachedPlace)}`);
|
||||
|
||||
return cachedPlace;
|
||||
}
|
||||
|
||||
// query is a nationality, lookup would get weird results (British resolves to British, Northern Ireland)
|
||||
const country = await knex('countries')
|
||||
.where('nationality', 'ilike', `%${query}%`)
|
||||
@@ -27,7 +41,7 @@ async function resolvePlace(query) {
|
||||
|
||||
try {
|
||||
// https://operations.osmfoundation.org/policies/nominatim/
|
||||
const res = await http.get(`https://nominatim.openstreetmap.org/search/${encodeURI(query)}?format=json&accept-language=en&addressdetails=1`, {
|
||||
const res = await http.get(`https://nominatim.openstreetmap.org/search?q=${encodeURI(query)}&format=json&accept-language=en&addressdetails=1`, {
|
||||
headers: {
|
||||
'User-Agent': config.location.userAgent,
|
||||
},
|
||||
@@ -54,6 +68,11 @@ async function resolvePlace(query) {
|
||||
if (rawPlace.country_code) place.country = rawPlace.country_code.toUpperCase();
|
||||
if (rawPlace.continent) place.continent = rawPlace.continent;
|
||||
|
||||
logger.debug(`Resolved place '${query}' to ${JSON.stringify(place)}`);
|
||||
|
||||
await redis.hSet(cacheKey, place);
|
||||
await redis.expire(cacheKey, 3600 * 24 * 30);
|
||||
|
||||
return place;
|
||||
}
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user