Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Migrate to X v2 api for media upload #105

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ ci:
autoupdate_schedule: quarterly
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.9.1"
rev: "v0.9.7"
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
Expand Down
17 changes: 13 additions & 4 deletions src/iembot/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import twitter
from pyiem.reference import TWEET_CHARS
from pyiem.util import utc
from requests_oauthlib import OAuth1
from requests_oauthlib import OAuth1, OAuth1Session
from twisted.internet import reactor
from twisted.mail import smtp
from twisted.python import log
Expand Down Expand Up @@ -68,12 +68,17 @@
bot.tw_users[user_id]["access_token"],
bot.tw_users[user_id]["access_token_secret"],
)
oauth = OAuth1Session(

Check warning on line 71 in src/iembot/util.py

View check run for this annotation

Codecov / codecov/patch

src/iembot/util.py#L71

Added line #L71 was not covered by tests
bot.config["bot.twitter.consumerkey"],
bot.config["bot.twitter.consumersecret"],
bot.tw_users[user_id]["access_token"],
bot.tw_users[user_id]["access_token_secret"],
)
log.msg(
f"Tweeting {bot.tw_users[user_id]['screen_name']}({user_id}) "
f"'{twttxt}' media:{kwargs.get('twitter_media')}"
)
media = kwargs.get("twitter_media")
media_id = None

def _helper(params):
"""Wrap common stuff"""
Expand All @@ -92,9 +97,13 @@
}
# If we have media, we have some work to do!
if media is not None:
media_id = api.UploadMediaSimple(media)
payload = requests.get(media, timeout=30).content
res = oauth.post(

Check warning on line 101 in src/iembot/util.py

View check run for this annotation

Codecov / codecov/patch

src/iembot/util.py#L100-L101

Added lines #L100 - L101 were not covered by tests
"https://api.x.com/2/media/upload",
files={"media": (media, payload, "image/png")},
).json()
# string required
params["media"] = {"media_ids": [f"{media_id}"]}
params["media"] = {"media_ids": [f"{res['id']}"]}

Check warning on line 106 in src/iembot/util.py

View check run for this annotation

Codecov / codecov/patch

src/iembot/util.py#L106

Added line #L106 was not covered by tests
res = _helper(params)
except TwitterError as exp:
errcode = twittererror_exp_to_code(exp)
Expand Down