<s> import objc <EOL> from AddressBook import * <EOL> import re <EOL> from time import time , gmtime <EOL> from urllib import quote <EOL> import json <EOL> from Utilities import * <EOL> from Provider import * <EOL> Provider . addProvider ( "<STR_LIT>" ) <EOL> Provider . addProvider ( "<STR_LIT>" ) <EOL> Provider . addProvider ( "<STR_LIT>" ) <EOL> Provider . addProvider ( "<STR_LIT>" ) <EOL> Provider . addProvider ( "<STR_LIT>" ) <EOL> Provider . addProvider ( "<STR_LIT>" ) <EOL> Provider . addProvider ( "<STR_LIT>" ) <EOL> class Clue ( object ) : <EOL> CACHE = { } <EOL> @ classmethod <EOL> def forPerson ( cls , person ) : <EOL> if person . uniqueId ( ) in Clue . CACHE : <EOL> print_info ( "<STR_LIT>" ) <EOL> return Clue . CACHE [ person . uniqueId ( ) ] <EOL> print_info ( "<STR_LIT>" ) <EOL> Clue . CACHE [ person . uniqueId ( ) ] = Clue ( person ) <EOL> return Clue . CACHE [ person . uniqueId ( ) ] <EOL> @ classmethod <EOL> def forUrl ( cls , url ) : <EOL> for clue in Clue . CACHE . values ( ) : <EOL> if normalize_url ( url ) in [ normalize_url ( u ) for u in clue . urls ( ) ] : <EOL> return clue <EOL> def __init__ ( self , person ) : <EOL> self . person = person <EOL> self . delegate = None <EOL> self . extra_urls = [ ] <EOL> if NSUserDefaults . standardUserDefaults ( ) . boolForKey_ ( "<STR_LIT>" ) : <EOL> self . getMoreUrls ( ) <EOL> self . providers = [ cls ( self ) for cls in Provider . providers ( ) ] <EOL> def setDelegate_ ( self , delegate ) : <EOL> self . delegate = delegate <EOL> def start ( self ) : <EOL> self . boring_urls = self . urls ( ) <EOL> for provider in self . providers : <EOL> provider . provide ( ) <EOL> self . changed ( ) <EOL> def getMoreUrls ( self ) : <EOL> if not self . ab_urls ( ) : return <EOL> api = "<STR_LIT>" + "<STR_LIT:U+002C>" . join ( [ quote ( url ) for url in self . ab_urls ( ) ] ) <EOL> print_info ( "<STR_LIT>" + api ) <EOL> Cache . getContentOfUrlAndCallback ( callback = self . gotSocialGraphData , url = api , timeout = <NUM_LIT> * <NUM_LIT> , wantStale = False , delay = <NUM_LIT:2> ) <EOL> def gotSocialGraphData ( self , raw , isStale ) : <EOL> try : <EOL> data = json . loads ( raw ) <EOL> except ValueError : <EOL> return <EOL> urls = data [ '<STR_LIT>' ] . keys ( ) <EOL> self . addExtraUrls ( urls ) <EOL> self . start ( ) <EOL> def addExtraUrls ( self , urls ) : <EOL> if not urls : return <EOL> dedupe = { } <EOL> for url in self . extra_urls + urls : <EOL> if re . match ( r'<STR_LIT:http>' , url ) : <EOL> if not normalize_url ( url ) in dedupe : <EOL> dedupe [ normalize_url ( url ) ] = url <EOL> self . extra_urls = dedupe . values ( ) <EOL> known_urls = [ normalize_url ( url ) for url in self . ab_urls ( ) ] <EOL> for url in [ u for u in self . extra_urls ] : <EOL> if normalize_url ( url ) in known_urls : <EOL> self . extra_urls . remove ( url ) <EOL> print_info ( "<STR_LIT>" % ( "<STR_LIT:U+002CU+0020>" . join ( self . ab_urls ( ) ) ) ) <EOL> print_info ( "<STR_LIT>" % ( "<STR_LIT:U+002CU+0020>" . join ( self . extra_urls ) ) ) <EOL> def changed ( self ) : <EOL> if self . delegate : <EOL> self . delegate . updateWebContent_fromClue_ ( self . content ( ) , self ) <EOL> def content ( self ) : <EOL> atoms = [ ] <EOL> for p in self . providers : <EOL> atoms += p . atoms <EOL> atoms . sort ( lambda a , b : cmp ( b . sortOrder ( ) , a . sortOrder ( ) ) ) <EOL> content = "<STR_LIT>" . join ( [ atom . content ( ) for atom in atoms ] ) <EOL> if content : return content <EOL> return "<STR_LIT>" <EOL> def stop ( self ) : <EOL> NSObject . cancelPreviousPerformRequestsWithTarget_ ( self ) <EOL> for current in self . providers : <EOL> current . stop ( ) <EOL> def takeUrls ( self , pattern ) : <EOL> interesting = [ ] <EOL> for u in self . urls ( ) : <EOL> if re . search ( pattern , u ) : <EOL> interesting . append ( u ) <EOL> for u in interesting : <EOL> if u in self . boring_urls : <EOL> self . boring_urls . remove ( u ) <EOL> return interesting <EOL> def __eq__ ( self , other ) : <EOL> if not other : return False <EOL> return self . uniqueId ( ) == other . uniqueId ( ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % self . displayName ( ) <EOL> def uniqueId ( self ) : <EOL> return self . person . uniqueId ( ) <EOL> def image ( self ) : <EOL> if self . person . imageData ( ) : <EOL> return NSImage . alloc ( ) . initWithData_ ( self . person . imageData ( ) ) <EOL> if self . isCompany ( ) : <EOL> return NSImage . imageNamed_ ( "<STR_LIT>" ) <EOL> else : <EOL> return NSImage . imageNamed_ ( "<STR_LIT>" ) <EOL> def forename ( self ) : <EOL> return self . person . valueForProperty_ ( kABFirstNameProperty ) <EOL> def surname ( self ) : <EOL> return self . person . valueForProperty_ ( kABLastNameProperty ) <EOL> def isCompany ( self ) : <EOL> return ( self . person . valueForProperty_ ( kABPersonFlags ) or <NUM_LIT:0> ) & kABShowAsCompany <EOL> def displayName ( self ) : <EOL> if self . isCompany ( ) : <EOL> return self . person . valueForProperty_ ( kABOrganizationProperty ) <EOL> f = self . forename ( ) <EOL> s = self . surname ( ) <EOL> if s and f : return f + "<STR_LIT:U+0020>" + s <EOL> if s : return s <EOL> if f : return f <EOL> return "<STR_LIT>" <EOL> def companyName ( self ) : <EOL> if self . isCompany ( ) : return "<STR_LIT>" <EOL> c = self . person . valueForProperty_ ( kABOrganizationProperty ) <EOL> if c : return c <EOL> return "<STR_LIT>" <EOL> def addresses ( self ) : <EOL> return self . _multi_to_list ( self . person . valueForProperty_ ( kABAddressProperty ) ) <EOL> def emails ( self ) : <EOL> return self . _multi_to_list ( self . person . valueForProperty_ ( kABEmailProperty ) ) <EOL> def ab_urls ( self ) : <EOL> return self . _multi_to_list ( self . person . valueForProperty_ ( kABURLsProperty ) ) <EOL> def urls ( self ) : <EOL> return self . ab_urls ( ) + self . extra_urls <EOL> def email ( self ) : <EOL> return self . emails ( ) [ <NUM_LIT:0> ] <EOL> def birthday ( self ) : <EOL> try : <EOL> if self . person . valueForProperty_ ( kABBirthdayProperty ) : <EOL> return gmtime ( self . person . valueForProperty_ ( kABBirthdayProperty ) . timeIntervalSince1970 ( ) ) <EOL> except ValueError : <EOL> pass <EOL> return None <EOL> def _multi_to_list ( self , multi ) : <EOL> if not multi : return [ ] <EOL> output = [ ] <EOL> for i in range ( <NUM_LIT:0> , multi . count ( ) ) : <EOL> output . append ( multi . valueAtIndex_ ( i ) ) <EOL> return output </s>
