我正在尝试自动化在 Instagram 上上传视频/图像的过程(不使用私有(private) 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);
}
此代码适用于图像
.jpg
.我面临的问题是,在上传过程中,当它打开文件选择器以选择要发布的内容时 它无法识别视频 .我尝试了所有可能的视频扩展。我还尝试在文件选择器中写入文件路径,而不是手动选择它,我看到如果你写一个非
.jpg
/.mp4
如果您将路径写入 .jpg
,它会显示仅允许使用图像的警告。 file 它会上传图像,如果您将文件写入 .mp4
它关闭文件管理器并且什么也不做,就像它忽略了你正在尝试上传的东西一样。转载
F12
用于打开开发工具 CTRL + SHIFT + M
用于切换设备仿真 +
上传内容按钮。 (视频为 6mb(< 15mb 为最大值)和 40 秒(<60s 为最大值)
最佳答案
如果您想通过 API 发布视频,您必须执行以下两个步骤:
POST /{ig-user-id}/media
创建媒体对象容器端点 POST /{ig-user-id}/media_publish
发布容器示例请求:
POST graph.facebook.com/89751400008469714/media
?media_type=VIDEO
&video_url=https//www.example.com/videos/my-video.mov
&caption=#myfirstvideo!
尝试使用 .mov 文件。如果这不能解决问题,您可以检查您的视频是否具有以下特征:
关于javascript - Instagram 从电脑上传视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59609003/