Module contactsearch

Source Code for Module contactsearch

  1  print('****************************************************************************'); 
  2  print('SkypeKit Python Wrapper Tutorial: Contact search.'); 
  3  print('****************************************************************************'); 
  4  
 
  5  # This example demonstrates, how to:
 
  6  # 1. Perform simple asynchronous contact search.
 
  7  # 1. Perform complex asynchronous contact search.
 
  8  
 
  9  # NB! You will need to launch the SkypeKit runtime before running this example.
 
 10  
 
 11  # As the contact search over P2P network is somewhat slow, it cannot really
 
 12  # be implemented as a blocking function. Instead, you can create a search record 
 
 13  # (ContactSearch object), add multiple search filters to it and then start the search
 
 14  # with ContactSearch.Submit method.
 
 15  #
 
 16  # Each time the search process finds a matching contact ContactSearch.OnNewResult
 
 17  # event will get fired.
 
 18  #
 
 19  # When no more matches are found, ContactSearch.contact_search_status will change
 
 20  # to 'FINISHED'.
 
 21  # 
 
 22  # The list of collected contacts can be compiled either in the OnNewResult callback,
 
 23  # if you want your UI to respond to matches in real time. Alternatively you can 
 
 24  # wait until the search finishes and then yse ContactSearch.GetResults method.
 
 25  
 
 26  #----------------------------------------------------------------------------------
 
 27  # Importing necessary libraries. Note that you will need to set the keyFileName value
 
 28  # in the keypair.py file.
 
 29  
 
 30  import sys; 
 31  import keypair; 
 32  from time import sleep; 
 33  
 
 34  sys.path.append(keypair.distroRoot + '/ipc/python'); 
 35  sys.path.append(keypair.distroRoot + '/interfaces/skype/python'); 
 36  
 
 37  try: 
 38          import Skype;    
 39  except ImportError: 
 40          raise SystemExit('Program requires Skype and skypekit modules'); 
 41  
 
 42  #----------------------------------------------------------------------------------
 
 43  # Taking skypename and password arguments from command-line.
 
 44  
 
 45  if len(sys.argv) < 4: 
 46          print('Usage: python contactsearch.py <skypename> <password> <searchstring>'); 
 47          sys.exit(); 
 48  
 
 49  accountName = sys.argv[1]; 
 50  accountPsw  = sys.argv[2]; 
 51  searchFor       = sys.argv[3]; 
 52  
 
 53  loggedIn                = False; 
 54  
 
 55  #----------------------------------------------------------------------------------
 
 56  # Defining our own Account property change callback and assigning it to the
 
 57  # Skype.Account class.
 
 58  
 
59 -def AccountOnChange (self, property_name):
60 global loggedIn; 61 if property_name == 'status': 62 if self.status == 'LOGGED_IN': 63 loggedIn = True; 64 print('Login complete.');
65 66 Skype.Account.OnPropertyChange = AccountOnChange; 67 68 69 #---------------------------------------------------------------------------------- 70 # ContactSearch class. 71 # ContactSearch.OnPropertyChange - to catch when the search has stopped or finished. 72 # ContactSearch.OnNewResult - to retrieve matching contacts in real-time, as they 73 # get found by the search process. 74
75 -def ContactSearchOnChange (self, property_name):
76 if property_name == 'contact_search_status': 77 print('search status: ' + self.contact_search_status); 78 79 # When there is a large number of matches (conceivably you could get hundreds 80 # of matches) the result list is terminated by 'page breaks'. In that case 81 # the search will stop and object's contact_search_status changes to 'EXTENDABLE' 82 # You can then choos to continue the search with Extend method. 83 if self.contact_search_status == 'EXTENDABLE': 84 print('Auto-extending the search..'); 85 self.Extend(); 86 87 if self.contact_search_status == 'FINISHED': 88 self.Release(); 89 print('Press ENTER to exit');
90 91 Skype.ContactSearch.OnPropertyChange = ContactSearchOnChange; 92
93 -def ContactSearchOnNewResult (self, contact, rankValue):
94 print(str(rankValue + 1) + '. ' + contact.skypename);
95 96 Skype.ContactSearch.OnNewResult = ContactSearchOnNewResult; 97 98 #---------------------------------------------------------------------------------- 99 # Creating our main Skype object 100 101 try: 102 MySkype = Skype.GetSkype(keypair.keyFileName); 103 MySkype.Start(); 104 except Exception: 105 raise SystemExit('Unable to create Skype instance'); 106 107 #---------------------------------------------------------------------------------- 108 # Retrieving account and logging in with it. Then waiting in loop. 109 110 account = MySkype.GetAccount(accountName); 111 112 print('Logging in with ' + accountName); 113 account.LoginWithPassword(accountPsw, False, False); 114 115 while loggedIn == False: 116 sleep(1); 117 118 #---------------------------------------------------------------------------------- 119 # Simple contact search, just by skypename string: 120 121 SearchRec = MySkype.CreateBasicContactSearch(searchFor); 122 123 #---------------------------------------------------------------------------------- 124 # More complex contact search, by skypename and language. 125 # Note that the AddStrTerm method takes proprty ID as it's first argument, rather 126 # than property name string. 127 # 128 # By default, filters criteria are added (filter1 AND filter2 AND ..) 129 # To get multiple groups of match criteria you can insert an OR between them 130 # with ContactSearch.AddOr method. 131 # 132 # Complete list of comparison operators between property value and pattern: 133 # 'EQ' - equal 134 # 'GT' - greater than 135 # 'GE' - greater or equal 136 # 'LT' - less than 137 # 'LE' - less or equal 138 # 'PREFIX_EQ' - starts with equal 139 # 'PREFIX_GE' - starts with greater or equal 140 # 'PREFIX_LE' - starts with less or equal 141 # 'CONTAINS_WORDS' - contains words 142 # 'CONTAINS_WORD_PREFIXES' - contains words that start with pattern 143 # 144 # Comment the simple search example above and uncomment the three codelines below 145 # to try out the complex search. 146 147 #SearchRec = MySkype.CreateContactSearch(); 148 #SearchRec.AddStrTerm(Skype.Contact.P_SKYPENAME, 'EQ', searchFor, False); 149 #SearchRec.AddLanguageTerm('en', False); 150 151 if SearchRec.IsValid(): 152 SearchRec.Submit(); 153 else: 154 print('Invalid search record.'); 155 156 raw_input(''); 157 158 print('Exiting..'); 159 SearchRec.Release(); 160 MySkype.stop(); 161