mirror of
https://github.com/vega-org/vega-providers.git
synced 2026-04-17 15:41:45 +00:00
fix: limit results in getPosts and enhance search filtering with fuzzy matching
This commit is contained in:
2
dist/a111477/posts.js
vendored
2
dist/a111477/posts.js
vendored
@@ -1 +1 @@
|
||||
"use strict";var __awaiter=this&&this.__awaiter||function(thisArg,_arguments,P,generator){return new(P||(P=Promise))(function(resolve,reject){function fulfilled(value){try{step(generator.next(value))}catch(e){reject(e)}}function rejected(value){try{step(generator.throw(value))}catch(e){reject(e)}}function step(result){var value;result.done?resolve(result.value):(value=result.value,value instanceof P?value:new P(function(resolve){resolve(value)})).then(fulfilled,rejected)}step((generator=generator.apply(thisArg,_arguments||[])).next())})};Object.defineProperty(exports,"__esModule",{value:!0}),exports.getSearchPosts=exports.getPosts=void 0;const getPosts=function(_a){return __awaiter(this,arguments,void 0,function*({filter:filter,page:page,signal:signal,providerContext:providerContext}){const{axios:axios,cheerio:cheerio}=providerContext;if(page>1)return[];return posts({baseUrl:"https://a.111477.xyz",url:`https://a.111477.xyz${filter}`,signal:signal,axios:axios,cheerio:cheerio})})};exports.getPosts=getPosts;const getSearchPosts=function(_a){return __awaiter(this,arguments,void 0,function*({searchQuery:searchQuery,page:page,signal:signal,providerContext:providerContext}){const{axios:axios,cheerio:cheerio}=providerContext,baseUrl="https://a.111477.xyz";if(page>1)return[];return[...yield posts({baseUrl:baseUrl,url:`${baseUrl}/movies/`,signal:signal,axios:axios,cheerio:cheerio}),...yield posts({baseUrl:baseUrl,url:`${baseUrl}/tvs/`,signal:signal,axios:axios,cheerio:cheerio})].filter(post=>post.title.toLowerCase().includes(searchQuery.toLowerCase()))})};function posts(_a){return __awaiter(this,arguments,void 0,function*({baseUrl:baseUrl,url:url,signal:signal,axios:axios,cheerio:cheerio}){try{const data=(yield axios.get(url,{signal:signal})).data,$=cheerio.load(data),catalog=[];return $("table tbody tr").each((i,element)=>{const linkElement=$(element).find("td:first-child a"),title=linkElement.text().trim(),link=linkElement.attr("href");if(title&&link&&"../"!==title&&"Parent Directory"!==title&&title.endsWith("/")){const cleanTitle=title.replace(/\/$/,""),fullLink=url+link,imageTitle=cleanTitle.length>30?cleanTitle.slice(0,30).replace(/\./g," "):cleanTitle.replace(/\./g," "),image=`https://placehold.jp/23/000000/ffffff/200x400.png?text=${encodeURIComponent(imageTitle)}&css=%7B%22background%22%3A%22%20-webkit-gradient(linear%2C%20left%20bottom%2C%20left%20top%2C%20from(%233f3b3b)%2C%20to(%23000000))%22%2C%22text-transform%22%3A%22%20capitalize%22%7D`;catalog.push({title:cleanTitle,link:fullLink,image:image})}}),catalog.slice(0,50)}catch(err){return[]}})}exports.getSearchPosts=getSearchPosts;
|
||||
"use strict";var __awaiter=this&&this.__awaiter||function(thisArg,_arguments,P,generator){return new(P||(P=Promise))(function(resolve,reject){function fulfilled(value){try{step(generator.next(value))}catch(e){reject(e)}}function rejected(value){try{step(generator.throw(value))}catch(e){reject(e)}}function step(result){var value;result.done?resolve(result.value):(value=result.value,value instanceof P?value:new P(function(resolve){resolve(value)})).then(fulfilled,rejected)}step((generator=generator.apply(thisArg,_arguments||[])).next())})};Object.defineProperty(exports,"__esModule",{value:!0}),exports.getSearchPosts=exports.getPosts=void 0;const getPosts=function(_a){return __awaiter(this,arguments,void 0,function*({filter:filter,page:page,signal:signal,providerContext:providerContext}){const{axios:axios,cheerio:cheerio}=providerContext;if(page>1)return[];const url=`https://a.111477.xyz${filter}`;return(yield posts({baseUrl:"https://a.111477.xyz",url:url,signal:signal,axios:axios,cheerio:cheerio})).slice(0,50)})};exports.getPosts=getPosts;const getSearchPosts=function(_a){return __awaiter(this,arguments,void 0,function*({searchQuery:searchQuery,page:page,signal:signal,providerContext:providerContext}){const{axios:axios,cheerio:cheerio}=providerContext,baseUrl="https://a.111477.xyz";if(page>1)return[];return[...yield posts({baseUrl:baseUrl,url:`${baseUrl}/movies/`,signal:signal,axios:axios,cheerio:cheerio}),...yield posts({baseUrl:baseUrl,url:`${baseUrl}/tvs/`,signal:signal,axios:axios,cheerio:cheerio})].filter(post=>{const title=post.title.toLowerCase(),query=searchQuery.toLowerCase();if(title.includes(query))return!0;const queryWords=query.split(/\s+/).filter(word=>word.length>0),titleWords=title.split(/[\s\-\.\(\)\[\]]+/).filter(word=>word.length>0);if(queryWords.every(queryWord=>titleWords.some(titleWord=>titleWord.includes(queryWord))))return!0;if(1===queryWords.length){const queryWord=queryWords[0];if(queryWord.length>=3){if(titleWords.some(titleWord=>titleWord.startsWith(queryWord)))return!0;if(titleWords.some(titleWord=>{if(Math.abs(titleWord.length-queryWord.length)>2)return!1;return levenshteinDistance(titleWord,queryWord)<=Math.max(1,Math.floor(.2*queryWord.length))}))return!0}}return!1})})};function posts(_a){return __awaiter(this,arguments,void 0,function*({baseUrl:baseUrl,url:url,signal:signal,axios:axios,cheerio:cheerio}){try{const data=(yield axios.get(url,{signal:signal})).data,$=cheerio.load(data),catalog=[];return $("table tbody tr").each((i,element)=>{const linkElement=$(element).find("td:first-child a"),title=linkElement.text().trim(),link=linkElement.attr("href");if(title&&link&&"../"!==title&&"Parent Directory"!==title&&title.endsWith("/")){const cleanTitle=title.replace(/\/$/,""),fullLink=url+link,imageTitle=cleanTitle.length>30?cleanTitle.slice(0,30).replace(/\./g," "):cleanTitle.replace(/\./g," "),image=`https://placehold.jp/23/000000/ffffff/200x400.png?text=${encodeURIComponent(imageTitle)}&css=%7B%22background%22%3A%22%20-webkit-gradient(linear%2C%20left%20bottom%2C%20left%20top%2C%20from(%233f3b3b)%2C%20to(%23000000))%22%2C%22text-transform%22%3A%22%20capitalize%22%7D`;catalog.push({title:cleanTitle,link:fullLink,image:image})}}),catalog}catch(err){return[]}})}function levenshteinDistance(str1,str2){const matrix=Array(str2.length+1).fill(null).map(()=>Array(str1.length+1).fill(null));for(let i=0;i<=str1.length;i++)matrix[0][i]=i;for(let j=0;j<=str2.length;j++)matrix[j][0]=j;for(let j=1;j<=str2.length;j++)for(let i=1;i<=str1.length;i++){const indicator=str1[i-1]===str2[j-1]?0:1;matrix[j][i]=Math.min(matrix[j][i-1]+1,matrix[j-1][i]+1,matrix[j-1][i-1]+indicator)}return matrix[str2.length][str1.length]}exports.getSearchPosts=getSearchPosts;
|
||||
@@ -18,7 +18,8 @@ export const getPosts = async function ({
|
||||
return [];
|
||||
}
|
||||
const url = `${baseUrl}${filter}`;
|
||||
return posts({ baseUrl, url, signal, axios, cheerio });
|
||||
const result = await posts({ baseUrl, url, signal, axios, cheerio });
|
||||
return result.slice(0, 50); // Limit only for getPosts
|
||||
};
|
||||
|
||||
export const getSearchPosts = async function ({
|
||||
@@ -58,10 +59,59 @@ export const getSearchPosts = async function ({
|
||||
// Combine all posts
|
||||
const allPosts = [...moviesPosts, ...tvsPosts];
|
||||
|
||||
// Filter posts based on search query
|
||||
const filteredPosts = allPosts.filter((post) =>
|
||||
post.title.toLowerCase().includes(searchQuery.toLowerCase())
|
||||
);
|
||||
// Filter posts based on search query with improved matching
|
||||
const filteredPosts = allPosts.filter((post) => {
|
||||
const title = post.title.toLowerCase();
|
||||
const query = searchQuery.toLowerCase();
|
||||
|
||||
// Direct substring match
|
||||
if (title.includes(query)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Word boundary matching
|
||||
const queryWords = query.split(/\s+/).filter((word) => word.length > 0);
|
||||
const titleWords = title
|
||||
.split(/[\s\-\.\(\)\[\]]+/)
|
||||
.filter((word) => word.length > 0);
|
||||
|
||||
// Check if all query words are found in title words
|
||||
const allWordsMatch = queryWords.every((queryWord) =>
|
||||
titleWords.some((titleWord) => titleWord.includes(queryWord))
|
||||
);
|
||||
|
||||
if (allWordsMatch) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Fuzzy matching for single word queries
|
||||
if (queryWords.length === 1) {
|
||||
const queryWord = queryWords[0];
|
||||
if (queryWord.length >= 3) {
|
||||
// Check if any title word starts with the query
|
||||
const startsWithMatch = titleWords.some((titleWord) =>
|
||||
titleWord.startsWith(queryWord)
|
||||
);
|
||||
|
||||
if (startsWithMatch) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Levenshtein distance for close matches
|
||||
const hasCloseMatch = titleWords.some((titleWord) => {
|
||||
if (Math.abs(titleWord.length - queryWord.length) > 2) return false;
|
||||
const distance = levenshteinDistance(titleWord, queryWord);
|
||||
return distance <= Math.max(1, Math.floor(queryWord.length * 0.2));
|
||||
});
|
||||
|
||||
if (hasCloseMatch) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
return filteredPosts;
|
||||
};
|
||||
@@ -120,9 +170,33 @@ async function posts({
|
||||
}
|
||||
});
|
||||
|
||||
return catalog.slice(0, 50);
|
||||
// Only limit for regular getPosts, not for search
|
||||
return catalog;
|
||||
} catch (err) {
|
||||
console.error("111477 directory listing error:", err);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
// Levenshtein distance function for fuzzy matching
|
||||
function levenshteinDistance(str1: string, str2: string): number {
|
||||
const matrix = Array(str2.length + 1)
|
||||
.fill(null)
|
||||
.map(() => Array(str1.length + 1).fill(null));
|
||||
|
||||
for (let i = 0; i <= str1.length; i++) matrix[0][i] = i;
|
||||
for (let j = 0; j <= str2.length; j++) matrix[j][0] = j;
|
||||
|
||||
for (let j = 1; j <= str2.length; j++) {
|
||||
for (let i = 1; i <= str1.length; i++) {
|
||||
const indicator = str1[i - 1] === str2[j - 1] ? 0 : 1;
|
||||
matrix[j][i] = Math.min(
|
||||
matrix[j][i - 1] + 1, // deletion
|
||||
matrix[j - 1][i] + 1, // insertion
|
||||
matrix[j - 1][i - 1] + indicator // substitution
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return matrix[str2.length][str1.length];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user