Danie1 / threads-api

Unofficial Python API for Meta's Threads App
https://pypi.org/project/threads-api/
MIT License
110 stars 15 forks source link

get post id from url except #8

Closed kirilkindan closed 1 year ago

kirilkindan commented 1 year ago

/Users/a123/PycharmProjects/pythonProject/venv2/bin/python /Users/a123/PycharmProjects/pythonProject/thre/test.py /Users/a123/PycharmProjects/pythonProject/thre/test.py:19: DeprecationWarning: There is no current event loop loop = asyncio.get_event_loop() Traceback (most recent call last): File "/Users/a123/PycharmProjects/pythonProject/thre/test.py", line 20, in loop.run_until_complete(main()) File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete return future.result() ^^^^^^^^^^^^^^^ File "/Users/a123/PycharmProjects/pythonProject/thre/test.py", line 16, in main await get_post_id_from_url() File "/Users/a123/PycharmProjects/pythonProject/thre/test.py", line 12, in get_post_id_from_url post_id = await threads_api.get_post_id_from_url(post_url) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/a123/PycharmProjects/pythonProject/venv2/lib/python3.11/site-packages/threads_api/src/threads_api.py", line 344, in get_post_id_from_url return post_id.group(1) ^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'group'

Danie1 commented 1 year ago

Thanks for reporting this :)

Please help by answering below questions:

I am in the works of implementing proper logging and error handling, so it will be easier to troubleshoot these issues.

tanu360 commented 1 year ago

Thanks for reporting this :)

Please help by answering below questions:

  • Which version of threads-api are you using?
  • What is the post_url you entered? Is it valid?

I am in the works of implementing proper logging and error handling, so it will be easier to troubleshoot these issues.

You can use my way to get the Post Numeral ID from URL / Post code. It will be faster and does not include login or opening any Threads Page as parsing Source and finding value via Regex may not be such an effective way, and a small change from Threads can break it.

https://github.com/tanu360/threads-posts-helper

1Mr-Newton commented 1 year ago

I made a function to generate postid from url or shortcode, make it better:

def get_post_id(url_or_shortcode):
    if "https://" in url_or_shortcode and "/@" in url_or_shortcode:
        raise "the argument passed is not a valid post URL"
    elif "https://" in url_or_shortcode and "/t" in url_or_shortcode:
        shortcode = url_or_shortcode.split("/t/")[-1].split("/")[0]
    elif len(url_or_shortcode) == 11:
        shortcode = url_or_shortcode
    alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
    id = 0
    for char in shortcode:
        id = (id * 64) + alphabet.index(char)
    return str(id)
Danie1 commented 1 year ago

Fixed in v1.1.4. Thanks @1Mr-Newton for the implementation, I have used it and it's 🔥