raffaele181188

Google contact fix

Jan 2nd, 2012
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.81 KB | None | 0 0
  1. package com.zybnet.contact.fix;
  2.  
  3. import com.google.gdata.client.contacts.ContactQuery;
  4. import com.google.gdata.client.contacts.ContactsService;
  5. import com.google.gdata.data.contacts.ContactEntry;
  6. import com.google.gdata.data.contacts.ContactFeed;
  7. import com.google.gdata.data.extensions.FullName;
  8. import com.google.gdata.data.extensions.PhoneNumber;
  9. import com.google.gdata.util.AuthenticationException;
  10. import com.google.gdata.util.ServiceException;
  11. import java.io.IOException;
  12. import java.net.MalformedURLException;
  13. import java.net.URL;
  14. import java.util.List;
  15.  
  16. /**
  17.  *
  18.  * @author Raffaele
  19.  */
  20. public class Main {
  21.  
  22.     /**
  23.      * @param args username and password
  24.      */
  25.     public static void main(String[] args) throws AuthenticationException, MalformedURLException, IOException, ServiceException {
  26.        ContactsService service = new ContactsService("zybnet-fix-android");
  27.         service.setUserCredentials(args[0], args[1]);
  28.         URL getAllURL = new URL("https://www.google.com/m8/feeds/contacts/" + args[0] + "/full");
  29.         ContactQuery query = new ContactQuery(getAllURL);
  30.         query.setMaxResults(10000);
  31.        
  32.         ContactFeed feed = service.getFeed(query, ContactFeed.class);
  33.         int counter = 0;
  34.         for (ContactEntry entry: feed.getEntries()) {
  35.             if (entry.hasName()  && entry.hasPhoneNumbers()) {
  36.                 counter++;
  37.                 printNumbers(entry);
  38.                 List<PhoneNumber> numbers = entry.getPhoneNumbers();
  39.                 for (PhoneNumber number : numbers) {
  40.                     setRel(entry, number);
  41.                 }
  42.             }
  43.         }
  44.         System.out.println(String.format("Founded %d elements", counter));
  45.     }
  46.    
  47.     private static void setRel(ContactEntry entry, PhoneNumber number) {
  48.         if ("http://schemas.google.com/g/2005#mobile".equals(number.getRel()))
  49.             return;
  50.         try {
  51.             number.setRel("http://schemas.google.com/g/2005#mobile");
  52.             ContactEntry updated = entry.update();
  53.             System.out.println("Update rel: OK");
  54.         } catch(com.google.gdata.util.InvalidEntryException e) {
  55.             number.setLabel(null);
  56.             try {
  57.                 entry.update();
  58.             } catch (Exception e2) {
  59.                 System.err.println("Another Failure. Giving up on " + entry.getName().getFullName().getValue());
  60.             }
  61.         }catch (Exception e) {
  62.             System.err.println("Error in updating rel: " + e.getClass().getCanonicalName());
  63.         }
  64.     }
  65.    
  66.     private static void printNumbers(ContactEntry entry) {
  67.         FullName name = entry.getName().getFullName();
  68.         for (PhoneNumber number : entry.getPhoneNumbers())
  69.             System.out.println(String.format("%s: %s [%s]", name.getValue(), number.getPhoneNumber(), number.getRel()));                
  70.     }
  71. }
Add Comment
Please, Sign In to add comment