<s> import time <EOL> from dateutil import parser <EOL> from datetime import datetime , timedelta , date <EOL> from causal . main . handlers import OAuthServiceHandler <EOL> from causal . main . models import OAuth , RequestToken , AccessToken , UserService , ServiceItem <EOL> from causal . main . utils . services import get_model_instance , get_url , generate_days_dict <EOL> from causal . main . exceptions import LoggedServiceError <EOL> from django . utils . datastructures import SortedDict <EOL> class ServiceHandler ( OAuthServiceHandler ) : <EOL> """<STR_LIT>""" <EOL> def setup ( self ) : <EOL> """<STR_LIT>""" <EOL> self . disable_ssl = True <EOL> display_name = '<STR_LIT>' <EOL> def _get_feed ( self ) : <EOL> """<STR_LIT>""" <EOL> username , name = self . _get_details ( ) <EOL> feed = self . get_data ( '<STR_LIT>' % ( username ) ) <EOL> return feed <EOL> def get_items ( self , since ) : <EOL> """<STR_LIT>""" <EOL> feed = self . _get_feed ( ) <EOL> if feed : <EOL> return self . _convert_feed ( feed , since ) <EOL> def get_stats_items ( self , since ) : <EOL> """<STR_LIT>""" <EOL> feed = self . _get_feed ( ) <EOL> if feed : <EOL> return self . _convert_stats_feed ( feed , since ) <EOL> def _get_repos ( self , since ) : <EOL> """<STR_LIT>""" <EOL> repos_url = "<STR_LIT>" % ( self . service . auth . access_token . oauth_token ) <EOL> repos = get_url ( repos_url ) <EOL> updated_repos = [ ] <EOL> for repo in repos : <EOL> pushed = datetime . strptime ( repo [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> if pushed . date ( ) > since : <EOL> updated_repos . append ( repo ) <EOL> return updated_repos <EOL> def _convert_feed ( self , feed , since ) : <EOL> """<STR_LIT>""" <EOL> items = [ ] <EOL> for entry in feed : <EOL> if entry . has_key ( '<STR_LIT>' ) and entry [ '<STR_LIT>' ] : <EOL> if entry . has_key ( '<STR_LIT>' ) : <EOL> created = self . _convert_date ( entry [ '<STR_LIT>' ] ) <EOL> if created . date ( ) >= since : <EOL> item = ServiceItem ( ) <EOL> self . _set_title_body ( entry , item ) <EOL> item . created = created <EOL> if entry [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> item . link_back = entry [ '<STR_LIT>' ] [ '<STR_LIT:url>' ] <EOL> else : <EOL> if entry . has_key ( '<STR_LIT:url>' ) : <EOL> item . link_back = entry [ '<STR_LIT:url>' ] <EOL> item . service = self . service <EOL> items . append ( item ) <EOL> return items <EOL> def _convert_date ( self , date_time ) : <EOL> """<STR_LIT>""" <EOL> if date_time : <EOL> if date_time . find ( '<STR_LIT:T>' ) < <NUM_LIT:0> : <EOL> date , time , offset = date_time . rsplit ( '<STR_LIT:U+0020>' ) <EOL> direction = offset [ <NUM_LIT:0> ] <EOL> offset = offset [ <NUM_LIT:1> : ] <EOL> time_format = '<STR_LIT>' <EOL> else : <EOL> date , time_and_offset = date_time . rsplit ( '<STR_LIT:T>' ) <EOL> time = time_and_offset [ : <NUM_LIT:8> ] <EOL> offset = time_and_offset [ <NUM_LIT:9> : ] <EOL> direction = time_and_offset [ <NUM_LIT:8> ] <EOL> time_format = '<STR_LIT>' <EOL> if direction == '<STR_LIT:->' : <EOL> direction = '<STR_LIT:+>' <EOL> elif direction == '<STR_LIT:+>' : <EOL> direction = '<STR_LIT:->' <EOL> time_offset = timedelta ( hours = int ( direction + offset [ : <NUM_LIT:2> ] ) ) <EOL> return datetime . strptime ( date + '<STR_LIT:U+0020>' + time , time_format ) + time_offset <EOL> def _convert_stats_feed ( self , feed , since ) : <EOL> """<STR_LIT>""" <EOL> items = [ ] <EOL> commits = [ ] <EOL> commit_times = { } <EOL> repos = { } <EOL> avatar = "<STR_LIT>" <EOL> list_of_commits = { } <EOL> username , name = self . _get_details ( ) <EOL> if not username or not feed : <EOL> return <EOL> if feed and feed [ <NUM_LIT:0> ] [ '<STR_LIT>' ] . has_key ( '<STR_LIT>' ) : <EOL> avatar = '<STR_LIT>' % ( feed [ <NUM_LIT:0> ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] , ) <EOL> days_committed = generate_days_dict ( ) <EOL> for entry in feed : <EOL> if entry [ '<STR_LIT>' ] : <EOL> dated , time , offset = entry [ '<STR_LIT>' ] . rsplit ( '<STR_LIT:U+0020>' ) <EOL> if entry . has_key ( '<STR_LIT>' ) : <EOL> created = self . _convert_date ( entry [ '<STR_LIT>' ] ) <EOL> if created . date ( ) >= since : <EOL> if entry . has_key ( '<STR_LIT>' ) and entry [ '<STR_LIT>' ] . has_key ( '<STR_LIT:name>' ) and entry [ '<STR_LIT>' ] . has_key ( '<STR_LIT>' ) : <EOL> url = "<STR_LIT>" % ( <EOL> entry [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> entry [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] ) <EOL> repo = self . get_data ( url ) <EOL> if repo : <EOL> repos [ entry [ '<STR_LIT>' ] [ '<STR_LIT>' ] + entry [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] ] = repo <EOL> if entry [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> for commit in entry [ '<STR_LIT>' ] [ '<STR_LIT>' ] : <EOL> if not list_of_commits . has_key ( commit [ <NUM_LIT:0> ] ) : <EOL> list_of_commits [ commit [ <NUM_LIT:0> ] ] = True <EOL> item = ServiceItem ( ) <EOL> if name == commit [ - <NUM_LIT:1> ] : <EOL> commit_detail = None <EOL> if entry . has_key ( '<STR_LIT>' ) : <EOL> url = "<STR_LIT>" % ( <EOL> entry [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> entry [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] , <EOL> commit [ <NUM_LIT:0> ] ) <EOL> commit_detail = self . get_data ( url ) <EOL> item . body = '<STR_LIT>' % ( commit_detail [ '<STR_LIT:message>' ] ) <EOL> item . title = "<STR_LIT>" % ( entry [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] ) <EOL> else : <EOL> if entry [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> item . title = "<STR_LIT>" % ( entry [ '<STR_LIT:url>' ] . split ( '<STR_LIT:/>' ) [ <NUM_LIT:4> ] ) <EOL> item . body = entry [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] <EOL> if commit_detail and commit_detail . has_key ( '<STR_LIT>' ) and commit_detail [ '<STR_LIT>' ] . has_key ( '<STR_LIT:date>' ) : <EOL> item . created = self . _convert_date ( commit_detail [ '<STR_LIT>' ] [ '<STR_LIT:date>' ] ) <EOL> else : <EOL> item . created = self . _convert_date ( entry [ '<STR_LIT>' ] ) <EOL> if repo and repo . has_key ( '<STR_LIT>' ) : <EOL> item . private = repo [ '<STR_LIT>' ] <EOL> if commit_detail and commit_detail . has_key ( '<STR_LIT:url>' ) : <EOL> item . link_back = commit_detail [ '<STR_LIT:url>' ] <EOL> else : <EOL> item . link_back = entry [ '<STR_LIT:url>' ] <EOL> item . service = self . service <EOL> commits . append ( item ) <EOL> if days_committed . has_key ( item . created_local . date ( ) ) : <EOL> days_committed [ item . created_local . date ( ) ] = days_committed [ item . created_local . date ( ) ] + <NUM_LIT:1> <EOL> hour = item . created_local . strftime ( '<STR_LIT>' ) <EOL> if commit_times . has_key ( hour ) : <EOL> commit_times [ hour ] += <NUM_LIT:1> <EOL> else : <EOL> commit_times [ hour ] = <NUM_LIT:1> <EOL> del ( item ) <EOL> else : <EOL> item = ServiceItem ( ) <EOL> self . _set_title_body ( entry , item ) <EOL> item . created = created <EOL> if entry . has_key ( '<STR_LIT:url>' ) : <EOL> item . link_back = entry [ '<STR_LIT:url>' ] <EOL> item . service = self . service <EOL> items . append ( item ) <EOL> commit_times = SortedDict ( sorted ( <EOL> commit_times . items ( ) , <EOL> reverse = True , <EOL> key = lambda x : x [ <NUM_LIT:1> ] <EOL> ) ) <EOL> days_committed = SortedDict ( sorted ( days_committed . items ( ) , reverse = False , key = lambda x : x [ <NUM_LIT:0> ] ) ) <EOL> max_commits_on_a_day = SortedDict ( sorted ( days_committed . items ( ) , reverse = True , key = lambda x : x [ <NUM_LIT:1> ] ) ) <EOL> max_commits_on_a_day = max_commits_on_a_day [ max_commits_on_a_day . keyOrder [ <NUM_LIT:0> ] ] + <NUM_LIT:1> <EOL> commits . sort ( key = lambda commit : commit . created_local , reverse = True ) <EOL> return { '<STR_LIT>' : items , <EOL> '<STR_LIT>' : commits , <EOL> '<STR_LIT>' : avatar , <EOL> '<STR_LIT>' : commit_times , <EOL> '<STR_LIT>' : self . _most_common_commit_time ( commit_times ) , <EOL> '<STR_LIT>' : days_committed , <EOL> '<STR_LIT>' : max_commits_on_a_day , <EOL> '<STR_LIT>' : repos } <EOL> def _most_common_commit_time ( self , commits ) : <EOL> """<STR_LIT>""" <EOL> if not commits : <EOL> return False <EOL> hour = commits . keys ( ) [ <NUM_LIT:0> ] <EOL> if hour == '<STR_LIT>' : <EOL> return '<STR_LIT>' % ( hour ) <EOL> else : <EOL> return '<STR_LIT>' % ( hour , int ( hour ) + <NUM_LIT:1> ) <EOL> def _set_title_body ( self , entry , item ) : <EOL> """<STR_LIT>""" <EOL> item . body = '<STR_LIT>' <EOL> try : <EOL> if entry [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> if entry [ '<STR_LIT>' ] . has_key ( '<STR_LIT:object_name>' ) : <EOL> item . title = "<STR_LIT>" % ( entry [ '<STR_LIT>' ] [ '<STR_LIT:object_name>' ] , entry [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] ) <EOL> else : <EOL> if entry . has_key ( '<STR_LIT>' ) : <EOL> item . title = "<STR_LIT>" % ( entry [ '<STR_LIT>' ] [ '<STR_LIT>' ] , entry [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] ) <EOL> else : <EOL> item . title = "<STR_LIT>" % ( entry [ '<STR_LIT>' ] [ '<STR_LIT>' ] , entry [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> elif entry [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> item . title = "<STR_LIT>" % ( entry [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> elif entry [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> url = '<STR_LIT>' % ( <EOL> entry [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> entry [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] , <EOL> str ( entry [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) ) <EOL> item . title = '<STR_LIT>' % ( url , str ( entry [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) , entry [ '<STR_LIT>' ] [ '<STR_LIT:action>' ] ) <EOL> elif entry [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> item . title = "<STR_LIT>" % ( entry [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] ) <EOL> elif entry [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> if entry . has_key ( '<STR_LIT>' ) : <EOL> item . title = "<STR_LIT>" % ( entry [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] , entry [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] ) <EOL> else : <EOL> item . title = entry [ '<STR_LIT:url>' ] . split ( '<STR_LIT:/>' ) [ <NUM_LIT:4> ] <EOL> elif entry [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> item . title = "<STR_LIT>" % ( entry [ '<STR_LIT>' ] [ '<STR_LIT:object_name>' ] , entry [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] ) <EOL> elif entry [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> item . title = "<STR_LIT>" % ( entry [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] ) <EOL> elif entry [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> item . title = "<STR_LIT>" % ( entry [ '<STR_LIT>' ] [ '<STR_LIT:target>' ] [ '<STR_LIT>' ] ) <EOL> elif entry [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> item . title = "<STR_LIT>" % ( entry [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> elif entry [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> item . title = "<STR_LIT>" % ( entry [ '<STR_LIT>' ] [ '<STR_LIT>' ] , entry [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> elif entry [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> pass <EOL> elif entry [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> comments = get_url ( '<STR_LIT>' % ( <EOL> entry [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> entry [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] , <EOL> entry [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> ) ) <EOL> item . title = '<STR_LIT>' % ( comments [ '<STR_LIT:body>' ] , entry [ '<STR_LIT:url>' ] , entry [ '<STR_LIT:url>' ] . split ( '<STR_LIT:#>' ) [ <NUM_LIT:0> ] . rsplit ( '<STR_LIT:/>' , <NUM_LIT:1> ) [ <NUM_LIT:1> ] ) <EOL> elif entry [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> item . title = "<STR_LIT>" % ( entry [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] ) <EOL> else : <EOL> item . title = "<STR_LIT>" <EOL> except : <EOL> item . title = "<STR_LIT>" <EOL> def _get_details ( self ) : <EOL> """<STR_LIT>""" <EOL> user_json = self . get_data ( '<STR_LIT>' ) <EOL> if user_json and user_json . has_key ( '<STR_LIT>' ) and user_json . has_key ( '<STR_LIT:name>' ) : <EOL> return user_json [ '<STR_LIT>' ] , user_json [ '<STR_LIT:name>' ] </s>
