Skip to content

Commit

Permalink
added upload_photo() to Mobile.py
Browse files Browse the repository at this point in the history
  • Loading branch information
diezo committed Mar 2, 2024
1 parent 5c26a59 commit 9d906ed
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
62 changes: 58 additions & 4 deletions ensta/Mobile.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ def profile(self, identifier: str) -> Profile:
# Response actually returns two JSON Objects with profile information,
# but we'll only use the 1st one for now.

with open("test.json", "w", encoding="utf-8") as file: file.write(response.text)

information: dict = tuple(json.loads(data) for data in response.text.strip().split("\n"))[0]

if information.get("status", "") != "ok":
Expand Down Expand Up @@ -300,7 +302,6 @@ def direct(self) -> Direct:
def follow(self, identifier: str) -> bool:
"""
Follows a user
:param self: None
:param identifier: Username or UserID of target (UserID Recommended)
:return: Boolean (Followed or not)
"""
Expand Down Expand Up @@ -331,7 +332,6 @@ def follow(self, identifier: str) -> bool:
def unfollow(self, identifier: str) -> bool:
"""
Unfollows a user by user_id
:param self: None
:param identifier: Username or UserID of target (UserID Recommended)
:return: Boolean (Unfollowed or not)
"""
Expand Down Expand Up @@ -362,7 +362,6 @@ def unfollow(self, identifier: str) -> bool:
def block(self, identifier: str) -> bool:
"""
Blocks a user
:param self: None
:param identifier: Username or UserID of target (UserID Recommended)
:return: Boolean (Blocked or not)
"""
Expand Down Expand Up @@ -394,7 +393,6 @@ def block(self, identifier: str) -> bool:
def unblock(self, identifier: str) -> bool:
"""
Unblocks a user
:param self: None
:param identifier: Username or UserID of target (UserID Recommended)
:return: Boolean (Unblocked or not)
"""
Expand Down Expand Up @@ -809,3 +807,59 @@ def comment(self, text: str, media_id: str) -> AddedComment:
"If not, try using another account, switch "
"to a different network, or use reputed proxies."
)

# TODO: Implement Return Data Type
def upload_photo(self, upload_id: str, caption: str = "", alt_text: str = "", location_id: str = "") -> bool:
"""
Uploads a single photo post.
:param upload_id: Returned by get_upload_id(image_path)
:param caption: Caption text
:param alt_text: Custom Accessibility Caption Text
:param location_id: Facebook place ID of location
:return: Boolean (Success or not)
"""

body: dict = {
"_uid": self.user_id,
"device_id": self.device_id,
"_uuid": str(uuid4()),
"custom_accessibility_caption": alt_text,
"source_type": "4",
"caption": caption,
"upload_id": upload_id
}

# Add Location ID
if location_id != "": body["location"] = f"{{\"facebook_places_id\":\"{location_id}\"}}"

# Hit Endpoint
response: Response = self.session.post(
url=f"https://i.instagram.com/api/v1/media/configure/",
data={
"signed_body": "SIGNATURE." + json.dumps(body)
}
)

try:
response_dict: dict = response.json()

if response_dict.get("status", "") != "ok":

if f"NodeInvalidTypeException: Node backed by fbid {location_id}" in response_dict.get("message", ""):
raise NetworkError(
f"Location ID {location_id} doesn't exist on facebook places."
)

raise NetworkError(
"Unable to upload photo.\n"
f"Response: {response_dict}"
)

return True

except JSONDecodeError:
raise NetworkError(
"Unable to upload photo. Is the upload_id correct? Are all other configurations "
"correct? Are you using a valid image? Try using another account, switch "
"to a different network, or use reputed proxies."
)
1 change: 0 additions & 1 deletion mobile_migration_checklist.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
- update display name

- fb upload video
- upload single photo post
- upload carousel
- upload single reel

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from distutils.core import setup
from pathlib import Path

version = "5.2.1"
version = "5.2.2"
long_description = (Path(__file__).parent / "README.md").read_text(encoding="utf-8")

setup(
Expand Down

0 comments on commit 9d906ed

Please sign in to comment.