ahmdrz / goinsta

Unofficial Instagram API written in Golang
MIT License
895 stars 248 forks source link

Uploading Videos #230

Open LoganHenderson opened 5 years ago

LoganHenderson commented 5 years ago

Going through the code the only upload methods I see are func (insta *Instagram) UploadPhoto(photo io.Reader, photoCaption string, quality int, filterType int) (Item, error) Is there an issue with supporting video uploads?

I've noticed similar python libraries offer video upload support.

ahmdrz commented 5 years ago

It's not implemented in this library. Implementing it is not a problem, but there's not enough time for it. But as you said, you can find Python and PHP versions of it.

arjunmahishi commented 5 years ago

Hey

I need this feature. I am willing to work on this if you can point me in the right direction. I had a look at the php code. It was greek and latin to me.

LoganHenderson commented 5 years ago

@arjunmahishi I am willing to work on this issue as well.

I will grab reference to python implementation of video uploads, post here, and try to get something started.

arjunmahishi commented 5 years ago

Sure... It doesn't look impossible, we can do this

arjunmahishi commented 5 years ago
def uploadVideo(self, video, thumbnail, caption=None, upload_id=None, is_sidecar=None):
        if upload_id is None:
            upload_id = str(int(time.time() * 1000))
        data = {'upload_id': upload_id,
                '_csrftoken': self.token,
                'media_type': '2',
                '_uuid': self.uuid}
        if is_sidecar:
            data['is_sidecar'] = '1'
        m = MultipartEncoder(data, boundary=self.uuid)
        self.s.headers.update({'X-IG-Capabilities': '3Q4=',
                               'X-IG-Connection-Type': 'WIFI',
                               'Host': 'i.instagram.com',
                               'Cookie2': '$Version=1',
                               'Accept-Language': 'en-US',
                               'Accept-Encoding': 'gzip, deflate',
                               'Content-type': m.content_type,
                               'Connection': 'keep-alive',
                               'User-Agent': self.USER_AGENT})
        response = self.s.post(self.API_URL + "upload/video/", data=m.to_string())
        if response.status_code == 200:
            body = json.loads(response.text)
            upload_url = body['video_upload_urls'][3]['url']
            upload_job = body['video_upload_urls'][3]['job']

            videoData = open(video, 'rb').read()
            # solve issue #85 TypeError: slice indices must be integers or None or have an __index__ method
            request_size = int(math.floor(len(videoData) / 4))
            lastRequestExtra = (len(videoData) - (request_size * 3))

            headers = copy.deepcopy(self.s.headers)
            self.s.headers.update({'X-IG-Capabilities': '3Q4=',
                                   'X-IG-Connection-Type': 'WIFI',
                                   'Cookie2': '$Version=1',
                                   'Accept-Language': 'en-US',
                                   'Accept-Encoding': 'gzip, deflate',
                                   'Content-type': 'application/octet-stream',
                                   'Session-ID': upload_id,
                                   'Connection': 'keep-alive',
                                   'Content-Disposition': 'attachment; filename="video.mov"',
                                   'job': upload_job,
                                   'Host': 'upload.instagram.com',
                                   'User-Agent': self.USER_AGENT})
            for i in range(0, 4):
                start = i * request_size
                if i == 3:
                    end = i * request_size + lastRequestExtra
                else:
                    end = (i + 1) * request_size
                length = lastRequestExtra if i == 3 else request_size
                content_range = "bytes {start}-{end}/{lenVideo}".format(start=start, end=(end - 1),
                                                                        lenVideo=len(videoData)).encode('utf-8')

                self.s.headers.update({'Content-Length': str(end - start), 'Content-Range': content_range, })
                response = self.s.post(upload_url, data=videoData[start:start + length])
            self.s.headers = headers

            if response.status_code == 200:
                if self.configureVideo(upload_id, video, thumbnail, caption):
                    self.expose()
        return False
arjunmahishi commented 5 years ago

@LoganHenderson The only difference is, we need to upload a thumbnail also right?

ahmdrz commented 5 years ago

I think it's better to use PHP code as your reference. Thank you @LoganHenderson and @arjunmahishi. It will be one of the best features in this library.

arjunmahishi commented 5 years ago

@ahmdrz Do you have any docs for the Instagram rest API that you are using?

ahmdrz commented 5 years ago

It's a private API. So I think only Instagram developers have it. The best documents are PHP code.