// http.createServer().listen('3000', () => console.log('Feeling good at 3000'))
async function test(){
return await getX()
}
async function getX(){
const
validate = await goto('https://hh.ru/'),
cookie = getCookie(validate),
loginPage = await goto('https://m.hh.ru/account/login', { headers: {cookie}}),
loginPageHtml = cheerio.load(await getHtml(loginPage)),
form = new FormData()
form.append('_xsrf', loginPageHtml('input[name=_xsrf]').val())
form.append('email', 'hr01@eurus.global')
form.append('password', 'Ab2K,;#a')
const login = await goto('https://m.hh.ru/account/login?backurl=%2F', {
headers: {
...loginPage.headers,
method: 'POST',
cookie: getCookie(loginPage),
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
body: form,
}
})
// console.log(xsrf, cookie, getCookie(login))
console.log(await getHtml(login))
// console.log(new FormData(loginPageHtml('[data-qa=account-login-form]')))
}
function getCookie(response){
return response.headers._headers['set-cookie']
}
function getRedirect(response){
return response.headers.get('location')
}
async function getHtml(response){
return await response.text()
}
async function goto(url, options = {}){
const
_url = url,
_options = options
return await fetchUrl()
async function fetchUrl(url = _url, options = _options){
const response = await fetch(url, {
method: 'GET',
headers,
credentials: 'include',
redirect: 'manual',
// mode: 'cors',
...options,
})
const
redirect = getRedirect(response),
cookie = getCookie(response)
if(response.status === 302 && redirect !== _url /*'https://hh.ru/'*/) {
// console.log('***********************************************');
// console.log(redirect + '\n' + response.status + '\n' + cookie)
// console.log('***********************************************');
return await fetchUrl(redirect, {
headers: { ...headers, cookie }
})
}
return await fetch(redirect, {
method: 'GET',
headers,
credentials: 'include',
redirect: 'manual',
// mode: 'cors',
...options,
})
// console.log('Final Result');
// console.log(resp.status);
}
}