var puppeteer=require('puppeteer');
var axios=require('axios');
var sd = require('silly-datetime');
async function login(page){
const login_url='https://passport.weibo.cn/signin/login?entry=mweibo&res=wel&wm=3349&r=http%3A%2F%2Fm.weibo.cn%2F'
const USERNAME='123123'
const PASSWORD='123123'
const USERNAME_SELECTOR='#loginName'
const PASSWORD_SELECTOR='#loginPassword'
const SUBMIT_SELECTOR="#loginAction"
await page.goto(login_url);
adjust(page,500);
await page.waitFor(USERNAME_SELECTOR,{visible:true})
await page.waitFor(PASSWORD_SELECTOR,{visible:true})
await page.waitFor(SUBMIT_SELECTOR,{visible:true})
await console.log('[*] Typing username ...');
await page.type(USERNAME_SELECTOR,USERNAME);
await console.log('[*] Typing password ...');
await page.type(PASSWORD_SELECTOR,PASSWORD);
await page.click(SUBMIT_SELECTOR);
await console.log('[*] submitting ...');
await page.waitForNavigation()
}
async function adjust(page,delay){
var y=0,tmp=1;
await page.keyboard.press('Tab')
await page.keyboard.down('Alt')
while(y!=tmp){
tmp=y
await page.keyboard.press('ArrowDown',{delay:delay})
y=await page.evaluate(()=>{return document.documentElement.getBoundingClientRect()['top']})
}
await page.keyboard.up('Alt')
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function getfeed(page){
//await page.goto("https://m.weibo.cn/");
const NEXTPAGE = "div.line-around.layout-box.mod-pagination > a.btn.box-col.line-left"
while(1 == 1){
await sleep(3000)
await adjust(page,700)
await page.waitFor(NEXTPAGE,{visible:true,timeout:0})
await console.log("[*] Starting to get contents")
let mcontents = await page.evaluate(()=>{return [].map.call(document.querySelectorAll(".weibo-detail"),(item)=>item.innerText)})
for (let i = 1; i < mcontents.length; i++) {
await console.log(mcontents[i]);
}
await console.log("[*] get next contents")
await page.click(NEXTPAGE);
}
//await adjust(page,700).catch(function(x){console.log(x)})
//await page.waitFor(NEXTPAGE,{visible:true,timeout:5000000})
}
async function post_message(page){
const main_url='https://m.weibo.cn/compose';
const TEXTSELECTOR = "#app > div.m-wrapper.m-wbox > div > main > div.m-box-model.m-pos-r > div > span > textarea:nth-child(1)";
var main_content = sd.format(new Date(), 'YYYY-MM-DD HH:mm');
await page.goto(main_url);
main_content = sd.format(new Date(), 'YYYY-MM-DD HH:mm');
await page.waitFor(TEXTSELECTOR,{visible:true})
await page.type(TEXTSELECTOR,main_content);
const BTN = "#app > div.m-wrapper.m-wbox > div > header > div.m-box.m-flex-grow1.m-box-model.m-fd-row.m-aln-center.m-justify-end.m-flex-base0 > a";
await page.waitFor(BTN,{visible:true})
await page.click(BTN);
await page.waitForNavigation();
// axios.post('http://m.weibo.cn/api/statuses/update',data).catch(function(e){console.log('1:'+e)})
}
(async () => {
// -- browser init start -----
console.info('[\033[1m\033[96m*\033[0m] Initing Browser environment ...')
console.time('[\033[93m*\033[0m] Init use ')
//const browser = await puppeteer.launch({headless:false,timeout:0,args:['--proxy-server=127.0.0.1:8080']});
const browser = await puppeteer.launch({headless:false,timeout:0});
const page=await browser.newPage();
await console.timeEnd('[\033[93m*\033[0m] Init use ')
page.on('dialog',async dialog => {await dialog.dismiss();});
// -- browser init end -----
await page.setDefaultNavigationTimeout(0)
await console.log('[*] Opening m.weibo.com ...')
await login(page).catch(function(x){console.log(x)})
await console.log('[*] done.')
await console.log('[*] Getting Data from m.weibo.com ...')
const first_url = "https://m.weibo.cn/"
//await page.goto(first_url);
//await getfeed(page).catch(function(x){console.log(x)})
await console.log('[*] Posting message to m.weibo.com ...')
await post_message(page).catch(function(x){console.log(x)})
await console.log('[*] done.')
await page.close()
await browser.close()
})()