Skip to content

Instantly share code, notes, and snippets.

@romasan
Last active November 21, 2020 21:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romasan/2dea5f6e7aeec91c293cfd8ed6bd7a49 to your computer and use it in GitHub Desktop.
Save romasan/2dea5f6e7aeec91c293cfd8ed6bd7a49 to your computer and use it in GitHub Desktop.
let kp = {};
kp.getViewedListFromEl = el => {
return [...el.querySelectorAll('.item')]
.filter(e => !e.classList.contains('itemAct'))
.filter(e => e.children[0].tagName === 'DIV')
.map(({children: [i, film, date]}) => ({
i: Number(i.innerText),
film,
viewdate: date.innerText
}))
.map(({i, film, viewdate}) => ({
index: i,
...((ru => ({
ru: ru[0].slice(0, -1),
en: film.querySelector('.nameEng').innerText,
year: ru[ru.length - 2],
}))(film.querySelector('.nameRus').innerText.split(/[\(\)]/ig))),
...((([rate,count,duration]) => ({
rate: rate ? rate.innerText : '',
duration: duration ? duration.innerText : '',
}))(
film.querySelector('.rating').children
)),
viewdate,
link: film.querySelector('a').href,
}))
}
kp.getPages = () => {
let t = [...document.querySelectorAll('.arr')]
.pop()
.children[0]
.href
.split('/');
const num = Number(t.splice(-2, 1, '{p}'));
t = t.join('/');
return Array(num - 1)
.fill()
.map((e, i) => i + 2)
.map(p => t.replace('{p}', p));
}
kp.fetchPages = pages => new Promise(async resolve => {
let url = null;
let data = [];
const count = pages.length;
while(url = pages.shift()) {
kp.echo(`download ${count - pages.length}/${count}`);
data.push(await fetch(url));
}
resolve(data);
});
kp.aofo2csv = list => {
const keys = Object.keys(list[0]);
let s = keys.map(e => '"' + e + '"').join(',') + '\n';
for (const line of list) {
for (const ik in keys) {
s = s + '"' + line[keys[ik]] + '"' + ((ik < (keys.length - 1)) ? ',' : '\n');
}
}
return s;
};
kp.text2file = (text, file, type = 'text/plain') => {
kp.echo('done');
const blob = new Blob([text], { type });
const elem = window.document.createElement('a');
elem.href = window.URL.createObjectURL(blob);
elem.download = file;
elem.click();
}
kp.downloadViewed = () => {
let list = kp.getViewedListFromEl(document);
const pages = kp.getPages();
kp.fetchPages(pages)
.then(async all => {
const texts = await Promise.all(all.map(e => e.text()));
texts.forEach((text, i) => {
let a = document.createElement('a');
a.innerHTML = text;
const sublist = kp.getViewedListFromEl(a);
list = list.concat(sublist);
});
return list;
})
.then(list => kp.aofo2csv(list))
.then(text => {
const name = ('kinoposk ' + ('' + new Date()).split('(').shift()).replace(/\s/ig, '-').slice(0, -1) + '.csv';
kp.text2file(text, name, 'text/csv')
});
}
kp.echo = (text) => {
let e = document.querySelector('.export-csv');
e && (e.innerText = 'Export CSV: ' + text);
}
kp.start = () => {
let e = document.querySelector('.js-rum-hero').nextElementSibling.nextElementSibling;
e && e.classList.remove('clear');
e && e.classList.add('export-csv');
e && (e.innerText = 'Export CSV');
e.addEventListener('click', kp.downloadViewed);
}
kp.start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment