-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHomeFunc.js
More file actions
184 lines (163 loc) · 5.81 KB
/
Copy pathHomeFunc.js
File metadata and controls
184 lines (163 loc) · 5.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// import axios from 'axios';
import cheerio from 'cheerio';
import {checkDownTime} from '../../Redux/Actions/GlobalActions';
import {fetchDataStart} from '../../Redux/Reducers';
import {AnimeHostName, ComicHostName as HostName} from '../../Utils/APIs';
import APICaller from '../../Redux/Controller/Interceptor';
export const fetchComicsData = async (link, dispatch, baseUrl) => {
console.log(baseUrl, 'baseUrl');
if (!link) return;
dispatch(fetchDataStart());
try {
// Fetch the HTML content from the website
let url = `${HostName[baseUrl]}`;
const response = await APICaller.get(`${url}${link}`);
const html = response.data;
// console.log(html, "html");
// Load the HTML into Cheerio
const $ = cheerio.load(html);
// Array to hold the extracted data
const comicsData = [];
let lastPage = null;
// Extract data from the website
if (baseUrl === 'azcomic') {
$('.ig-grid .ig-box').each((index, element) => {
const title = $(element).find('.igb-name').text().trim();
const imageUrl = $(element).find('.igb-image img').attr('src');
const link = $(element).find('.igb-image').attr('href');
const genres = [];
// Extract genres
$(element)
.find('.igb-genres a')
.each((i, el) => {
genres.push($(el).text().trim());
});
// Push the comic data to the array
comicsData.push({
title,
imageUrl,
link,
genres,
date: null,
});
});
} else {
// Select and iterate over each comic post
$('#post-area .post').each((index, element) => {
// Extract the title
const title = $(element).find('h2 a').text();
// Extract the date
const date = $(element).find('span').first().text();
// Extract the image URL
const imageUrl = $(element).find('img').attr('src');
//link to comic
const link = $(element).find('h2 a').attr('href');
// Push the extracted data into the array
comicsData.push({title, date, imageUrl, link});
});
let page = link.split('/');
page = page[page.length - 2];
if (page == 1) {
lastPage = $('.pagenavi span.page-numbers.dots')
.next()
.text()
.trim()
.replaceAll(',', '');
}
}
dispatch(checkDownTime(response, baseUrl));
return {data: comicsData, lastPage};
} catch (error) {
// console.log(link, 'link');
console.log('Error fetching or parsing data Home:', error);
if (dispatch) dispatch(checkDownTime(error, baseUrl));
return [];
}
};
export const FetchAnimeData = async (link, dispatch, baseUrl) => {
// console.log(baseUrl, link, 'baseUrl');
if (!link) return;
dispatch(fetchDataStart());
try {
let url = 'https://ajax.gogocdn.net/ajax/page-recent-release.html';
const baseUrlLink = AnimeHostName[baseUrl];
//check if link have ?type= or not
if (!link.includes('type=')) {
url = AnimeHostName[baseUrl];
}
console.log(`${url}${link}`, 'url');
url = baseUrl == 'gogoanimes' ? `${url}${link}` : `${baseUrlLink}${link}`;
// Fetch the HTML content from the website
// console.log(url, 'url');
const response = await APICaller.get(url);
const html = response.data;
// console.log(response, "html");
// Load the HTML into Cheerio
const $ = cheerio.load(html);
// Array to hold the extracted data
const AnimaData = [];
let lastPage = null;
// Extract data from the website
if (baseUrl == 'gogoanimes') {
console.log('gogoanimes');
$('.last_episodes .items li').each((index, element) => {
let title = $(element).find('.name a').attr('title');
let link = $(element).find('.name a').attr('href');
let imageUrl = $(element).find('.img a img').attr('src');
let episode = $(element).find('.episode').text();
let date = $(element).find('.released').text().trim() || null;
//if image missing hostName then add base url
if (!imageUrl.includes('https://'))
imageUrl = `${baseUrlLink}${imageUrl.replace('/', '')}`;
AnimaData.push({
title,
link: `${baseUrlLink}${link.replace('/', '')}`,
imageUrl,
episode,
date,
});
});
// console.log(AnimaData, 'AnimaData');
} else {
// console.log($(".meta").text(), 'html');
$('.listing li.video-block').each((i, elem) => {
const title = $(elem).find('.name').text().trim();
const link = $(elem).find('a').attr('href');
const imageUrl = $(elem).find('.picture img').attr('src');
//extract episode from title
const episodeMatch = title.match(/Episode (\d+)/);
let episode = episodeMatch ? parseInt(episodeMatch[1], 10) : null;
AnimaData.push({
title,
link: `${baseUrlLink}${link}`,
imageUrl,
episode: 'Episode ' + episode,
});
});
// console.log(AnimaData, 'videos');
}
dispatch(checkDownTime(response, baseUrl));
return AnimaData;
} catch (error) {
console.log('Error fetching or parsing data Anime Home page: ', error);
if (dispatch) dispatch(checkDownTime(error, baseUrl));
return [];
}
};
export const checkServerDown = async (url, dispatch, sourceKey = null) => {
dispatch(fetchDataStart());
try {
const response = await APICaller.get(url);
//set DownTime to false
console.log(response.status, 'response');
dispatch(checkDownTime(response, sourceKey));
return false;
} catch (error) {
//set DownTime to true
console.log('Error checking server down:', error.message);
dispatch(checkDownTime(error, sourceKey));
return true;
}
};
export const serverStatusUp = serverStatus =>
!(serverStatus && serverStatus >= 500);