open-wa / wa-automate-python

💬 🤖 The most advanced Python whatsapp library for chatbots with advanced features. Be sure to ⭐ this repository for updates!
Other
349 stars 75 forks source link

get_all_groups() API not returning group meta data correctly #11

Closed manojsitapara closed 4 years ago

manojsitapara commented 4 years ago

I have WhatsApp account in which there are 45 groups. When I called get_all_groups() API, it return only top 12 groups groupMetadata, for rest all groupMetadata is empty.

ws_groups = whatsapp_driver.get_all_groups() for ws_group in ws_groups: group_owner = ws_group['groupMetadata']['owner']['_serialized']

e.g. group_owner = ws_group['groupMetadata']['owner']['_serialized'] become empty after 13th group comes in loop

Can anyone please help me how to resolve this issue?

mrodal commented 4 years ago

The latest version (1.1.2) provides driver.get_all_groups

Its working for me with 46 groups. Please update here if its working for you

manojsitapara commented 4 years ago

@mrodal Thanks !! I have taken latest code following are my observations

a) I think it does not have get_all_groups()

b) It still does not return owner information from groupmetadata after 15th group, Initial top 15 groups return all the details including groupmetadata, contact object but after 15th rest all objects are empty. I have attached the screenshot for your review.

image

mrodal commented 4 years ago

a. Did you update the package? with pip install --upgrade openwa b. I think this is done for optimization purposes. Metadata is very detailed information, you can do this to get the metadata on all the groups you want:

metadatas = [driver.get_chat_from_id(g.id)._js_obj['groupMetadata'] for g in groups]

manojsitapara commented 4 years ago

a. Did you update the package? with pip install --upgrade openwa

It was my bad somehow it didn't update correctly. It is coming up now.

b. I think this is done for optimization purposes. Metadata is very detailed information, you can do this to get the metadata on all the groups you want: metadatas = [driver.get_chat_from_id(g.id)._js_obj['groupMetadata'] for g in groups]

I don't know why it is problem in my system, even I checked on both firefox and chrome browser, both has same behavior(I know it will not issue, but just tried it)

Look at below image with code and debug output.

Untitled

manojsitapara commented 4 years ago

During debug I have observed that until ws_group object is not fully loaded, it does not return owner data. Once ws_group object is fully loaded, it returns all data.

But the point is that without debug how can we check in code that if object is fully loaded or not.

see following screenshot in which I have highlighted all three steps.

Untitled

mrodal commented 4 years ago

I found the issue, its because of whatsapp lazy loading groups metadata. Please try this function:

def get_all_owners(driver):
    for g in driver.get_all_groups():
        g.get_participants_ids()

    owners = [False]
    while next((True for o in owners if not o), None):
        time.sleep(3)
        owners = [g._js_obj['groupMetadata']['owner'] for g in driver.get_all_groups()]
    return owners

It takes something like 20 seconds for me with 45 groups

manojsitapara commented 4 years ago
def get_all_owners(driver):
    for g in driver.get_all_groups():
        g.get_participants_ids()

    owners = [False]
    while next((True for o in owners if not o), None):
        time.sleep(3)
        owners = [g._js_obj['groupMetadata']['owner'] for g in driver.get_all_groups()]
    return owners

Perfectly working !! Thank you @mrodal. I am closing this issue for now.

manojsitapara commented 4 years ago

@mrodal Just wanted to know that above code is works fine to get owner information but when I tried to get profile picture url in same way, why it gives only initial 14 group profile pictures? Is it different behavior for contact in lazy loading?

Just instead of this code owners = [g._js_obj['groupMetadata']['owner'] for g in driver.get_all_groups()]

I am using this to get profile pictures pictures = [g1._js_obj['contact']['profilePicThumbObj'] for g1 in g.driver.get_all_groups()

Can you please help me?