Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
3.9 kB
3
Indexable
Never
    def upload_ava(self):

        file_path = f'{path_avatar}\\{str(random.randint(0, 4999))}.png'
        request_url = 'https://upload.twitter.com/1.1/media/upload.json'
        total_bytes = os.path.getsize(file_path)
        data = {'command': 'INIT',
                'total_bytes': total_bytes,
                'media_type': 'image/jpeg'}
        # print(self.headers)
        headers_ava = {
            'Authorization': 'Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA',
            'Authority': 'upload.twitter.com', 'Accept': '*/*', 'Origin': 'https://twitter.com',
            'Referer': 'https://twitter.com', 'Sec-Fetch-Site': 'same-site', 'Sec-Fetch-Mode': 'cors',
            'Sec-Fetch-Dest': 'empty', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'en-US,en;q=0.9',
            'X-Twitter-Client-Language': 'en', 'Sec-Ch-Ua-Mobile': '?0',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.5845.97 Safari/537.36',
            'X-Twitter-Active-User': 'yes',
            "X-Csrf-Token": self.cookies["ct0"]}
        self.session.headers.clear()
        response = self.session.post(url=request_url, data=data, headers=headers_ava)
        media_id = response.json()['media_id']

        with open(file_path, 'rb') as file:
            binary_media_data = file.read()
        payload = {
            'command': 'APPEND',
            'media_id': media_id,
            'segment_index': 0,
        }
        multipart_body = io.BytesIO()
        boundary = f"----WebKitFormBoundary{self.generate_random_string(16)}"

        def add_part(name, value):
            multipart_body.write(f'--{boundary}\r\n'.encode('utf-8'))
            multipart_body.write(f'Content-Disposition: form-data; name="{name}"\r\n'.encode('utf-8'))
            multipart_body.write(b'\r\n')
            multipart_body.write(value)
            multipart_body.write(b'\r\n')

        # Add form fields
        for key, value in payload.items():
            if isinstance(value, str):
                add_part(key, value.encode('utf-8'))
            else:
                add_part(key, str(value).encode('utf-8'))

        # Add binary media
        add_part('media', binary_media_data)

        multipart_body.write(f'--{boundary}--\r\n'.encode('utf-8'))

        # Set the Content-Type header with the boundary
        headers_ava['Content-Type'] = f'multipart/form-data; boundary={boundary}'

        # Send the POST request
        response = self.session.post(request_url, data=multipart_body.getvalue(), headers=headers_ava)

        # Handle the response as needed

        headers_ava.pop('Content-Type')
        data = {
            'command': 'FINALIZE',
            'media_id': media_id,
        }
        response = self.session.post(url=request_url, data=data, headers=headers_ava)

        update_url = 'https://api.twitter.com/1.1/account/update_profile_image.json'
        data = {"include_profile_interstitial_type": 1,
                "include_blocking": 1,
                "include_blocked_by": 1,
                "include_followed_by": 1,
                "include_want_retweets": 1,
                "include_mute_edge": 1,
                "include_can_dm": 1,
                "include_can_media_tag": 1,
                "include_ext_has_nft_avatar": 1,
                "include_ext_is_blue_verified": 1,
                "include_ext_verified_type": 1,
                "include_ext_profile_image_shape:": 1,
                "skip_status": 1,
                "return_user": 1,
                "media_id": media_id}

        response = self.session.post(url=update_url, data=data, headers=headers_ava)
        print(response.text)
Leave a Comment