Instagram从PC上传视频

Mat*_*t.C 8 javascript node.js instagram electron

我正在尝试自动化在 Instagram 上上传视频/图像的过程(不使用私有 API)。现在我自动化了图像上传,我正在尝试为视频做同样的事情。我正在用electron和来做这件事Nodejs

单击上传按钮并选择一个图像我执行此代码,实际上工作正常。

const fs = require('fs'),
      {remote} = require('electron'),
      clipboardy    = require('clipboardy'),
      BrowserWindow = remote.BrowserWindow;

const LOAD_IMAGE = '.UP43G',
      NEW_POST = '.glyphsSpriteNew_post__outline__24__grey_9.u-__7';

function get_files(path){
    return fs.readdirSync(path, { withFileTypes: true })
    .filter(dirent => dirent.isFile())
    .map(dirent => __dirname + '/../../' + path + '/' + dirent.name);
}

function randomRange(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

function createWindow (session_id, hidden) {
    win = new BrowserWindow({
        width: 500,
        height: 500
    });
    win.loadURL('https://www.instagram.com');
    return win;
}

////select the files to upload////

var files = UPLOAD_POST_FOLDER_CUSTOM
var file_to_upload = files[randomRange(0, files.length - 1)];

///////////////////////////////////////

function async upload_image(){
    // click the upload button on the page
    await electron_window.webContents.executeJavaScript(`
        async function click_upload_button(){
            let new_post_button = document.querySelector('${NEW_POST}');
            await sleep(1000);
            new_post_button.click()
        }
        click_upload_button();
    `);
    // write the path of the file and press enter in the file selector
    await sleep(500);
    let previous_clipboard = clipboardy.readSync();
    clipboardy.writeSync(file_to_upload);
    await fake_input.keyTap('l', 'control');
    await fake_input.keyTap('v', 'control');
    await fake_input.keyTap('enter');
    clipboardy.writeSync(previous_clipboard);       
    await sleep(2000);

}

Run Code Online (Sandbox Code Playgroud)

此代码适用于图像.jpg。我面临的问题是,在上传过程中,当它打开文件选择器以选择要发布的内容时,它无法识别视频。我尝试了所有可能的视频扩展。

我还尝试在文件选择器中写入文件路径,而不是手动选择它,我看到如果你写一个非.jpg/.mp4文件,它会显示一个警告,只允许图像,相反,如果你写入.jpg文件的路径,它会上传图像如果您向.mp4其中写入文件,则会关闭文件管理器并且什么也不做,就像它忽略您正在尝试上传的内容一样。

繁殖

  • 去Instagram
  • 登录
  • 点击F12打开开发工具
  • 单击CTRL + SHIFT + M以切换设备仿真
  • 选择任何设备或调整页面大小以切换网站的移动视图
  • 重新加载站点
  • 尝试通过单击底部+按钮上传一些东西。

(视频为 6mb(< 15mb 是最大值)和 40 秒(<60s 是最大值)

Rom*_*man 0

我认为Instagram 私人 api 客户端会比你的方法做得更好