11"""
22WebWhatsAPI module
3+
34.. moduleauthor:: Mukul Hase <mukulhase@gmail.com>, Adarsh Sanjeev <adarshsanjeev@gmail.com>
45"""
56
2425from selenium .webdriver .support import expected_conditions as EC
2526from selenium .webdriver .support .ui import WebDriverWait
2627
27- from .objects .chat import UserChat , factory_chat
28+ from .objects .chat import Chat , UserChat , factory_chat
2829from .objects .contact import Contact
2930from .objects .message import MessageGroup , factory_message
3031from .wapi_js_wrapper import WapiJsWrapper
@@ -106,7 +107,7 @@ def set_local_storage(self, data):
106107 for k , v in data .items ()]))
107108
108109 def save_firefox_profile (self , remove_old = False ):
109- "Function to save the firefox profile to the permanant one"
110+ """ Function to save the firefox profile to the permanant one"" "
110111 self .logger .info ("Saving profile from %s to %s" % (self ._profile .path , self ._profile_path ))
111112
112113 if remove_old :
@@ -143,11 +144,12 @@ def set_proxy(self, proxy):
143144 self ._profile .set_preference ("network.proxy.ssl_port" , int (proxy_port ))
144145
145146 def close (self ):
147+ """Closes the selenium instance"""
146148 self .driver .close ()
147149
148150 def __init__ (self , client = "firefox" , username = "API" , proxy = None , command_executor = None , loadstyles = False ,
149151 profile = None , headless = False , autoconnect = True , logger = None , extra_params = None , chrome_options = None ):
150- "Initialises the webdriver"
152+ """ Initialises the webdriver"" "
151153
152154 self .logger = logger or self .logger
153155 extra_params = extra_params or {}
@@ -236,6 +238,7 @@ def connect(self):
236238
237239 def is_logged_in (self ):
238240 """Returns if user is logged. Can be used if non-block needed for wait_for_login"""
241+
239242 # self.driver.find_element_by_css_selector(self._SELECTORS['mainPage'])
240243 # it becomes ridiculously slow if the element is not found.
241244
@@ -274,6 +277,7 @@ def get_contacts(self):
274277 Fetches list of all contacts
275278 This will return chats with people from the address book only
276279 Use get_all_chats for all chats
280+
277281 :return: List of contacts
278282 :rtype: list[Contact]
279283 """
@@ -283,6 +287,7 @@ def get_contacts(self):
283287 def get_my_contacts (self ):
284288 """
285289 Fetches list of added contacts
290+
286291 :return: List of contacts
287292 :rtype: list[Contact]
288293 """
@@ -292,6 +297,7 @@ def get_my_contacts(self):
292297 def get_all_chats (self ):
293298 """
294299 Fetches all chats
300+
295301 :return: List of chats
296302 :rtype: list[Chat]
297303 """
@@ -300,6 +306,7 @@ def get_all_chats(self):
300306 def get_all_chat_ids (self ):
301307 """
302308 Fetches all chat ids
309+
303310 :return: List of chat ids
304311 :rtype: list[str]
305312 """
@@ -328,19 +335,18 @@ def get_unread(self, include_me=False, include_notifications=False, use_unread_c
328335
329336 return unread_messages
330337
331-
332338 def get_unread_messages_in_chat (self ,
333339 id ,
334340 include_me = False ,
335341 include_notifications = False ):
336342 """
337343 I fetch unread messages from an asked chat.
344+
338345 :param id: chat id
339346 :type id: str
340347 :param include_me: if user's messages are to be included
341348 :type include_me: bool
342- :param include_notifications: if events happening on chat are to be
343- included
349+ :param include_notifications: if events happening on chat are to be included
344350 :type include_notifications: bool
345351 :return: list of unread messages from asked chat
346352 :rtype: list
@@ -357,12 +363,13 @@ def get_unread_messages_in_chat(self,
357363
358364 # return them
359365 return unread
360- # get_unread_messages_in_chat()
361366
367+ # get_unread_messages_in_chat()
362368
363369 def get_all_messages_in_chat (self , chat , include_me = False , include_notifications = False ):
364370 """
365371 Fetches messages in chat
372+
366373 :param include_me: Include user's messages
367374 :type include_me: bool or None
368375 :param include_notifications: Include events happening on chat
@@ -374,13 +381,12 @@ def get_all_messages_in_chat(self, chat, include_me=False, include_notifications
374381
375382 messages = []
376383 for message in message_objs :
377- messages .append (factory_message (message , self ))
378-
379- return messages
384+ yield (factory_message (message , self ))
380385
381386 def get_all_message_ids_in_chat (self , chat , include_me = False , include_notifications = False ):
382387 """
383388 Fetches message ids in chat
389+
384390 :param include_me: Include user's messages
385391 :type include_me: bool or None
386392 :param include_notifications: Include events happening on chat
@@ -393,6 +399,9 @@ def get_all_message_ids_in_chat(self, chat, include_me=False, include_notificati
393399 def get_message_by_id (self , message_id ):
394400 """
395401 Fetch a message
402+
403+ :param message_id: Message ID
404+ :type message_id: str
396405 :return: Message or False
397406 :rtype: Message
398407 """
@@ -404,6 +413,14 @@ def get_message_by_id(self, message_id):
404413 return result
405414
406415 def get_contact_from_id (self , contact_id ):
416+ """
417+ Fetches a contact given its ID
418+
419+ :param contact_id: Contact ID
420+ :type contact_id: str
421+ :return: Contact or Error
422+ :rtype: Contact
423+ """
407424 contact = self .wapi_functions .getContact (contact_id )
408425
409426 if contact is None :
@@ -412,6 +429,14 @@ def get_contact_from_id(self, contact_id):
412429 return Contact (contact , self )
413430
414431 def get_chat_from_id (self , chat_id ):
432+ """
433+ Fetches a chat given its ID
434+
435+ :param chat_id: Chat ID
436+ :type chat_id: str
437+ :return: Chat or Error
438+ :rtype: Chat
439+ """
415440 chat = self .wapi_functions .getChatById (chat_id )
416441 if chat :
417442 return factory_chat (chat , self )
@@ -426,6 +451,7 @@ def get_chat_from_phone_number(self, number):
426451 +972-51-234-5678
427452 This function would receive:
428453 972512345678
454+
429455 :param number: Phone number
430456 :return: Chat
431457 :rtype: Chat
@@ -434,7 +460,7 @@ def get_chat_from_phone_number(self, number):
434460 if not isinstance (chat , UserChat ) or number not in chat .id :
435461 continue
436462 return chat
437-
463+
438464 self .create_chat_by_number (number )
439465 self .wait_for_login ()
440466 for chat in self .get_all_chats ():
@@ -447,6 +473,12 @@ def reload_qr(self):
447473 self .driver .find_element_by_css_selector (self ._SELECTORS ['qrCode' ]).click ()
448474
449475 def get_status (self ):
476+ """
477+ Returns status of the driver
478+
479+ :return: Status
480+ :rtype: WhatsAPIDriverStatus
481+ """
450482 if self .driver is None :
451483 return WhatsAPIDriverStatus .NotConnected
452484 if self .driver .session_id is None :
@@ -464,6 +496,12 @@ def get_status(self):
464496 return WhatsAPIDriverStatus .Unknown
465497
466498 def contact_get_common_groups (self , contact_id ):
499+ """
500+ Returns groups common between a user and the contact with given id.
501+
502+ :return: Contact or Error
503+ :rtype: Contact
504+ """
467505 for group in self .wapi_functions .getCommonGroups (contact_id ):
468506 yield factory_chat (group , self )
469507
@@ -475,15 +513,24 @@ def chat_send_message(self, chat_id, message):
475513 return result
476514
477515 def send_message_to_id (self , recipient , message ):
516+ """
517+ Send a message to a chat given its ID
518+
519+ :param recipient: Chat ID
520+ :type recipient: str
521+ :param message: Plain-text message to be sent.
522+ :type message: str
523+ """
478524 return self .wapi_functions .sendMessageToID (recipient , message )
479525
480526 def chat_send_seen (self , chat_id ):
481- return self .wapi_functions .sendSeen (chat_id )
527+ """
528+ Send a seen to a chat given its ID
482529
483- def chat_get_messages ( self , chat_id , include_me = False , include_notifications = False ):
484- message_objs = self . wapi_functions . getAllMessagesInChat ( chat_id , include_me , include_notifications )
485- for message in message_objs :
486- yield factory_message ( message , self )
530+ :param chat_id: Chat ID
531+ :type chat_id: str
532+ """
533+ return self . wapi_functions . sendSeen ( chat_id )
487534
488535 def chat_load_earlier_messages (self , chat_id ):
489536 self .wapi_functions .loadEarlierMessages (chat_id )
@@ -551,13 +598,15 @@ def mark_default_unread_messages(self):
551598 def get_battery_level (self ):
552599 """
553600 Check the battery level of device
601+
554602 :return: int: Battery level
555603 """
556604 return self .wapi_functions .getBatteryLevel ()
557605
558606 def leave_group (self , chat_id ):
559607 """
560608 Leave a group
609+
561610 :param chat_id: id of group
562611 :return:
563612 """
@@ -566,14 +615,15 @@ def leave_group(self, chat_id):
566615 def delete_chat (self , chat_id ):
567616 """
568617 Delete a chat
618+
569619 :param chat_id: id of chat
570620 :return:
571621 """
572622 return self .wapi_functions .deleteConversation (chat_id )
573623
574624 def quit (self ):
575625 self .driver .quit ()
576-
626+
577627 def create_chat_by_number (self , number ):
578- url = self ._URL + "/send?phone=" + number
628+ url = self ._URL + "/send?phone=" + number
579629 self .driver .get (url )
0 commit comments