subzeroid / instagrapi

🔥 The fastest and powerful Python library for Instagram Private API 2024
https://hikerapi.com/p/bkXQlaVe
MIT License
4.38k stars 685 forks source link

[BUG] In mac #1706

Closed kanishkcse closed 11 months ago

kanishkcse commented 11 months ago

Describe the bug Input should be a valid datetime, dates after 9999 are not supported as unix timestamps [type=datetime_parsing, input_value='1702926042126901', input_type=str] For further information visit https://errors.pydantic.dev/2.5/v/datetime_parsing i am getting this error

To Reproduce from instagrapi import Client from datetime import datetime import time from pathlib import Path

cl = Client() USERNAME = "YourInstagramUsername" PASSWORD = "YourInstagramPassword" hastag = "programming"

Load session if available

cl.load_settings(Path('dump.json'))

Check the timeline feed

cl.get_timeline_feed()

time.sleep(3) second = cl.user_id_from_username("zara")

Read data from file and send direct messages

with open('volume1.txt', 'r') as file: for line in file: data = line.strip()

    # Format the current timestamp as a string
    timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

    print(data)
    time.sleep(1)

    # Send a direct message with the modified message text

    cl.direct_send(data, [second])

Traceback File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/instagrapi/mixins/direct.py", line 469, in direct_send return extract_direct_message(result["payload"]) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/instagrapi/extractors.py", line 333, in extract_direct_message return DirectMessage(**data) ^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pydantic/main.py", line 164, in init pydantic_self.pydantic_validator.validate_python(data, self_instance=__pydantic_self__) pydantic_core._pydantic_core.ValidationError: 1 validation error for DirectMessage timestamp Input should be a valid datetime, dates after 9999 are not supported as unix timestamps [type=datetime_parsing, input_value='1702926042126901', input_type=str] For further information visit https://errors.pydantic.dev/2.5/v/datetime_parsing i am getting this error

Expected behavior i want it to send line by line the message written on the txt file but it only sends one line and stops

Desktop (please complete the following information):

Additional context i entered correct username and password and save the setting and use it to login so there is no problem to login but the country in the json file is usa and i am in india

kanishkcse commented 11 months ago

Hey bro you are really great person here let me help you with the error the error is because the instagram is returning data object with the help of api and that api has big numbers like very big so now your function called extract_direct_message in extractors.py doesnot accept that type of input as date so modify that code to get this type of timestamp=datetime.datetime(2021, 8, 31, 18, 33, 5, 127298) or just replace it with this and it will work def extract_direct_message(data):

Extract the unique identifier

data["id"] = data.get("item_id")

# Handle replied messages, media shares, and other relevant data
if "replied_to_message" in data:
    data["reply"] = extract_reply_message(data["replied_to_message"])
if "media_share" in data:
    ms = data["media_share"]
    if not ms.get("code"):
        ms["code"] = InstagramIdCodec.encode(ms["id"])
    data["media_share"] = extract_media_v1(ms)
if "media" in data:
    data["media"] = extract_direct_media(data["media"])
if "voice_media" in data:
    if "media" in data["voice_media"]:
        data["media"] = extract_direct_media(data["voice_media"]["media"])
clip = data.get("clip", {})
if clip:
    if "clip" in clip:
        clip = clip.get("clip")
    data["clip"] = extract_media_v1(clip)
xma_media_share = data.get("xma_media_share", {})
if xma_media_share:
    data["xma_share"] = extract_media_v1_xma(xma_media_share[0])

# Handle timestamp conversion
timestamp = data.get("timestamp")
if timestamp:
    try:
        # Convert the timestamp to seconds from microseconds
        timestamp_seconds = int(timestamp) / 1_000_000

        # Convert the timestamp to a datetime object
        dt = datetime.fromtimestamp(timestamp_seconds)

        # Format the datetime object as a string
        data["timestamp"] = dt.strftime('%Y-%m-%d %H:%M:%S.%f')
    except ValueError:
        # If parsing fails, set the timestamp to None
        data["timestamp"] = None

return DirectMessage(**data)