Instagram 从电脑上传视频

huangapple go评论80阅读模式
英文:

Instagram upload video from pc

问题

我明白你的需求,以下是代码部分的中文翻译:

我正在尝试自动化上传Instagram上的视频/图片的过程不使用私有API)。目前我已经自动化了图片上传现在我正在尝试对视频做同样的事情我是使用 `electron`  `Nodejs` 来实现的

对于点击上传按钮并选择图片我执行以下代码它实际上运行良好

```javascript
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;
}

////选择要上传的文件////

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

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

async function upload_image(){
    // 点击页面上的上传按钮
    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();
    `);
    // 在文件选择器中写入文件路径并按回车键
    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 文件路径,它会上传该图片,如果写入 .mp4 文件路径,它会关闭文件管理器并什么都不做,就好像它忽略了你要上传的内容。

复现步骤

  • 转到Instagram网站
  • 登录
  • F12 打开开发工具
  • CTRL + SHIFT + M 切换设备模拟
  • 选择任何设备或调整页面大小以切换到移动视图
  • 重新加载网站
  • 尝试通过点击底部的 + 按钮上传内容。

(视频大小为6MB(小于15MB的最大值),持续时间为40秒(小于60秒的最大值))


请注意,这是对代码的翻译部分,如有需要,请继续使用这个翻译,或者如果需要其他帮助,请告诉我。
<details>
<summary>英文:</summary>
I&#39;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&#39;m trying to do the same thing for the videos. I&#39;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(&#39;${NEW_POST}&#39;);
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&#39;m facing is that during the uploading, when it opens the file selector for choose something to post __it doesn&#39;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**
- go to instagram
- do the login
- click `F12` for open the dev tools
- click ` CTRL + SHIFT + M` for toggle the device emulation
- select any device or resize the page for toggle the mobile view of the site
- reload the site
- try to upload something by clicking the bottom ` + ` button.
(The video is 6mb (&lt; 15mb that is the maximum) and 40seconds (&lt;60s that is the maximum)
</details>
# 答案1
**得分**: 1
如果您想通过API发布视频,需要执行以下两个步骤:
- 使用`POST /{ig-user-id}/media`端点创建一个媒体对象容器
- 使用`POST /{ig-user-id}/media_publish`发布容器
示例请求:
POST graph.facebook.com/89751400008469714/media
?media_type=VIDEO
&amp;video_url=https//www.example.com/videos/my-video.mov
&amp;caption=#myfirstvideo!
尝试使用**.mov**文件。如果这不解决问题,您可以检查您的视频是否具有以下特征:
- 视频比特率为3,500 kbps
- H.264编解码器 / MP4格式
- 3至60秒的持续时间
- 最大宽度为1080像素(1080P)
- 每秒29.96帧
- 44.1 kHz单声道的AAC音频编解码器
<details>
<summary>英文:</summary>
If you want to publish a video via the API, you have to do the following two steps:
- create a media object container by using the `POST /{ig-user-id}/media` endpoint
- publish the container with `POST /{ig-user-id}/media_publish`
Example Request:
POST graph.facebook.com/89751400008469714/media
?media_type=VIDEO
&amp;video_url=https//www.example.com/videos/my-video.mov
&amp;caption=#myfirstvideo!
Try using a **.mov** file. If this doesn&#180;t fix the problem, you can check if your video has the following characteristics:
- 3,500 kbps video bitrate
- H.264 codec / MP4 format
- 3 to 60 seconds duration
- maximum width 1080 pixels (1080P)
- 29.96 frames per second
- AAC audio codec at 44.1 kHz mono
</details>
# 答案2
**得分**: 0
我认为[instagram private api client][1]会比你的方法做得更好
[1]: https://github.com/dilame/instagram-private-api
<details>
<summary>英文:</summary>
I think [instagram private api client][1] will do better job than your approach
[1]: https://github.com/dilame/instagram-private-api
</details>

huangapple
  • 本文由 发表于 2020年1月6日 16:42:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609003.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定