1. var puppeteer=require('puppeteer');
    2. var axios=require('axios');
    3. var sd = require('silly-datetime');
    4. async function login(page){
    5. const login_url='https://passport.weibo.cn/signin/login?entry=mweibo&res=wel&wm=3349&r=http%3A%2F%2Fm.weibo.cn%2F'
    6. const USERNAME='123123'
    7. const PASSWORD='123123'
    8. const USERNAME_SELECTOR='#loginName'
    9. const PASSWORD_SELECTOR='#loginPassword'
    10. const SUBMIT_SELECTOR="#loginAction"
    11. await page.goto(login_url);
    12. adjust(page,500);
    13. await page.waitFor(USERNAME_SELECTOR,{visible:true})
    14. await page.waitFor(PASSWORD_SELECTOR,{visible:true})
    15. await page.waitFor(SUBMIT_SELECTOR,{visible:true})
    16. await console.log('[*] Typing username ...');
    17. await page.type(USERNAME_SELECTOR,USERNAME);
    18. await console.log('[*] Typing password ...');
    19. await page.type(PASSWORD_SELECTOR,PASSWORD);
    20. await page.click(SUBMIT_SELECTOR);
    21. await console.log('[*] submitting ...');
    22. await page.waitForNavigation()
    23. }
    24. async function adjust(page,delay){
    25. var y=0,tmp=1;
    26. await page.keyboard.press('Tab')
    27. await page.keyboard.down('Alt')
    28. while(y!=tmp){
    29. tmp=y
    30. await page.keyboard.press('ArrowDown',{delay:delay})
    31. y=await page.evaluate(()=>{return document.documentElement.getBoundingClientRect()['top']})
    32. }
    33. await page.keyboard.up('Alt')
    34. }
    35. function sleep(ms) {
    36. return new Promise(resolve => setTimeout(resolve, ms));
    37. }
    38. async function getfeed(page){
    39. //await page.goto("https://m.weibo.cn/");
    40. const NEXTPAGE = "div.line-around.layout-box.mod-pagination > a.btn.box-col.line-left"
    41. while(1 == 1){
    42. await sleep(3000)
    43. await adjust(page,700)
    44. await page.waitFor(NEXTPAGE,{visible:true,timeout:0})
    45. await console.log("[*] Starting to get contents")
    46. let mcontents = await page.evaluate(()=>{return [].map.call(document.querySelectorAll(".weibo-detail"),(item)=>item.innerText)})
    47. for (let i = 1; i < mcontents.length; i++) {
    48. await console.log(mcontents[i]);
    49. }
    50. await console.log("[*] get next contents")
    51. await page.click(NEXTPAGE);
    52. }
    53. //await adjust(page,700).catch(function(x){console.log(x)})
    54. //await page.waitFor(NEXTPAGE,{visible:true,timeout:5000000})
    55. }
    56. async function post_message(page){
    57. const main_url='https://m.weibo.cn/compose';
    58. const TEXTSELECTOR = "#app > div.m-wrapper.m-wbox > div > main > div.m-box-model.m-pos-r > div > span > textarea:nth-child(1)";
    59. var main_content = sd.format(new Date(), 'YYYY-MM-DD HH:mm');
    60. await page.goto(main_url);
    61. main_content = sd.format(new Date(), 'YYYY-MM-DD HH:mm');
    62. await page.waitFor(TEXTSELECTOR,{visible:true})
    63. await page.type(TEXTSELECTOR,main_content);
    64. 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";
    65. await page.waitFor(BTN,{visible:true})
    66. await page.click(BTN);
    67. await page.waitForNavigation();
    68. // axios.post('http://m.weibo.cn/api/statuses/update',data).catch(function(e){console.log('1:'+e)})
    69. }
    70. (async () => {
    71. // -- browser init start -----
    72. console.info('[\033[1m\033[96m*\033[0m] Initing Browser environment ...')
    73. console.time('[\033[93m*\033[0m] Init use ')
    74. //const browser = await puppeteer.launch({headless:false,timeout:0,args:['--proxy-server=127.0.0.1:8080']});
    75. const browser = await puppeteer.launch({headless:false,timeout:0});
    76. const page=await browser.newPage();
    77. await console.timeEnd('[\033[93m*\033[0m] Init use ')
    78. page.on('dialog',async dialog => {await dialog.dismiss();});
    79. // -- browser init end -----
    80. await page.setDefaultNavigationTimeout(0)
    81. await console.log('[*] Opening m.weibo.com ...')
    82. await login(page).catch(function(x){console.log(x)})
    83. await console.log('[*] done.')
    84. await console.log('[*] Getting Data from m.weibo.com ...')
    85. const first_url = "https://m.weibo.cn/"
    86. //await page.goto(first_url);
    87. //await getfeed(page).catch(function(x){console.log(x)})
    88. await console.log('[*] Posting message to m.weibo.com ...')
    89. await post_message(page).catch(function(x){console.log(x)})
    90. await console.log('[*] done.')
    91. await page.close()
    92. await browser.close()
    93. })()