| 208 | } |
| 209 | |
| 210 | export function getProgress(res, mode, type) { |
| 211 | const config: { |
| 212 | mainId?: string; |
| 213 | fallbackPrediction?: string; |
| 214 | fallback?: string; |
| 215 | } = {}; |
| 216 | |
| 217 | if (!res.length) return null; |
| 218 | |
| 219 | if (mode === 'default') { |
| 220 | if (type === 'anime') { |
| 221 | config.mainId = api.settings.get('progressIntervalDefaultAnime'); |
| 222 | } else { |
| 223 | config.mainId = api.settings.get('progressIntervalDefaultManga'); |
| 224 | } |
| 225 | config.fallback = 'en/sub'; |
| 226 | } else { |
| 227 | config.mainId = mode; |
| 228 | } |
| 229 | |
| 230 | config.fallbackPrediction = 'jp/dub'; |
| 231 | |
| 232 | let top; |
| 233 | |
| 234 | if (config.mainId) { |
| 235 | const mainTemp = res.find(el => el.id === config.mainId); |
| 236 | if (mainTemp) top = mainTemp; |
| 237 | } |
| 238 | |
| 239 | if (config.fallback && !top) { |
| 240 | const mainTemp = res.find(el => el.id === config.fallback); |
| 241 | if (mainTemp) top = mainTemp; |
| 242 | } |
| 243 | |
| 244 | if (config.fallbackPrediction && top && !top.predicition && top.lastEp && top.lastEp.timestamp) { |
| 245 | const predTemp = res.find(el => el.id === config.fallbackPrediction); |
| 246 | const predTime = top.lastEp.timestamp + 7 * 24 * 60 * 60 * 1000; |
| 247 | if (predTime && predTemp && predTemp.predicition) { |
| 248 | if (top.lastEp.total === predTemp.lastEp.total) { |
| 249 | if (Math.abs(predTime - predTemp.predicition.timestamp) < 30 * 60 * 60 * 1000) { |
| 250 | top.predicition = { |
| 251 | timestamp: predTime, |
| 252 | probability: 'medium', |
| 253 | }; |
| 254 | } |
| 255 | } else if (predTemp.lastEp.total && top.lastEp.total === predTemp.lastEp.total - 1) { |
| 256 | if ( |
| 257 | Math.abs(predTime - (predTemp.predicition.timestamp - 7 * 24 * 60 * 60 * 1000)) < |
| 258 | 30 * 60 * 60 * 1000 |
| 259 | ) { |
| 260 | top.predicition = { |
| 261 | timestamp: predTime, |
| 262 | probability: 'medium', |
| 263 | }; |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | } |