Skip to content
This repository has been archived by the owner on Oct 1, 2023. It is now read-only.

Commit

Permalink
Added more API calls (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
Danie1 authored Jul 19, 2023
1 parent f419c21 commit 6b64f68
Show file tree
Hide file tree
Showing 10 changed files with 236 additions and 60 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ jobs:
python -m poetry run python -m flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
- name: Run tests
run: |
python -m poetry run python -m pytest -sxv
python -m poetry run python -m pytest -sxv --disable-pytest-warnings
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ It allows you to configure the session object. Choose between:
* `instagrapi` - utilize the same connection all the way for private api
* (Advanced) Implement your own and call ThreadsAPI like this: `ThreadsAPI(http_session_class=YourOwnHTTPSessionImpl)`

> **Note** Since v1.1.3 we are using ```instagrapi``` package to login.
> **Note** Since v1.1.10 you can use `requests` or `instagrapi` as HTTP clients, not just `aiohttp`.
> **Note** Since v1.1.12 a `.session.json` file will be created by-default to save default settings (to reduce risk of being flagged). You can disable it by passing `ThreadsAPI(settings_path=None)`
Expand Down Expand Up @@ -149,15 +147,16 @@ source env/bin/activate # Unix/MacOS
pip install -r requirements.txt
```


# Supported Features
- [x] ✅ Login functionality, including 2FA 🔒
- [x] ✅ Cache login token securely (reduce login requests / due to restrictive limits)
- [x] ✅ Saves settings locally, such as device information and timezone to use along your sessions
- [x] ✅ Read recommended posts from timeline (Requires Login 🔒)
- [x] ✅ Write Posts (Requires Login 🔒)
- [x] ✅ Posts with just text
- [x] ✅ Posts and quote another post
- [x] ✅ Posts with text and an image
- [x] ✅ Posts with text and multiple images
- [x] ✅ Posts with text that shares a url
- [x] ✅ Repost a post
- [x] ✅ Reply to Posts
Expand All @@ -174,18 +173,24 @@ pip install -r requirements.txt
- [x] ✅ Unrestrict User
- [x] ✅ Mute User
- [x] ✅ Unmute User
- [x] ✅ Search for users
- [x] ✅ Get Recommended Users
- [x] ✅ Get Notifications (`replies` / `mentions` / `verified`)
- [x] ✅ Read a user's followers list
- [x] ✅ Read a user's following list
- [x] ✅ Read Public Data
- [x] ✅ Read a user_id (eg. `314216`) via username(eg. `zuck`)
- [x] ✅ Read a user's profile info
- [x] ✅ Read list of a user's Threads
- [x] ✅ Read list of a user's Replies
- [x] ✅ Read Post and a list of its Replies
- [x] ✅ View who liked a post
- [x] ✅ Read Private Data (Requires Login 🔒)
- [x] ✅ Read a user's followers list
- [x] ✅ Read a user's following list
- [x] ✅ CI/CD
- [x] ✅ GitHub Actions Pipeline
- [x] ✅ HTTP Clients
- [x] ✅ AioHTTP
- [x] ✅ Requests
- [x] ✅ Instagrapi

## Usage Examples
View [examples/public_api_examples.py](https://github.com/Danie1/threads-api/blob/main/examples/public_api_examples.py) for Public API code examples. For the Private API usage (requires login), head over to [examples/private_api_examples.py](https://github.com/Danie1/threads-api/blob/main/examples/private_api_examples.py)
Expand Down Expand Up @@ -469,7 +474,6 @@ Post has been successfully posted
</details>

## 📌 Roadmap
- [ ] 🚧 Upload multiple images at once to a post
- [ ] 🚧 Post text and share a video
- [ ] 🚧 Implement all public API functions with private API, including pagination
- [ ] 🚧 Documentation Improvements
Expand Down
49 changes: 42 additions & 7 deletions examples/private_api_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ async def post_include_image(api):
else:
print("Unable to post.")



# Asynchronously posts a message with an image
async def post_include_image_from_url(api):
result = await api.post("Hello World with an image!", image_path="https://upload.wikimedia.org/wikipedia/commons/thumb/4/47/PNG_transparency_demonstration_1.png/280px-PNG_transparency_demonstration_1.png")
Expand All @@ -71,7 +69,17 @@ async def post_include_image_from_url(api):
print("Post has been successfully posted")
else:
print("Unable to post.")


# Asynchronously posts a message with an image
async def post_include_multiple_images(api):
result = await api.post("Hello World with an image!", image_path=[".github/logo.jpg",
"https://upload.wikimedia.org/wikipedia/commons/b/b5/Baby.tux.sit-black-800x800.png",
"https://upload.wikimedia.org/wikipedia/commons/thumb/4/47/PNG_transparency_demonstration_1.png/280px-PNG_transparency_demonstration_1.png"])

if result:
print("Post has been successfully posted")
else:
print("Unable to post.")


# Asynchronously posts a message with a URL
Expand All @@ -84,8 +92,6 @@ async def post_include_url(api):
else:
print("Unable to post.")



# Asynchronously follows a user
async def follow_user(api):
username_to_follow = "zuck"
Expand Down Expand Up @@ -257,14 +263,39 @@ async def get_post_likes(api : ThreadsAPI):
for user_info in likes['users'][:number_of_likes_to_display]:
print(f'Username: {user_info["username"]} || Full Name: {user_info["full_name"]}')

# Asynchronously gets the likes for a post
# Asynchronously reposts and deletes the repost
async def repost_and_delete(api : ThreadsAPI):
post_url = "https://www.threads.net/t/Cu0BgHESnwF"

post_id = await api.get_post_id_from_url(post_url)
await api.repost(post_id)
await api.delete_repost(post_id)


# Asynchronously search users by query
async def search_user(api : ThreadsAPI):
res = await api.search_user(query="zuck")

index = 1
for user in res['users']:
print(f"#{index} -> Username:[{user['username']}] Followers: [{user['follower_count']}] Following: [{user['following_count']}]")
index += 1

# Asynchronously gets a list of recommended users
async def get_recommended_users(api : ThreadsAPI):
res = await api.get_recommended_users()

index = 1
for user in res['users']:
print(f"#{index} -> Username:[{user['username']}] Full Name: [{user['full_name']}] Followers: [{user['follower_count']}]")
index += 1

# Asynchronously gets a list of recommended users
async def get_notifications(api : ThreadsAPI):
res = await api.get_notifications()

print(res)


async def main():
supported_http_session_classes = [AioHTTPSession, RequestsSession, InstagrapiSession]

Expand All @@ -282,6 +313,7 @@ async def main():
#await post_and_quote(api) # Post a message and quote another post
#await post_include_image(api) # Posts a message with an image.
#await post_include_image_from_url(api) # Posts a message with an image.
#await post_include_multiple_images(api) # Post with multiple images
#await post_include_url(api) # Posts a message with a URL.
#await follow_user(api) # Follows a user.
#await unfollow_user(api) # Unfollows a user.
Expand All @@ -297,6 +329,9 @@ async def main():
#await get_post(api) # Retrieves a post and its associated replies.
#await get_post_likes(api) # Get likers of a post
#await repost_and_delete(api) # Repost a post and delete it immediately
#await search_user(api) # Search for users by query
#await get_recommended_users(api) # Get a list of recommended users
#await get_notifications(api) # Get a list of notifications

await api.close_gracefully()

Expand Down
52 changes: 51 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ instagrapi = "^1.17.13"
pillow = "^10.0.0"
colorama = "^0.4.6"
pytest-mock = "^3.11.1"
flake8 = "^6.0.0"
pytz = "^2023.3"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down
Loading

0 comments on commit 6b64f68

Please sign in to comment.