Skip to content

Commit

Permalink
fix: #14
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulrahman1s committed Mar 7, 2023
1 parent 3cba281 commit aa12433
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
10 changes: 5 additions & 5 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ export const FEM_CAPTIONS_ENDPOINT = `https://captions.${FEM_BASE}`
export const PLAYLIST_EXT = 'm3u8'
export const CAPTION_EXT = 'vtt'
export const QUALITY_FORMAT = {
2160: 'index_2160p_Q10_20mbps',
1440: 'index_1440p_Q10_9mbps',
1080: 'index_1080_Q10_7mbps',
720: 'index_720_Q8_5mbps',
360: 'index_360_Q8_2mbps'
2160: ['index_2160p_Q10_20mbps'],
1440: ['index_1440p_Q10_9mbps'],
1080: ['index_1080_Q10_7mbps', 'index_1080_Q8_7mbps'],
720: ['index_720_Q8_5mbps'],
360: ['index_360_Q8_2mbps']
}

export const FEM_COURSE_REG = /(:?https?:\/\/)?frontendmasters\.com\/courses\/([^/]+)/
Expand Down
19 changes: 12 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ const [lessons, totalEpisodes] = course.lessonElements.reduce((acc, cur) => {
}, [{}, 0, ''])


let i = 1, x = 0, QUALITY = PREFERRED_QUALITY
let i = 1, x = 0, QUALITY = PREFERRED_QUALITY, downgradeAlert = false

const coursePath = safeJoin(DOWNLOAD_DIR, course.title)

Expand Down Expand Up @@ -146,8 +146,10 @@ for (const [lesson, episodes] of Object.entries(lessons)) {
// Automatically downgrade quality when preferred quality not found
const qualities = Object.values(QUALITY_FORMAT)

while (!availableQualities.includes(QUALITY) && availableQualities.includes('#EXTM3U')) {
QUALITY = qualities[qualities.indexOf(QUALITY) - 1]
while (!QUALITY.some((it) => availableQualities.includes(it)) && availableQualities.includes('#EXTM3U')) {
const index = qualities.findIndex(it => it.every(q => QUALITY.includes(q)))

QUALITY = qualities[index - 1]

if (typeof QUALITY === 'undefined') {
console.warn(`This shouldn't happen, please fill an issue`)
Expand All @@ -156,12 +158,15 @@ for (const [lesson, episodes] of Object.entries(lessons)) {
}
}

if (QUALITY !== PREFERRED_QUALITY) {
const [formattedQuality] = Object.entries(QUALITY_FORMAT).find(([_, value]) => value === QUALITY)
spinner.text = `The preferred quality was not found, downgraded to ${formattedQuality}p`
if (!downgradeAlert && !PREFERRED_QUALITY.some(it => QUALITY.includes(it))) {
downgradeAlert = true
const [formattedQuality] = Object.entries(QUALITY_FORMAT).find(([_, it]) => it.every(q => QUALITY.includes(q)))
spinner.clear()
console.log(`\nThe preferred quality was not found, downgraded to ${formattedQuality}p`)
}

const m3u8Url = [...m3u8RequestUrl.split('/').slice(0, -1), `${QUALITY}.${PLAYLIST_EXT}`].join('/')
const streamQuality = QUALITY.find(it => availableQualities.includes(it))
const m3u8Url = [...m3u8RequestUrl.split('/').slice(0, -1), `${streamQuality}.${PLAYLIST_EXT}`].join('/')

headers['Cookie'] = await cookies.getCookieString(m3u8Url)

Expand Down

0 comments on commit aa12433

Please sign in to comment.