Skip to content

Commit

Permalink
Merge pull request #145 from MerleLiuKun/feat-media
Browse files Browse the repository at this point in the history
Feat media upload
  • Loading branch information
MerleLiuKun authored Nov 21, 2023
2 parents 68a074d + 745cd9e commit d1cea37
Show file tree
Hide file tree
Showing 17 changed files with 556 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: Build and publish to pypi
uses: JRubics/poetry-publish@v1.8
uses: JRubics/poetry-publish@v1.17
with:
pypi_token: ${{ secrets.PYPI_TOKEN }}

Expand Down
27 changes: 10 additions & 17 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.7', '3.8', '3.9', '3.10', '3.11']
python-version: ['3.7', '3.8', '3.9', '3.10', '3.11', '3.12']
include:
- python-version: 3.8
- python-version: '3.10'
update-coverage: true

steps:
Expand All @@ -22,25 +22,18 @@ jobs:
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install Poetry
uses: snok/install-poetry@v1
with:
virtualenvs-create: true
virtualenvs-in-project: true
installer-parallel: true
- name: Load cached venv
id: cached-poetry-dependencies
- name: Cache pip
uses: actions/cache@v2
with:
path: .venv
path: ~/.cache/pip
key: ${{ matrix.python-version }}-poetry-${{ hashFiles('pyproject.toml') }}
- name: Install dependencies
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: poetry install --no-interaction --no-root
run: |
python -m pip install --upgrade pip poetry
poetry install
- name: Test with pytest
run: |
source .venv/bin/activate
pytest
poetry run pytest
- name: Upload coverage to Codecov
if: ${{ matrix.update-coverage }}
uses: codecov/codecov-action@v3
Expand All @@ -58,7 +51,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: 3.8
python-version: '3.10'
- name: Cache pip
uses: actions/cache@v2
with:
Expand All @@ -67,4 +60,4 @@ jobs:
- name: Install dependencies
run: python -m pip install --upgrade pip black
- name: Black test
run: black --check .
run: make lint-check
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ Now covers these features:
- Direct Messages lookup
- Manage Direct Messages

- Media Upload
- Media Simple upload
- Media Chunked upload

-----------
INSTANTIATE
-----------
Expand Down
69 changes: 69 additions & 0 deletions docs/docs/usage/media-upload/chunked-upload.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
Chunked upload is the recommended method for uploading media files. It allows you to upload media files up to 512MB. The chunked upload endpoint can be used to upload both images and videos.

You can get more information for this at [docs](https://developer.twitter.com/en/docs/twitter-api/v1/media/upload-media/api-reference/post-media-upload-init).

follow the steps below to upload a video by chunked upload:

### Step 1: Initialize the upload

```python
resp = my_api.upload_media_chunked_init(
total_bytes=1234561,
media_type="video/mp4",
)
media_id = resp.media_id_string
```

### Step 2: Append the file

Assume the file has split into 3 parts

```python
video_parts = [
"path/to/video/part1.mp4",
"path/to/video/part2.mp4",
"path/to/video/part3.mp4",
]

for idx, part in enumerate(video_parts):
with open(part, "rb") as media:
status = my_api.upload_media_chunked_append(
media_id=media_id,
media=media,
segment_index=idx,
)
print(status)
```

### Step 3: Finalize the upload

Once you have appended all the file parts, you need to finalize the upload.

```python
resp = my_api.upload_media_chunked_finalize(media_id=media_id)
print(resp)
```

### Step 4 (Optional): Check the status

If the finalize response show the video is processing, you can check the status by using the following code:

```python
resp = my_api.upload_media_chunked_status(media_id=media_id)
print(resp)
```

Note, only the media is in processing status, you can get the status.

### Step 5: Create a tweet with video

Once the processing is complete, you can use the `media_id` to create a tweet with video.

```python
my_api.create_tweet(
text="Hello World",
media_media_ids=[media_id],
)
```

Enjoy it!
23 changes: 23 additions & 0 deletions docs/docs/usage/media-upload/simple-upload.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
The simple upload endpoint can only be used to upload images(gifs).

You can get more information for this at [docs](https://developer.twitter.com/en/docs/twitter-api/v1/media/upload-media/api-reference/post-media-upload)

follow the steps below to upload an image:

```python

with open("path/to/image", "rb") as media:
resp = my_api.upload_media_simple(media=media)
print(resp)
```

also you can upload with base64 encoded image:

```python
import base64

with open("path/to/image", "rb") as f:
media_data = base64.b64encode(f.read()).decode("utf-8")
resp = my_api.upload_media_simple(media_data=media_data)
print(resp)
```
3 changes: 3 additions & 0 deletions docs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ nav:
- Authorization OAuth2.0: authorization_oauth2.md
- Usage:
- Preparation: usage/preparation.md
- Media Upload:
- Simple Upload: usage/media-upload/simple-upload.md
- Chunked Upload: usage/media-upload/chunked-upload.md
- Tweets:
- Tweet Lookup: usage/tweets/tweet-lookup.md
- Manage Tweets: usage/tweets/tweet-manage.md
Expand Down
Loading

0 comments on commit d1cea37

Please sign in to comment.