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

Migrate to X v2 api for media upload #105

wants to merge 2 commits into from

Conversation

akrherz
Copy link
Owner

@akrherz akrherz commented Feb 26, 2025

Will sit on this until 25 March 2025 gets closer. Maybe an upstream library will better support this.

Copy link

deepsource-io bot commented Feb 26, 2025

Here's the code health analysis summary for commits 8e2a708..9f0fbbd. View details on DeepSource ↗.

Analysis Summary

AnalyzerStatusSummaryLink
DeepSource Shell LogoShell✅ SuccessView Check ↗
DeepSource Python LogoPython✅ SuccessView Check ↗

💡 If you’re a repository administrator, you can configure the quality gates from the settings.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @akrherz, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

Summary of Changes

This pull request addresses the migration to the X (formerly Twitter) v2 API for media uploads. The primary goal is to update the iembot utility to use the new API endpoints for uploading media when tweeting. The PR also includes a minor update to the ruff-pre-commit hook version.

Highlights

  • API Migration: The core change involves migrating from the older Twitter API to the X v2 API for media uploads, ensuring continued functionality as older APIs are deprecated.
  • Media Upload Implementation: The implementation uses requests_oauthlib to handle the OAuth1 authentication and uploads media files to the new X API endpoint.
  • Dependency Update: The pull request updates the ruff-pre-commit hook to version v0.9.7 from v0.9.1.

Changelog

  • .pre-commit-config.yaml
    • Updated ruff-pre-commit hook version from v0.9.1 to v0.9.7.
  • src/iembot/util.py
    • Imported OAuth1Session from requests_oauthlib.
    • Initialized OAuth1Session with consumer key, consumer secret, access token, and access token secret on line 71.
    • Replaced api.UploadMediaSimple with a direct POST request to the X v2 media upload endpoint (https://api.x.com/2/media/upload) on line 101.
    • Used requests.get to fetch the media content before uploading on line 100.
    • Modified the media upload logic to include the fetched media content as a file in the POST request on line 103.
    • Updated the media_ids parameter to use the ID returned by the X v2 API on line 106.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.


Did you know?

The 'X' logo is part of the Unicode character set as U+1D4E7 (Mathematical Italic Small X).

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request migrates to the X v2 API for media upload, which is a necessary update. The changes are generally well-structured, but there are a few areas where improvements can be made to enhance efficiency and robustness. Please ensure that the updated Twitter API integration is thoroughly tested after addressing the comments.

Summary of Findings

  • OAuth1Session Usage: The OAuth1Session object is instantiated but the original OAuth1 object is still being used. Consider removing the OAuth1 object if it's no longer needed.
  • Media Upload: The media upload logic fetches the entire media content into memory. For larger files, this could lead to memory issues. Consider using a streaming approach.
  • Error Handling: The code catches TwitterError but doesn't handle potential exceptions during the media upload process (e.g., requests.exceptions.RequestException).

Assessment

The pull request updates the Ruff pre-commit hook and migrates the Twitter API usage to v2 for media uploads. The changes seem reasonable, but there are a few areas that could be improved for clarity and efficiency. Please address the comments before merging, and ensure that the updated Twitter API integration is thoroughly tested.

Copy link

codecov bot commented Feb 26, 2025

Codecov Report

Attention: Patch coverage is 20.00000% with 4 lines in your changes missing coverage. Please review.

Project coverage is 40.01%. Comparing base (8e2a708) to head (9f0fbbd).

Files with missing lines Patch % Lines
src/iembot/util.py 20.00% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #105      +/-   ##
==========================================
- Coverage   40.05%   40.01%   -0.04%     
==========================================
  Files           7        7              
  Lines        1191     1192       +1     
==========================================
  Hits          477      477              
- Misses        714      715       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant