Skip to content

Commit

Permalink
(Improve): Search poster with Thai title
Browse files Browse the repository at this point in the history
  • Loading branch information
kang49 committed Apr 4, 2024
1 parent 9a95d88 commit 24cc597
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 25 deletions.
49 changes: 28 additions & 21 deletions src/findAnimeName.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
import axios from 'axios';
import cheerio from 'cheerio';

export async function findName(BiliLink: string) {
export async function findName(biliLink: string) {
const fetchPageName = async (url: string) => {
try {
const response = await axios.get(BiliLink);
const { data, status } = await axios.get(url);
if (status !== 200) throw new Error('Failed to fetch page data, findname');

const $ = cheerio.load(data);
const elementSelector = '#app > div > div > section > main > div > section > div.video-play__meta.video-play__meta--ogv > section > header > h1 > a';
const element = $(elementSelector);

if (response.status === 200) {
const $ = cheerio.load(response.data);
return element.length > 0 ? element.text().trim() : 'No data found, findname';
} catch (error) {
console.error(`Error fetching data from ${url}:`, error, 'findname');
return 'Error fetching data, findname';
}
};

// Find the element with the specified selector
const element = $('#app > div > div > section > main > div > section > div.video-play__meta.video-play__meta--ogv > section > header > h1 > a');
try {
// Fetch the original page to get the response URL for possible redirection
const { request } = await axios.get(biliLink);
const originalLink = request.res.responseUrl;
const thBiliLink = originalLink.replace('/en/', '/th/');

// Check if the element was found
if (element.length > 0) {
// Get the text content of the element
const animeName = element.text();
return animeName as string;
} else {
return console.error('No data found, findName');
}
const names = await Promise.all([
fetchPageName(originalLink),
fetchPageName(thBiliLink)
]);

// Add code to retrieve additional data from the website as needed.
} else {
return console.error('Unable to fetch data', 'findName');
}
} catch (error) {
return console.error('An error occurred while fetching data:', error, 'findName');
}
return names;
} catch (error) {
console.error('An error occurred while fetching data:', error, 'findname');
return ['Error fetching data', 'Error fetching data', 'findname'];
}
}
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ app.get(/\/api$/, async (req, res) => {
BiliLink = link;

if (BiliLink) {
const animeName: string = await findName(BiliLink) as string; //Anime Name
const animeName: string[] = await findName(BiliLink) //Anime Name
console.log(animeName);
if (!animeName) return res.status(400).json({ error: 'anime not found' });
const animePoster: string = await findPoster(animeName) as string; //Anime Poster
const animePoster: string = await findPoster(animeName[1]) as string; //Anime Poster
if (!animePoster || animePoster === undefined) return res.status(400).json({ error: 'anime poster not found' });
const storyImageBase64: string = await storyCreator(animePoster, animeName) as string;
const storyImageBase64: string = await storyCreator(animePoster, animeName[0]) as string;

//response main API
res.json({ title: title, imageBase64: storyImageBase64 });
Expand Down
2 changes: 1 addition & 1 deletion src/test/main-Test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async function mainTest(BiliLink: string) {
BiliLink = link;

if (BiliLink) {
const animeName: string = await findName(BiliLink) as string; //Anime Name
const animeName: string[] = await findName(BiliLink) //Anime Name
console.log(animeName);
if (!animeName || animeName === undefined) throw new Error("anime not found")

Expand Down

0 comments on commit 24cc597

Please sign in to comment.