I'm trying to automate the process of uploading videos/images on instagram (without using a private API). For now i automated the image uploading and i'm trying to do the same thing for the videos. I'm doing this with electron and Nodejs.
for click the upload button and select an image I execute this code that actually works fine.
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);
}
This code works fine for images .jpg. The problem that i'm facing is that during the uploading, when it opens the file selector for choose something to post it doesn't recognize the videos. I tried all the possible video extensions.
I also tried to write the file path in the file selector instead select it manually and I saw that if u write a non .jpg/.mp4 file it show a warning only images are allowed, instead, if you write the path to a .jpg file it uploads the image and if you write a file to .mp4 it closes the file manager and do nothing, like that it ignores that you are trying to upload something.
To reproduce
F12 for open the dev toolsCTRL + SHIFT + M for toggle the device emulation+ button.(The video is 6mb (< 15mb that is the maximum) and 40seconds (<60s that is the maximum)
If you want to publish a video via the API, you have to do the following two steps:
POST /{ig-user-id}/media endpointPOST /{ig-user-id}/media_publish
Example Request:
POST graph.facebook.com/89751400008469714/media
?media_type=VIDEO
&video_url=https//www.example.com/videos/my-video.mov
&caption=#myfirstvideo!
Try using a .mov file. If this doesn´t fix the problem, you can check if your video has the following characteristics:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With