|
1 | 1 | import base64
|
| 2 | +import copy |
| 3 | +import hashlib |
| 4 | +import mimetypes |
2 | 5 | import hmac
|
| 6 | +import os |
3 | 7 | import time
|
4 | 8 | import urllib.parse
|
5 | 9 |
|
@@ -47,13 +51,93 @@ def new_tweets(bot):
|
47 | 51 | config.state.tweet_ids[account] = tweets[0]['id']
|
48 | 52 | config.state.save()
|
49 | 53 |
|
| 54 | +def post(bot): |
| 55 | + rs = requests.Session() |
| 56 | + message_id = '316960031314804737' |
| 57 | + channel = config.bot.twitter_post['channel'] |
| 58 | + message = bot.get('/channels/%s/messages/%s' % (channel, message_id)) |
| 59 | + media_ids = [] |
| 60 | + upload_url = 'https://upload.twitter.com/1.1/media/upload.json' |
| 61 | + for attachment in message['attachments'][:4]: |
| 62 | + media = rs.get(attachment['url']).content |
| 63 | + |
| 64 | + media_type, _ = mimetypes.guess_type(attachment['filename']) |
| 65 | + rs = requests.Session() |
| 66 | + response = signed_request(rs, 'POST', upload_url, params={ |
| 67 | + 'command': 'INIT', |
| 68 | + 'media_type': media_type, |
| 69 | + 'total_bytes': str(attachment['size']), |
| 70 | + }) |
| 71 | + media_id = response.json()['media_id_string'] |
| 72 | + |
| 73 | + response = signed_request(rs, 'POST', upload_url, params={ |
| 74 | + 'command': 'APPEND', |
| 75 | + 'media_id': media_id, |
| 76 | + 'segment_index': '0', |
| 77 | + }, files={'media': media}) |
| 78 | + |
| 79 | + response = signed_request(rs, 'POST', upload_url, params={ |
| 80 | + 'command': 'FINALIZE', |
| 81 | + 'media_id': media_id, |
| 82 | + }) |
| 83 | + |
| 84 | + media_ids.append(media_id) |
| 85 | + |
| 86 | + discord_link = 'https://discord.com/channels/%s/%s/%s' % ( |
| 87 | + config.bot.twitter_post['server'], config.bot.twitter_post['channel'], message_id) |
| 88 | + tweet = '%s on %s\n%s' % (message['author']['username'], message['timestamp'][:10], discord_link) |
| 89 | + response = signed_request(rs, 'POST', 'https://api.twitter.com/1.1/statuses/update.json', { |
| 90 | + 'status': tweet, |
| 91 | + 'media_ids': ','.join(media_ids), |
| 92 | + 'trim_user': '1', |
| 93 | + }, {}) |
| 94 | + |
50 | 95 | def tweet_id_to_ts(tweet_id):
|
51 | 96 | # https://github.com/client9/snowflake2time#snowflake-layout
|
52 | 97 | return (tweet_id & 0x7fffffffffffffff) >> 22
|
53 | 98 |
|
54 |
| -def sign(method, url, params, consumer_secret, token_secret): |
| 99 | +def signed_request(rs, method, url, params=None, data=None, files=None): |
| 100 | + signing_params = { |
| 101 | + 'oauth_consumer_key': config.bot.twitter_post['consumer_key'], |
| 102 | + 'oauth_nonce': base64.b64encode(os.getrandom(16)), |
| 103 | + 'oauth_signature_method': 'HMAC-SHA1', |
| 104 | + 'oauth_timestamp': str(int(time.time())), |
| 105 | + 'oauth_version': '1.0', |
| 106 | + 'oauth_token': config.bot.twitter_post['token'], |
| 107 | + } |
| 108 | + final_data = None |
| 109 | + headers = {} |
| 110 | + if files: |
| 111 | + # https://github.com/oauthlib/oauthlib/blob/bda81b3cb6306dec19a6e60113e21b2933d0950c/oauthlib/oauth1/rfc5849/__init__.py#L212 |
| 112 | + request = requests.Request(method, url, data=data, files=files).prepare() |
| 113 | + signing_params['oauth_body_hash'] = base64.b64encode(hashlib.sha1(request.body).digest()) |
| 114 | + final_data = request.body |
| 115 | + headers['Content-Type'] = request.headers['Content-Type'] |
| 116 | + auth_params = copy.copy(signing_params) |
| 117 | + if params: |
| 118 | + signing_params.update(params) |
| 119 | + if data: |
| 120 | + signing_params.update(data) |
| 121 | + if not files: |
| 122 | + final_data = data |
| 123 | + |
| 124 | + consumer_secret = config.bot.twitter_post['consumer_secret'] |
| 125 | + token_secret = config.bot.twitter_post['token_secret'] |
| 126 | + auth_params['oauth_signature'] = sign(method, url, |
| 127 | + signing_params, consumer_secret, token_secret) |
| 128 | + # https://developer.twitter.com/en/docs/authentication/oauth-1-0a/authorizing-a-request |
| 129 | + auth = 'OAuth ' |
| 130 | + auth += ', '.join('%s="%s"' % (k, urllib.parse.quote(v)) for k, v in auth_params.items()) |
| 131 | + headers['Authorization'] = auth |
| 132 | + response = rs.request(method, url, params=params, data=final_data, headers=headers) |
| 133 | + response.raise_for_status() |
| 134 | + return response |
| 135 | + |
| 136 | +def sign(method, url, signing_params, consumer_secret, token_secret): |
55 | 137 | # https://developer.twitter.com/en/docs/authentication/oauth-1-0a/creating-a-signature
|
56 |
| - parameter_string = urllib.parse.urlencode(sorted(params.items()), quote_via=urllib.parse.quote) |
| 138 | + # https://github.com/oauthlib/oauthlib/blob/d54965b86ce4ede956db70baff0b3d5e9182a007/oauthlib/oauth1/rfc5849/utils.py#L52 |
| 139 | + parameter_string = urllib.parse.urlencode(sorted(signing_params.items()), |
| 140 | + quote_via=urllib.parse.quote, safe='~') |
57 | 141 | # that's right! we re-quote the parameter string
|
58 | 142 | encoded_params = urllib.parse.quote_plus(parameter_string)
|
59 | 143 | base_string = '%s&%s&%s' % (method, urllib.parse.quote_plus(url), encoded_params)
|
|
0 commit comments