<s> '''<STR_LIT>''' <EOL> __author__ = '<STR_LIT>' <EOL> __version__ = '<STR_LIT>' <EOL> import base64 <EOL> import calendar <EOL> import httplib <EOL> import os <EOL> import rfc822 <EOL> import simplejson <EOL> import sys <EOL> import tempfile <EOL> import textwrap <EOL> import time <EOL> import urllib <EOL> import urllib2 <EOL> import urlparse <EOL> try : <EOL> from hashlib import md5 <EOL> except ImportError : <EOL> from md5 import md5 <EOL> CHARACTER_LIMIT = <NUM_LIT> <EOL> DEFAULT_CACHE = object ( ) <EOL> class TwitterError ( Exception ) : <EOL> '''<STR_LIT>''' <EOL> @ property <EOL> def message ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . args [ <NUM_LIT:0> ] <EOL> class Status ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , <EOL> created_at = None , <EOL> favorited = None , <EOL> id = None , <EOL> text = None , <EOL> user = None , <EOL> in_reply_to_screen_name = None , <EOL> in_reply_to_user_id = None , <EOL> in_reply_to_status_id = None , <EOL> truncated = None , <EOL> source = None , <EOL> now = None ) : <EOL> '''<STR_LIT>''' <EOL> self . created_at = created_at <EOL> self . favorited = favorited <EOL> self . id = id <EOL> self . text = text <EOL> self . user = user <EOL> self . now = now <EOL> self . in_reply_to_screen_name = in_reply_to_screen_name <EOL> self . in_reply_to_user_id = in_reply_to_user_id <EOL> self . in_reply_to_status_id = in_reply_to_status_id <EOL> self . truncated = truncated <EOL> self . source = source <EOL> def GetCreatedAt ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . _created_at <EOL> def SetCreatedAt ( self , created_at ) : <EOL> '''<STR_LIT>''' <EOL> self . _created_at = created_at <EOL> created_at = property ( GetCreatedAt , SetCreatedAt , <EOL> doc = '<STR_LIT>' ) <EOL> def GetCreatedAtInSeconds ( self ) : <EOL> '''<STR_LIT>''' <EOL> return calendar . timegm ( rfc822 . parsedate ( self . created_at ) ) <EOL> created_at_in_seconds = property ( GetCreatedAtInSeconds , <EOL> doc = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def GetFavorited ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . _favorited <EOL> def SetFavorited ( self , favorited ) : <EOL> '''<STR_LIT>''' <EOL> self . _favorited = favorited <EOL> favorited = property ( GetFavorited , SetFavorited , <EOL> doc = '<STR_LIT>' ) <EOL> def GetId ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . _id <EOL> def SetId ( self , id ) : <EOL> '''<STR_LIT>''' <EOL> self . _id = id <EOL> id = property ( GetId , SetId , <EOL> doc = '<STR_LIT>' ) <EOL> def GetInReplyToScreenName ( self ) : <EOL> return self . _in_reply_to_screen_name <EOL> def SetInReplyToScreenName ( self , in_reply_to_screen_name ) : <EOL> self . _in_reply_to_screen_name = in_reply_to_screen_name <EOL> in_reply_to_screen_name = property ( GetInReplyToScreenName , SetInReplyToScreenName , <EOL> doc = '<STR_LIT>' ) <EOL> def GetInReplyToUserId ( self ) : <EOL> return self . _in_reply_to_user_id <EOL> def SetInReplyToUserId ( self , in_reply_to_user_id ) : <EOL> self . _in_reply_to_user_id = in_reply_to_user_id <EOL> in_reply_to_user_id = property ( GetInReplyToUserId , SetInReplyToUserId , <EOL> doc = '<STR_LIT>' ) <EOL> def GetInReplyToStatusId ( self ) : <EOL> return self . _in_reply_to_status_id <EOL> def SetInReplyToStatusId ( self , in_reply_to_status_id ) : <EOL> self . _in_reply_to_status_id = in_reply_to_status_id <EOL> in_reply_to_status_id = property ( GetInReplyToStatusId , SetInReplyToStatusId , <EOL> doc = '<STR_LIT>' ) <EOL> def GetTruncated ( self ) : <EOL> return self . _truncated <EOL> def SetTruncated ( self , truncated ) : <EOL> self . _truncated = truncated <EOL> truncated = property ( GetTruncated , SetTruncated , <EOL> doc = '<STR_LIT>' ) <EOL> def GetSource ( self ) : <EOL> return self . _source <EOL> def SetSource ( self , source ) : <EOL> self . _source = source <EOL> source = property ( GetSource , SetSource , <EOL> doc = '<STR_LIT>' ) <EOL> def GetText ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . _text <EOL> def SetText ( self , text ) : <EOL> '''<STR_LIT>''' <EOL> self . _text = text <EOL> text = property ( GetText , SetText , <EOL> doc = '<STR_LIT>' ) <EOL> def GetRelativeCreatedAt ( self ) : <EOL> '''<STR_LIT>''' <EOL> fudge = <NUM_LIT> <EOL> delta = long ( self . now ) - long ( self . created_at_in_seconds ) <EOL> if delta < ( <NUM_LIT:1> * fudge ) : <EOL> return '<STR_LIT>' <EOL> elif delta < ( <NUM_LIT> * ( <NUM_LIT:1> / fudge ) ) : <EOL> return '<STR_LIT>' % ( delta ) <EOL> elif delta < ( <NUM_LIT> * fudge ) : <EOL> return '<STR_LIT>' <EOL> elif delta < ( <NUM_LIT> * <NUM_LIT> * ( <NUM_LIT:1> / fudge ) ) : <EOL> return '<STR_LIT>' % ( delta / <NUM_LIT> ) <EOL> elif delta < ( <NUM_LIT> * <NUM_LIT> * fudge ) : <EOL> return '<STR_LIT>' <EOL> elif delta < ( <NUM_LIT> * <NUM_LIT> * <NUM_LIT> * ( <NUM_LIT:1> / fudge ) ) : <EOL> return '<STR_LIT>' % ( delta / ( <NUM_LIT> * <NUM_LIT> ) ) <EOL> elif delta < ( <NUM_LIT> * <NUM_LIT> * <NUM_LIT> * fudge ) : <EOL> return '<STR_LIT>' <EOL> else : <EOL> return '<STR_LIT>' % ( delta / ( <NUM_LIT> * <NUM_LIT> * <NUM_LIT> ) ) <EOL> relative_created_at = property ( GetRelativeCreatedAt , <EOL> doc = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def GetUser ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . _user <EOL> def SetUser ( self , user ) : <EOL> '''<STR_LIT>''' <EOL> self . _user = user <EOL> user = property ( GetUser , SetUser , <EOL> doc = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def GetNow ( self ) : <EOL> '''<STR_LIT>''' <EOL> if self . _now is None : <EOL> self . _now = time . time ( ) <EOL> return self . _now <EOL> def SetNow ( self , now ) : <EOL> '''<STR_LIT>''' <EOL> self . _now = now <EOL> now = property ( GetNow , SetNow , <EOL> doc = '<STR_LIT>' ) <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> def __eq__ ( self , other ) : <EOL> try : <EOL> return other and self . created_at == other . created_at and self . id == other . id and self . text == other . text and self . user == other . user and self . in_reply_to_screen_name == other . in_reply_to_screen_name and self . in_reply_to_user_id == other . in_reply_to_user_id and self . in_reply_to_status_id == other . in_reply_to_status_id and self . truncated == other . truncated and self . favorited == other . favorited and self . source == other . source <EOL> except AttributeError : <EOL> return False <EOL> def __str__ ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . AsJsonString ( ) <EOL> def AsJsonString ( self ) : <EOL> '''<STR_LIT>''' <EOL> return simplejson . dumps ( self . AsDict ( ) , sort_keys = True ) <EOL> def AsDict ( self ) : <EOL> '''<STR_LIT>''' <EOL> data = { } <EOL> if self . created_at : <EOL> data [ '<STR_LIT>' ] = self . created_at <EOL> if self . favorited : <EOL> data [ '<STR_LIT>' ] = self . favorited <EOL> if self . id : <EOL> data [ '<STR_LIT:id>' ] = self . id <EOL> if self . text : <EOL> data [ '<STR_LIT:text>' ] = self . text <EOL> if self . user : <EOL> data [ '<STR_LIT:user>' ] = self . user . AsDict ( ) <EOL> if self . in_reply_to_screen_name : <EOL> data [ '<STR_LIT>' ] = self . in_reply_to_screen_name <EOL> if self . in_reply_to_user_id : <EOL> data [ '<STR_LIT>' ] = self . in_reply_to_user_id <EOL> if self . in_reply_to_status_id : <EOL> data [ '<STR_LIT>' ] = self . in_reply_to_status_id <EOL> if self . truncated is not None : <EOL> data [ '<STR_LIT>' ] = self . truncated <EOL> if self . favorited is not None : <EOL> data [ '<STR_LIT>' ] = self . favorited <EOL> if self . source : <EOL> data [ '<STR_LIT:source>' ] = self . source <EOL> return data <EOL> @ staticmethod <EOL> def NewFromJsonDict ( data ) : <EOL> '''<STR_LIT>''' <EOL> if '<STR_LIT:user>' in data : <EOL> user = User . NewFromJsonDict ( data [ '<STR_LIT:user>' ] ) <EOL> else : <EOL> user = None <EOL> return Status ( created_at = data . get ( '<STR_LIT>' , None ) , <EOL> favorited = data . get ( '<STR_LIT>' , None ) , <EOL> id = data . get ( '<STR_LIT:id>' , None ) , <EOL> text = data . get ( '<STR_LIT:text>' , None ) , <EOL> in_reply_to_screen_name = data . get ( '<STR_LIT>' , None ) , <EOL> in_reply_to_user_id = data . get ( '<STR_LIT>' , None ) , <EOL> in_reply_to_status_id = data . get ( '<STR_LIT>' , None ) , <EOL> truncated = data . get ( '<STR_LIT>' , None ) , <EOL> source = data . get ( '<STR_LIT:source>' , None ) , <EOL> user = user ) <EOL> class User ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , <EOL> id = None , <EOL> name = None , <EOL> screen_name = None , <EOL> location = None , <EOL> description = None , <EOL> profile_image_url = None , <EOL> profile_background_tile = None , <EOL> profile_background_image_url = None , <EOL> profile_sidebar_fill_color = None , <EOL> profile_background_color = None , <EOL> profile_link_color = None , <EOL> profile_text_color = None , <EOL> protected = None , <EOL> utc_offset = None , <EOL> time_zone = None , <EOL> followers_count = None , <EOL> friends_count = None , <EOL> statuses_count = None , <EOL> favourites_count = None , <EOL> url = None , <EOL> status = None ) : <EOL> self . id = id <EOL> self . name = name <EOL> self . screen_name = screen_name <EOL> self . location = location <EOL> self . description = description <EOL> self . profile_image_url = profile_image_url <EOL> self . profile_background_tile = profile_background_tile <EOL> self . profile_background_image_url = profile_background_image_url <EOL> self . profile_sidebar_fill_color = profile_sidebar_fill_color <EOL> self . profile_background_color = profile_background_color <EOL> self . profile_link_color = profile_link_color <EOL> self . profile_text_color = profile_text_color <EOL> self . protected = protected <EOL> self . utc_offset = utc_offset <EOL> self . time_zone = time_zone <EOL> self . followers_count = followers_count <EOL> self . friends_count = friends_count <EOL> self . statuses_count = statuses_count <EOL> self . favourites_count = favourites_count <EOL> self . url = url <EOL> self . status = status <EOL> def GetId ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . _id <EOL> def SetId ( self , id ) : <EOL> '''<STR_LIT>''' <EOL> self . _id = id <EOL> id = property ( GetId , SetId , <EOL> doc = '<STR_LIT>' ) <EOL> def GetName ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . _name <EOL> def SetName ( self , name ) : <EOL> '''<STR_LIT>''' <EOL> self . _name = name <EOL> name = property ( GetName , SetName , <EOL> doc = '<STR_LIT>' ) <EOL> def GetScreenName ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . _screen_name <EOL> def SetScreenName ( self , screen_name ) : <EOL> '''<STR_LIT>''' <EOL> self . _screen_name = screen_name <EOL> screen_name = property ( GetScreenName , SetScreenName , <EOL> doc = '<STR_LIT>' ) <EOL> def GetLocation ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . _location <EOL> def SetLocation ( self , location ) : <EOL> '''<STR_LIT>''' <EOL> self . _location = location <EOL> location = property ( GetLocation , SetLocation , <EOL> doc = '<STR_LIT>' ) <EOL> def GetDescription ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . _description <EOL> def SetDescription ( self , description ) : <EOL> '''<STR_LIT>''' <EOL> self . _description = description <EOL> description = property ( GetDescription , SetDescription , <EOL> doc = '<STR_LIT>' ) <EOL> def GetUrl ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . _url <EOL> def SetUrl ( self , url ) : <EOL> '''<STR_LIT>''' <EOL> self . _url = url <EOL> url = property ( GetUrl , SetUrl , <EOL> doc = '<STR_LIT>' ) <EOL> def GetProfileImageUrl ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . _profile_image_url <EOL> def SetProfileImageUrl ( self , profile_image_url ) : <EOL> '''<STR_LIT>''' <EOL> self . _profile_image_url = profile_image_url <EOL> profile_image_url = property ( GetProfileImageUrl , SetProfileImageUrl , <EOL> doc = '<STR_LIT>' ) <EOL> def GetProfileBackgroundTile ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . _profile_background_tile <EOL> def SetProfileBackgroundTile ( self , profile_background_tile ) : <EOL> '''<STR_LIT>''' <EOL> self . _profile_background_tile = profile_background_tile <EOL> profile_background_tile = property ( GetProfileBackgroundTile , SetProfileBackgroundTile , <EOL> doc = '<STR_LIT>' ) <EOL> def GetProfileBackgroundImageUrl ( self ) : <EOL> return self . _profile_background_image_url <EOL> def SetProfileBackgroundImageUrl ( self , profile_background_image_url ) : <EOL> self . _profile_background_image_url = profile_background_image_url <EOL> profile_background_image_url = property ( GetProfileBackgroundImageUrl , SetProfileBackgroundImageUrl , <EOL> doc = '<STR_LIT>' ) <EOL> def GetProfileSidebarFillColor ( self ) : <EOL> return self . _profile_sidebar_fill_color <EOL> def SetProfileSidebarFillColor ( self , profile_sidebar_fill_color ) : <EOL> self . _profile_sidebar_fill_color = profile_sidebar_fill_color <EOL> profile_sidebar_fill_color = property ( GetProfileSidebarFillColor , SetProfileSidebarFillColor ) <EOL> def GetProfileBackgroundColor ( self ) : <EOL> return self . _profile_background_color <EOL> def SetProfileBackgroundColor ( self , profile_background_color ) : <EOL> self . _profile_background_color = profile_background_color <EOL> profile_background_color = property ( GetProfileBackgroundColor , SetProfileBackgroundColor ) <EOL> def GetProfileLinkColor ( self ) : <EOL> return self . _profile_link_color <EOL> def SetProfileLinkColor ( self , profile_link_color ) : <EOL> self . _profile_link_color = profile_link_color <EOL> profile_link_color = property ( GetProfileLinkColor , SetProfileLinkColor ) <EOL> def GetProfileTextColor ( self ) : <EOL> return self . _profile_text_color <EOL> def SetProfileTextColor ( self , profile_text_color ) : <EOL> self . _profile_text_color = profile_text_color <EOL> profile_text_color = property ( GetProfileTextColor , SetProfileTextColor ) <EOL> def GetProtected ( self ) : <EOL> return self . _protected <EOL> def SetProtected ( self , protected ) : <EOL> self . _protected = protected <EOL> protected = property ( GetProtected , SetProtected ) <EOL> def GetUtcOffset ( self ) : <EOL> return self . _utc_offset <EOL> def SetUtcOffset ( self , utc_offset ) : <EOL> self . _utc_offset = utc_offset <EOL> utc_offset = property ( GetUtcOffset , SetUtcOffset ) <EOL> def GetTimeZone ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . _time_zone <EOL> def SetTimeZone ( self , time_zone ) : <EOL> '''<STR_LIT>''' <EOL> self . _time_zone = time_zone <EOL> time_zone = property ( GetTimeZone , SetTimeZone ) <EOL> def GetStatus ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . _status <EOL> def SetStatus ( self , status ) : <EOL> '''<STR_LIT>''' <EOL> self . _status = status <EOL> status = property ( GetStatus , SetStatus , <EOL> doc = '<STR_LIT>' ) <EOL> def GetFriendsCount ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . _friends_count <EOL> def SetFriendsCount ( self , count ) : <EOL> '''<STR_LIT>''' <EOL> self . _friends_count = count <EOL> friends_count = property ( GetFriendsCount , SetFriendsCount , <EOL> doc = '<STR_LIT>' ) <EOL> def GetFollowersCount ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . _followers_count <EOL> def SetFollowersCount ( self , count ) : <EOL> '''<STR_LIT>''' <EOL> self . _followers_count = count <EOL> followers_count = property ( GetFollowersCount , SetFollowersCount , <EOL> doc = '<STR_LIT>' ) <EOL> def GetStatusesCount ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . _statuses_count <EOL> def SetStatusesCount ( self , count ) : <EOL> '''<STR_LIT>''' <EOL> self . _statuses_count = count <EOL> statuses_count = property ( GetStatusesCount , SetStatusesCount , <EOL> doc = '<STR_LIT>' ) <EOL> def GetFavouritesCount ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . _favourites_count <EOL> def SetFavouritesCount ( self , count ) : <EOL> '''<STR_LIT>''' <EOL> self . _favourites_count = count <EOL> favourites_count = property ( GetFavouritesCount , SetFavouritesCount , <EOL> doc = '<STR_LIT>' ) <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> def __eq__ ( self , other ) : <EOL> try : <EOL> return other and self . id == other . id and self . name == other . name and self . screen_name == other . screen_name and self . location == other . location and self . description == other . description and self . profile_image_url == other . profile_image_url and self . profile_background_tile == other . profile_background_tile and self . profile_background_image_url == other . profile_background_image_url and self . profile_sidebar_fill_color == other . profile_sidebar_fill_color and self . profile_background_color == other . profile_background_color and self . profile_link_color == other . profile_link_color and self . profile_text_color == other . profile_text_color and self . protected == other . protected and self . utc_offset == other . utc_offset and self . time_zone == other . time_zone and self . url == other . url and self . statuses_count == other . statuses_count and self . followers_count == other . followers_count and self . favourites_count == other . favourites_count and self . friends_count == other . friends_count and self . status == other . status <EOL> except AttributeError : <EOL> return False <EOL> def __str__ ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . AsJsonString ( ) <EOL> def AsJsonString ( self ) : <EOL> '''<STR_LIT>''' <EOL> return simplejson . dumps ( self . AsDict ( ) , sort_keys = True ) <EOL> def AsDict ( self ) : <EOL> '''<STR_LIT>''' <EOL> data = { } <EOL> if self . id : <EOL> data [ '<STR_LIT:id>' ] = self . id <EOL> if self . name : <EOL> data [ '<STR_LIT:name>' ] = self . name <EOL> if self . screen_name : <EOL> data [ '<STR_LIT>' ] = self . screen_name <EOL> if self . location : <EOL> data [ '<STR_LIT:location>' ] = self . location <EOL> if self . description : <EOL> data [ '<STR_LIT:description>' ] = self . description <EOL> if self . profile_image_url : <EOL> data [ '<STR_LIT>' ] = self . profile_image_url <EOL> if self . profile_background_tile is not None : <EOL> data [ '<STR_LIT>' ] = self . profile_background_tile <EOL> if self . profile_background_image_url : <EOL> data [ '<STR_LIT>' ] = self . profile_background_image_url <EOL> if self . profile_background_color : <EOL> data [ '<STR_LIT>' ] = self . profile_background_color <EOL> if self . profile_link_color : <EOL> data [ '<STR_LIT>' ] = self . profile_link_color <EOL> if self . profile_text_color : <EOL> data [ '<STR_LIT>' ] = self . profile_text_color <EOL> if self . protected is not None : <EOL> data [ '<STR_LIT>' ] = self . protected <EOL> if self . utc_offset : <EOL> data [ '<STR_LIT>' ] = self . utc_offset <EOL> if self . time_zone : <EOL> data [ '<STR_LIT>' ] = self . time_zone <EOL> if self . url : <EOL> data [ '<STR_LIT:url>' ] = self . url <EOL> if self . status : <EOL> data [ '<STR_LIT:status>' ] = self . status . AsDict ( ) <EOL> if self . friends_count : <EOL> data [ '<STR_LIT>' ] = self . friends_count <EOL> if self . followers_count : <EOL> data [ '<STR_LIT>' ] = self . followers_count <EOL> if self . statuses_count : <EOL> data [ '<STR_LIT>' ] = self . statuses_count <EOL> if self . favourites_count : <EOL> data [ '<STR_LIT>' ] = self . favourites_count <EOL> return data <EOL> @ staticmethod <EOL> def NewFromJsonDict ( data ) : <EOL> '''<STR_LIT>''' <EOL> if '<STR_LIT:status>' in data : <EOL> status = Status . NewFromJsonDict ( data [ '<STR_LIT:status>' ] ) <EOL> else : <EOL> status = None <EOL> return User ( id = data . get ( '<STR_LIT:id>' , None ) , <EOL> name = data . get ( '<STR_LIT:name>' , None ) , <EOL> screen_name = data . get ( '<STR_LIT>' , None ) , <EOL> location = data . get ( '<STR_LIT:location>' , None ) , <EOL> description = data . get ( '<STR_LIT:description>' , None ) , <EOL> statuses_count = data . get ( '<STR_LIT>' , None ) , <EOL> followers_count = data . get ( '<STR_LIT>' , None ) , <EOL> favourites_count = data . get ( '<STR_LIT>' , None ) , <EOL> friends_count = data . get ( '<STR_LIT>' , None ) , <EOL> profile_image_url = data . get ( '<STR_LIT>' , None ) , <EOL> profile_background_tile = data . get ( '<STR_LIT>' , None ) , <EOL> profile_background_image_url = data . get ( '<STR_LIT>' , None ) , <EOL> profile_sidebar_fill_color = data . get ( '<STR_LIT>' , None ) , <EOL> profile_background_color = data . get ( '<STR_LIT>' , None ) , <EOL> profile_link_color = data . get ( '<STR_LIT>' , None ) , <EOL> profile_text_color = data . get ( '<STR_LIT>' , None ) , <EOL> protected = data . get ( '<STR_LIT>' , None ) , <EOL> utc_offset = data . get ( '<STR_LIT>' , None ) , <EOL> time_zone = data . get ( '<STR_LIT>' , None ) , <EOL> url = data . get ( '<STR_LIT:url>' , None ) , <EOL> status = status ) <EOL> class DirectMessage ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , <EOL> id = None , <EOL> created_at = None , <EOL> sender_id = None , <EOL> sender_screen_name = None , <EOL> recipient_id = None , <EOL> recipient_screen_name = None , <EOL> text = None ) : <EOL> '''<STR_LIT>''' <EOL> self . id = id <EOL> self . created_at = created_at <EOL> self . sender_id = sender_id <EOL> self . sender_screen_name = sender_screen_name <EOL> self . recipient_id = recipient_id <EOL> self . recipient_screen_name = recipient_screen_name <EOL> self . text = text <EOL> def GetId ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . _id <EOL> def SetId ( self , id ) : <EOL> '''<STR_LIT>''' <EOL> self . _id = id <EOL> id = property ( GetId , SetId , <EOL> doc = '<STR_LIT>' ) <EOL> def GetCreatedAt ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . _created_at <EOL> def SetCreatedAt ( self , created_at ) : <EOL> '''<STR_LIT>''' <EOL> self . _created_at = created_at <EOL> created_at = property ( GetCreatedAt , SetCreatedAt , <EOL> doc = '<STR_LIT>' ) <EOL> def GetCreatedAtInSeconds ( self ) : <EOL> '''<STR_LIT>''' <EOL> return calendar . timegm ( rfc822 . parsedate ( self . created_at ) ) <EOL> created_at_in_seconds = property ( GetCreatedAtInSeconds , <EOL> doc = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def GetSenderId ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . _sender_id <EOL> def SetSenderId ( self , sender_id ) : <EOL> '''<STR_LIT>''' <EOL> self . _sender_id = sender_id <EOL> sender_id = property ( GetSenderId , SetSenderId , <EOL> doc = '<STR_LIT>' ) <EOL> def GetSenderScreenName ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . _sender_screen_name <EOL> def SetSenderScreenName ( self , sender_screen_name ) : <EOL> '''<STR_LIT>''' <EOL> self . _sender_screen_name = sender_screen_name <EOL> sender_screen_name = property ( GetSenderScreenName , SetSenderScreenName , <EOL> doc = '<STR_LIT>' ) <EOL> def GetRecipientId ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . _recipient_id <EOL> def SetRecipientId ( self , recipient_id ) : <EOL> '''<STR_LIT>''' <EOL> self . _recipient_id = recipient_id <EOL> recipient_id = property ( GetRecipientId , SetRecipientId , <EOL> doc = '<STR_LIT>' ) <EOL> def GetRecipientScreenName ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . _recipient_screen_name <EOL> def SetRecipientScreenName ( self , recipient_screen_name ) : <EOL> '''<STR_LIT>''' <EOL> self . _recipient_screen_name = recipient_screen_name <EOL> recipient_screen_name = property ( GetRecipientScreenName , SetRecipientScreenName , <EOL> doc = '<STR_LIT>' ) <EOL> def GetText ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . _text <EOL> def SetText ( self , text ) : <EOL> '''<STR_LIT>''' <EOL> self . _text = text <EOL> text = property ( GetText , SetText , <EOL> doc = '<STR_LIT>' ) <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> def __eq__ ( self , other ) : <EOL> try : <EOL> return other and self . id == other . id and self . created_at == other . created_at and self . sender_id == other . sender_id and self . sender_screen_name == other . sender_screen_name and self . recipient_id == other . recipient_id and self . recipient_screen_name == other . recipient_screen_name and self . text == other . text <EOL> except AttributeError : <EOL> return False <EOL> def __str__ ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . AsJsonString ( ) <EOL> def AsJsonString ( self ) : <EOL> '''<STR_LIT>''' <EOL> return simplejson . dumps ( self . AsDict ( ) , sort_keys = True ) <EOL> def AsDict ( self ) : <EOL> '''<STR_LIT>''' <EOL> data = { } <EOL> if self . id : <EOL> data [ '<STR_LIT:id>' ] = self . id <EOL> if self . created_at : <EOL> data [ '<STR_LIT>' ] = self . created_at <EOL> if self . sender_id : <EOL> data [ '<STR_LIT>' ] = self . sender_id <EOL> if self . sender_screen_name : <EOL> data [ '<STR_LIT>' ] = self . sender_screen_name <EOL> if self . recipient_id : <EOL> data [ '<STR_LIT>' ] = self . recipient_id <EOL> if self . recipient_screen_name : <EOL> data [ '<STR_LIT>' ] = self . recipient_screen_name <EOL> if self . text : <EOL> data [ '<STR_LIT:text>' ] = self . text <EOL> return data <EOL> @ staticmethod <EOL> def NewFromJsonDict ( data ) : <EOL> '''<STR_LIT>''' <EOL> return DirectMessage ( created_at = data . get ( '<STR_LIT>' , None ) , <EOL> recipient_id = data . get ( '<STR_LIT>' , None ) , <EOL> sender_id = data . get ( '<STR_LIT>' , None ) , <EOL> text = data . get ( '<STR_LIT:text>' , None ) , <EOL> sender_screen_name = data . get ( '<STR_LIT>' , None ) , <EOL> id = data . get ( '<STR_LIT:id>' , None ) , <EOL> recipient_screen_name = data . get ( '<STR_LIT>' , None ) ) <EOL> class Api ( object ) : <EOL> '''<STR_LIT>''' <EOL> DEFAULT_CACHE_TIMEOUT = <NUM_LIT> <EOL> _API_REALM = '<STR_LIT>' <EOL> def __init__ ( self , <EOL> username = None , <EOL> password = None , <EOL> input_encoding = None , <EOL> request_headers = None , <EOL> cache = DEFAULT_CACHE ) : <EOL> '''<STR_LIT>''' <EOL> self . SetCache ( cache ) <EOL> self . _urllib = urllib2 <EOL> self . _cache_timeout = Api . DEFAULT_CACHE_TIMEOUT <EOL> self . _InitializeRequestHeaders ( request_headers ) <EOL> self . _InitializeUserAgent ( ) <EOL> self . _InitializeDefaultParameters ( ) <EOL> self . _input_encoding = input_encoding <EOL> self . SetCredentials ( username , password ) <EOL> def GetPublicTimeline ( self , since_id = None ) : <EOL> '''<STR_LIT>''' <EOL> parameters = { } <EOL> if since_id : <EOL> parameters [ '<STR_LIT>' ] = since_id <EOL> url = '<STR_LIT>' <EOL> json = self . _FetchUrl ( url , parameters = parameters ) <EOL> data = simplejson . loads ( json ) <EOL> self . _CheckForTwitterError ( data ) <EOL> return [ Status . NewFromJsonDict ( x ) for x in data ] <EOL> def GetFriendsTimeline ( self , <EOL> user = None , <EOL> count = None , <EOL> since = None , <EOL> since_id = None ) : <EOL> '''<STR_LIT>''' <EOL> if not user and not self . _username : <EOL> raise TwitterError ( "<STR_LIT>" ) <EOL> if user : <EOL> url = '<STR_LIT>' % user <EOL> else : <EOL> url = '<STR_LIT>' <EOL> parameters = { } <EOL> if count is not None : <EOL> try : <EOL> if int ( count ) > <NUM_LIT:200> : <EOL> raise TwitterError ( "<STR_LIT>" ) <EOL> except ValueError : <EOL> raise TwitterError ( "<STR_LIT>" ) <EOL> parameters [ '<STR_LIT:count>' ] = count <EOL> if since : <EOL> parameters [ '<STR_LIT>' ] = since <EOL> if since_id : <EOL> parameters [ '<STR_LIT>' ] = since_id <EOL> json = self . _FetchUrl ( url , parameters = parameters ) <EOL> data = simplejson . loads ( json ) <EOL> self . _CheckForTwitterError ( data ) <EOL> return [ Status . NewFromJsonDict ( x ) for x in data ] <EOL> def GetUserTimeline ( self , <EOL> id = None , <EOL> user_id = None , <EOL> screen_name = None , <EOL> since_id = None , <EOL> max_id = None , <EOL> count = None , <EOL> page = None ) : <EOL> '''<STR_LIT>''' <EOL> parameters = { } <EOL> if id : <EOL> url = '<STR_LIT>' % id <EOL> elif user_id : <EOL> url = '<STR_LIT>' % user_id <EOL> elif screen_name : <EOL> url = ( '<STR_LIT>' % <EOL> screen_name ) <EOL> elif not self . _username : <EOL> raise TwitterError ( "<STR_LIT>" ) <EOL> else : <EOL> url = '<STR_LIT>' <EOL> if since_id : <EOL> try : <EOL> parameters [ '<STR_LIT>' ] = long ( since_id ) <EOL> except : <EOL> raise TwitterError ( "<STR_LIT>" ) <EOL> if max_id : <EOL> try : <EOL> parameters [ '<STR_LIT>' ] = long ( max_id ) <EOL> except : <EOL> raise TwitterError ( "<STR_LIT>" ) <EOL> if count : <EOL> try : <EOL> parameters [ '<STR_LIT:count>' ] = int ( count ) <EOL> except : <EOL> raise TwitterError ( "<STR_LIT>" ) <EOL> if page : <EOL> try : <EOL> parameters [ '<STR_LIT>' ] = int ( page ) <EOL> except : <EOL> raise TwitterError ( "<STR_LIT>" ) <EOL> json = self . _FetchUrl ( url , parameters = parameters ) <EOL> data = simplejson . loads ( json ) <EOL> self . _CheckForTwitterError ( data ) <EOL> return [ Status . NewFromJsonDict ( x ) for x in data ] <EOL> def GetStatus ( self , id ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> if id : <EOL> long ( id ) <EOL> except : <EOL> raise TwitterError ( "<STR_LIT>" ) <EOL> url = '<STR_LIT>' % id <EOL> json = self . _FetchUrl ( url ) <EOL> data = simplejson . loads ( json ) <EOL> self . _CheckForTwitterError ( data ) <EOL> return Status . NewFromJsonDict ( data ) <EOL> def DestroyStatus ( self , id ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> if id : <EOL> long ( id ) <EOL> except : <EOL> raise TwitterError ( "<STR_LIT>" ) <EOL> url = '<STR_LIT>' % id <EOL> json = self . _FetchUrl ( url , post_data = { } ) <EOL> data = simplejson . loads ( json ) <EOL> self . _CheckForTwitterError ( data ) <EOL> return Status . NewFromJsonDict ( data ) <EOL> def PostUpdate ( self , status , in_reply_to_status_id = None ) : <EOL> '''<STR_LIT>''' <EOL> if not self . _username : <EOL> raise TwitterError ( "<STR_LIT>" ) <EOL> url = '<STR_LIT>' <EOL> if len ( status ) > CHARACTER_LIMIT : <EOL> raise TwitterError ( "<STR_LIT>" <EOL> "<STR_LIT>" % CHARACTER_LIMIT ) <EOL> data = { '<STR_LIT:status>' : status } <EOL> if in_reply_to_status_id : <EOL> data [ '<STR_LIT>' ] = in_reply_to_status_id <EOL> json = self . _FetchUrl ( url , post_data = data ) <EOL> data = simplejson . loads ( json ) <EOL> self . _CheckForTwitterError ( data ) <EOL> return Status . NewFromJsonDict ( data ) <EOL> def PostUpdates ( self , status , continuation = None , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> results = list ( ) <EOL> if continuation is None : <EOL> continuation = '<STR_LIT>' <EOL> line_length = CHARACTER_LIMIT - len ( continuation ) <EOL> lines = textwrap . wrap ( status , line_length ) <EOL> for line in lines [ <NUM_LIT:0> : - <NUM_LIT:1> ] : <EOL> results . append ( self . PostUpdate ( line + continuation , ** kwargs ) ) <EOL> results . append ( self . PostUpdate ( lines [ - <NUM_LIT:1> ] , ** kwargs ) ) <EOL> return results <EOL> def GetReplies ( self , since = None , since_id = None , page = None ) : <EOL> '''<STR_LIT>''' <EOL> url = '<STR_LIT>' <EOL> if not self . _username : <EOL> raise TwitterError ( "<STR_LIT>" ) <EOL> parameters = { } <EOL> if since : <EOL> parameters [ '<STR_LIT>' ] = since <EOL> if since_id : <EOL> parameters [ '<STR_LIT>' ] = since_id <EOL> if page : <EOL> parameters [ '<STR_LIT>' ] = page <EOL> json = self . _FetchUrl ( url , parameters = parameters ) <EOL> data = simplejson . loads ( json ) <EOL> self . _CheckForTwitterError ( data ) <EOL> return [ Status . NewFromJsonDict ( x ) for x in data ] <EOL> def GetFriends ( self , user = None , page = None ) : <EOL> '''<STR_LIT>''' <EOL> if not user and not self . _username : <EOL> raise TwitterError ( "<STR_LIT>" ) <EOL> if user : <EOL> url = '<STR_LIT>' % user <EOL> else : <EOL> url = '<STR_LIT>' <EOL> parameters = { } <EOL> if page : <EOL> parameters [ '<STR_LIT>' ] = page <EOL> json = self . _FetchUrl ( url , parameters = parameters ) <EOL> data = simplejson . loads ( json ) <EOL> self . _CheckForTwitterError ( data ) <EOL> return [ User . NewFromJsonDict ( x ) for x in data ] <EOL> def GetFollowers ( self , page = None ) : <EOL> '''<STR_LIT>''' <EOL> if not self . _username : <EOL> raise TwitterError ( "<STR_LIT>" ) <EOL> url = '<STR_LIT>' <EOL> parameters = { } <EOL> if page : <EOL> parameters [ '<STR_LIT>' ] = page <EOL> json = self . _FetchUrl ( url , parameters = parameters ) <EOL> data = simplejson . loads ( json ) <EOL> self . _CheckForTwitterError ( data ) <EOL> return [ User . NewFromJsonDict ( x ) for x in data ] <EOL> def GetFeatured ( self ) : <EOL> '''<STR_LIT>''' <EOL> url = '<STR_LIT>' <EOL> json = self . _FetchUrl ( url ) <EOL> data = simplejson . loads ( json ) <EOL> self . _CheckForTwitterError ( data ) <EOL> return [ User . NewFromJsonDict ( x ) for x in data ] <EOL> def GetUser ( self , user ) : <EOL> '''<STR_LIT>''' <EOL> url = '<STR_LIT>' % user <EOL> json = self . _FetchUrl ( url ) <EOL> data = simplejson . loads ( json ) <EOL> self . _CheckForTwitterError ( data ) <EOL> return User . NewFromJsonDict ( data ) <EOL> def GetDirectMessages ( self , since = None , since_id = None , page = None ) : <EOL> '''<STR_LIT>''' <EOL> url = '<STR_LIT>' <EOL> if not self . _username : <EOL> raise TwitterError ( "<STR_LIT>" ) <EOL> parameters = { } <EOL> if since : <EOL> parameters [ '<STR_LIT>' ] = since <EOL> if since_id : <EOL> parameters [ '<STR_LIT>' ] = since_id <EOL> if page : <EOL> parameters [ '<STR_LIT>' ] = page <EOL> json = self . _FetchUrl ( url , parameters = parameters ) <EOL> data = simplejson . loads ( json ) <EOL> self . _CheckForTwitterError ( data ) <EOL> return [ DirectMessage . NewFromJsonDict ( x ) for x in data ] <EOL> def PostDirectMessage ( self , user , text ) : <EOL> '''<STR_LIT>''' <EOL> if not self . _username : <EOL> raise TwitterError ( "<STR_LIT>" ) <EOL> url = '<STR_LIT>' <EOL> data = { '<STR_LIT:text>' : text , '<STR_LIT:user>' : user } <EOL> json = self . _FetchUrl ( url , post_data = data ) <EOL> data = simplejson . loads ( json ) <EOL> self . _CheckForTwitterError ( data ) <EOL> return DirectMessage . NewFromJsonDict ( data ) <EOL> def DestroyDirectMessage ( self , id ) : <EOL> '''<STR_LIT>''' <EOL> url = '<STR_LIT>' % id <EOL> json = self . _FetchUrl ( url , post_data = { } ) <EOL> data = simplejson . loads ( json ) <EOL> self . _CheckForTwitterError ( data ) <EOL> return DirectMessage . NewFromJsonDict ( data ) <EOL> def CreateFriendship ( self , user ) : <EOL> '''<STR_LIT>''' <EOL> url = '<STR_LIT>' % user <EOL> json = self . _FetchUrl ( url , post_data = { } ) <EOL> data = simplejson . loads ( json ) <EOL> self . _CheckForTwitterError ( data ) <EOL> return User . NewFromJsonDict ( data ) <EOL> def DestroyFriendship ( self , user ) : <EOL> '''<STR_LIT>''' <EOL> url = '<STR_LIT>' % user <EOL> json = self . _FetchUrl ( url , post_data = { } ) <EOL> data = simplejson . loads ( json ) <EOL> self . _CheckForTwitterError ( data ) <EOL> return User . NewFromJsonDict ( data ) <EOL> def CreateFavorite ( self , status ) : <EOL> '''<STR_LIT>''' <EOL> url = '<STR_LIT>' % status . id <EOL> json = self . _FetchUrl ( url , post_data = { } ) <EOL> data = simplejson . loads ( json ) <EOL> self . _CheckForTwitterError ( data ) <EOL> return Status . NewFromJsonDict ( data ) <EOL> def DestroyFavorite ( self , status ) : <EOL> '''<STR_LIT>''' <EOL> url = '<STR_LIT>' % status . id <EOL> json = self . _FetchUrl ( url , post_data = { } ) <EOL> data = simplejson . loads ( json ) <EOL> self . _CheckForTwitterError ( data ) <EOL> return Status . NewFromJsonDict ( data ) <EOL> def GetUserByEmail ( self , email ) : <EOL> '''<STR_LIT>''' <EOL> url = '<STR_LIT>' % email <EOL> json = self . _FetchUrl ( url ) <EOL> data = simplejson . loads ( json ) <EOL> self . _CheckForTwitterError ( data ) <EOL> return User . NewFromJsonDict ( data ) <EOL> def VerifyCredentials ( self ) : <EOL> '''<STR_LIT>''' <EOL> if not self . _username : <EOL> raise TwitterError ( "<STR_LIT>" ) <EOL> url = '<STR_LIT>' <EOL> try : <EOL> json = self . _FetchUrl ( url , no_cache = True ) <EOL> except urllib2 . HTTPError , http_error : <EOL> if http_error . code == httplib . UNAUTHORIZED : <EOL> return None <EOL> else : <EOL> raise http_error <EOL> data = simplejson . loads ( json ) <EOL> self . _CheckForTwitterError ( data ) <EOL> return User . NewFromJsonDict ( data ) <EOL> def SetCredentials ( self , username , password ) : <EOL> '''<STR_LIT>''' <EOL> self . _username = username <EOL> self . _password = password <EOL> def ClearCredentials ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . _username = None <EOL> self . _password = None <EOL> def SetCache ( self , cache ) : <EOL> '''<STR_LIT>''' <EOL> if cache == DEFAULT_CACHE : <EOL> self . _cache = _FileCache ( ) <EOL> else : <EOL> self . _cache = cache <EOL> def SetUrllib ( self , urllib ) : <EOL> '''<STR_LIT>''' <EOL> self . _urllib = urllib <EOL> def SetCacheTimeout ( self , cache_timeout ) : <EOL> '''<STR_LIT>''' <EOL> self . _cache_timeout = cache_timeout <EOL> def SetUserAgent ( self , user_agent ) : <EOL> '''<STR_LIT>''' <EOL> self . _request_headers [ '<STR_LIT>' ] = user_agent <EOL> def SetXTwitterHeaders ( self , client , url , version ) : <EOL> '''<STR_LIT>''' <EOL> self . _request_headers [ '<STR_LIT>' ] = client <EOL> self . _request_headers [ '<STR_LIT>' ] = url <EOL> self . _request_headers [ '<STR_LIT>' ] = version <EOL> def SetSource ( self , source ) : <EOL> '''<STR_LIT>''' <EOL> self . _default_params [ '<STR_LIT:source>' ] = source <EOL> def _BuildUrl ( self , url , path_elements = None , extra_params = None ) : <EOL> ( scheme , netloc , path , params , query , fragment ) = urlparse . urlparse ( url ) <EOL> if path_elements : <EOL> p = [ i for i in path_elements if i ] <EOL> if not path . endswith ( '<STR_LIT:/>' ) : <EOL> path += '<STR_LIT:/>' <EOL> path += '<STR_LIT:/>' . join ( p ) <EOL> if extra_params and len ( extra_params ) > <NUM_LIT:0> : <EOL> extra_query = self . _EncodeParameters ( extra_params ) <EOL> if query : <EOL> query += '<STR_LIT:&>' + extra_query <EOL> else : <EOL> query = extra_query <EOL> return urlparse . urlunparse ( ( scheme , netloc , path , params , query , fragment ) ) <EOL> def _InitializeRequestHeaders ( self , request_headers ) : <EOL> if request_headers : <EOL> self . _request_headers = request_headers <EOL> else : <EOL> self . _request_headers = { } <EOL> def _InitializeUserAgent ( self ) : <EOL> user_agent = '<STR_LIT>' % ( self . _urllib . __version__ , __version__ ) <EOL> self . SetUserAgent ( user_agent ) <EOL> def _InitializeDefaultParameters ( self ) : <EOL> self . _default_params = { } <EOL> def _AddAuthorizationHeader ( self , username , password ) : <EOL> if username and password : <EOL> basic_auth = base64 . encodestring ( '<STR_LIT>' % ( username , password ) ) [ : - <NUM_LIT:1> ] <EOL> self . _request_headers [ '<STR_LIT>' ] = '<STR_LIT>' % basic_auth <EOL> def _RemoveAuthorizationHeader ( self ) : <EOL> if self . _request_headers and '<STR_LIT>' in self . _request_headers : <EOL> del self . _request_headers [ '<STR_LIT>' ] <EOL> def _GetOpener ( self , url , username = None , password = None ) : <EOL> if username and password : <EOL> self . _AddAuthorizationHeader ( username , password ) <EOL> handler = self . _urllib . HTTPBasicAuthHandler ( ) <EOL> ( scheme , netloc , path , params , query , fragment ) = urlparse . urlparse ( url ) <EOL> handler . add_password ( Api . _API_REALM , netloc , username , password ) <EOL> opener = self . _urllib . build_opener ( handler ) <EOL> else : <EOL> opener = self . _urllib . build_opener ( ) <EOL> opener . addheaders = self . _request_headers . items ( ) <EOL> return opener <EOL> def _Encode ( self , s ) : <EOL> if self . _input_encoding : <EOL> return unicode ( s , self . _input_encoding ) . encode ( '<STR_LIT:utf-8>' ) <EOL> else : <EOL> return unicode ( s ) . encode ( '<STR_LIT:utf-8>' ) <EOL> def _EncodeParameters ( self , parameters ) : <EOL> '''<STR_LIT>''' <EOL> if parameters is None : <EOL> return None <EOL> else : <EOL> return urllib . urlencode ( dict ( [ ( k , self . _Encode ( v ) ) for k , v in parameters . items ( ) if v is not None ] ) ) <EOL> def _EncodePostData ( self , post_data ) : <EOL> '''<STR_LIT>''' <EOL> if post_data is None : <EOL> return None <EOL> else : <EOL> return urllib . urlencode ( dict ( [ ( k , self . _Encode ( v ) ) for k , v in post_data . items ( ) ] ) ) <EOL> def _CheckForTwitterError ( self , data ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT:error>' in data : <EOL> raise TwitterError ( data [ '<STR_LIT:error>' ] ) <EOL> def _FetchUrl ( self , <EOL> url , <EOL> post_data = None , <EOL> parameters = None , <EOL> no_cache = None ) : <EOL> '''<STR_LIT>''' <EOL> extra_params = { } <EOL> if self . _default_params : <EOL> extra_params . update ( self . _default_params ) <EOL> if parameters : <EOL> extra_params . update ( parameters ) <EOL> url = self . _BuildUrl ( url , extra_params = extra_params ) <EOL> opener = self . _GetOpener ( url , username = self . _username , password = self . _password ) <EOL> encoded_post_data = self . _EncodePostData ( post_data ) <EOL> if encoded_post_data or no_cache or not self . _cache or not self . _cache_timeout : <EOL> url_data = opener . open ( url , encoded_post_data ) . read ( ) <EOL> opener . close ( ) <EOL> else : <EOL> if self . _username : <EOL> key = self . _username + '<STR_LIT::>' + url <EOL> else : <EOL> key = url <EOL> last_cached = self . _cache . GetCachedTime ( key ) <EOL> if not last_cached or time . time ( ) >= last_cached + self . _cache_timeout : <EOL> url_data = opener . open ( url , encoded_post_data ) . read ( ) <EOL> opener . close ( ) <EOL> self . _cache . Set ( key , url_data ) <EOL> else : <EOL> url_data = self . _cache . Get ( key ) <EOL> return url_data <EOL> class _FileCacheError ( Exception ) : <EOL> '''<STR_LIT>''' <EOL> class _FileCache ( object ) : <EOL> DEPTH = <NUM_LIT:3> <EOL> def __init__ ( self , root_directory = None ) : <EOL> self . _InitializeRootDirectory ( root_directory ) <EOL> def Get ( self , key ) : <EOL> path = self . _GetPath ( key ) <EOL> if os . path . exists ( path ) : <EOL> return open ( path ) . read ( ) <EOL> else : <EOL> return None <EOL> def Set ( self , key , data ) : <EOL> path = self . _GetPath ( key ) <EOL> directory = os . path . dirname ( path ) <EOL> if not os . path . exists ( directory ) : <EOL> os . makedirs ( directory ) <EOL> if not os . path . isdir ( directory ) : <EOL> raise _FileCacheError ( '<STR_LIT>' % directory ) <EOL> temp_fd , temp_path = tempfile . mkstemp ( ) <EOL> temp_fp = os . fdopen ( temp_fd , '<STR_LIT:w>' ) <EOL> temp_fp . write ( data ) <EOL> temp_fp . close ( ) <EOL> if not path . startswith ( self . _root_directory ) : <EOL> raise _FileCacheError ( '<STR_LIT>' % <EOL> ( path , self . _root_directory ) ) <EOL> if os . path . exists ( path ) : <EOL> os . remove ( path ) <EOL> os . rename ( temp_path , path ) <EOL> def Remove ( self , key ) : <EOL> path = self . _GetPath ( key ) <EOL> if not path . startswith ( self . _root_directory ) : <EOL> raise _FileCacheError ( '<STR_LIT>' % <EOL> ( path , self . _root_directory ) ) <EOL> if os . path . exists ( path ) : <EOL> os . remove ( path ) <EOL> def GetCachedTime ( self , key ) : <EOL> path = self . _GetPath ( key ) <EOL> if os . path . exists ( path ) : <EOL> return os . path . getmtime ( path ) <EOL> else : <EOL> return None <EOL> def _GetUsername ( self ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> return os . getenv ( '<STR_LIT>' ) or os . getenv ( '<STR_LIT>' ) or os . getenv ( '<STR_LIT>' ) or os . getlogin ( ) or '<STR_LIT>' <EOL> except ( IOError , OSError ) , e : <EOL> return '<STR_LIT>' <EOL> def _GetTmpCachePath ( self ) : <EOL> username = self . _GetUsername ( ) <EOL> cache_directory = '<STR_LIT>' + username <EOL> return os . path . join ( tempfile . gettempdir ( ) , cache_directory ) <EOL> def _InitializeRootDirectory ( self , root_directory ) : <EOL> if not root_directory : <EOL> root_directory = self . _GetTmpCachePath ( ) <EOL> root_directory = os . path . abspath ( root_directory ) <EOL> if not os . path . exists ( root_directory ) : <EOL> os . mkdir ( root_directory ) <EOL> if not os . path . isdir ( root_directory ) : <EOL> raise _FileCacheError ( '<STR_LIT>' % <EOL> root_directory ) <EOL> self . _root_directory = root_directory <EOL> def _GetPath ( self , key ) : <EOL> try : <EOL> hashed_key = md5 ( key ) . hexdigest ( ) <EOL> except TypeError : <EOL> hashed_key = md5 . new ( key ) . hexdigest ( ) <EOL> return os . path . join ( self . _root_directory , <EOL> self . _GetPrefix ( hashed_key ) , <EOL> hashed_key ) <EOL> def _GetPrefix ( self , hashed_key ) : <EOL> return os . path . sep . join ( hashed_key [ <NUM_LIT:0> : _FileCache . DEPTH ] ) </s>
