(cidd)
| 16 | } |
| 17 | |
| 18 | async function fetchMeta(cidd) { |
| 19 | //default metadata values |
| 20 | const meta = { |
| 21 | 'url': ciddToURL(cidd), |
| 22 | 'cid': cidd['cid'], |
| 23 | 'proper name': '', |
| 24 | 'thumbnail': '', |
| 25 | 'url name': cidd['cid'], |
| 26 | 'number of levels': '??', |
| 27 | 'number of items': '??', |
| 28 | 'description': '', |
| 29 | 'author': '', |
| 30 | 'ava': 'https://static.memrise.com/accounts/img/placeholders/empty-avatar-2.png', // -> rnd 1..4 |
| 31 | }; |
| 32 | |
| 33 | let course_page; |
| 34 | try { |
| 35 | course_page = await fetch(ciddToURL(cidd)); |
| 36 | } catch(err) { |
| 37 | console.error(`failed to fetch course ${cidd['cid']} html`, err); |
| 38 | } |
| 39 | if (!course_page) return meta; |
| 40 | |
| 41 | meta['url name'] = course_page.url.split("/")[6]; // course name from the end of url after redirections |
| 42 | |
| 43 | try { |
| 44 | const parser = new DOMParser(); |
| 45 | const doc = parser.parseFromString(await course_page.text(), "text/html"); |
| 46 | |
| 47 | meta['proper name'] = doc.querySelector('.course-name').innerText; |
| 48 | meta['thumbnail'] = doc.querySelector('.course-photo img').src; |
| 49 | meta['number of levels'] = (query => (query ? query.childElementCount : 1))(doc.querySelector('div.levels')); |
| 50 | meta['description'] = (desc => (desc ? desc.innerText : ""))(doc.querySelector('.course-description.text')); |
| 51 | meta['author'] = doc.querySelector('.creator-name span').innerText; |
| 52 | meta['ava'] = doc.querySelector('.creator-image img').src; |
| 53 | try { |
| 54 | //only works for courses that have been started, otherwise the info is not present on the page |
| 55 | meta['number of items'] = parseInt(doc.querySelector('.progress-box-title').innerText.split('/').at(-1).trim(), 10).toString(); |
| 56 | } catch(err) { |
| 57 | console.log(`course ${cidd['cid']} has not been started, info for 'number of items' unavailable`); |
| 58 | } |
| 59 | } catch(err) { |
| 60 | console.error(`failed to parse course ${cidd['cid']} html`, err); |
| 61 | } |
| 62 | |
| 63 | console.log(meta); |
| 64 | |
| 65 | return meta; |
| 66 | } |
| 67 | |
| 68 | function meta2txt(meta) { |
| 69 | let text = 'data:md/plain;charset=utf-8,' + encodeURIComponent( |
no test coverage detected