<s> """<STR_LIT>""" <EOL> import httplib2 <EOL> from BeautifulSoup import Tag , SoupStrainer , BeautifulSoup as soup <EOL> from causal . main . decorators import can_view_service <EOL> from causal . main . utils . services import get_model_instance , settings_redirect , check_is_service_id , get_url <EOL> from causal . main . utils . views import render <EOL> from causal . main . models import UserService , Auth <EOL> from datetime import datetime , date , timedelta <EOL> from django . core . urlresolvers import reverse <EOL> from django . contrib import messages <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . shortcuts import redirect , get_object_or_404 <EOL> from django . utils . datastructures import SortedDict <EOL> PACKAGE_NAME = '<STR_LIT>' <EOL> @ login_required ( redirect_field_name = '<STR_LIT>' ) <EOL> def auth ( request ) : <EOL> """<STR_LIT>""" <EOL> service = get_model_instance ( request . user , PACKAGE_NAME ) <EOL> if service and request . method == '<STR_LIT:POST>' : <EOL> username = request . POST [ '<STR_LIT:username>' ] <EOL> if username : <EOL> url = "<STR_LIT>" % ( username , ) <EOL> links = SoupStrainer ( '<STR_LIT>' ) <EOL> content = get_url ( url , json = False ) <EOL> parsed_links = [ tag for tag in soup ( str ( content ) , parseOnlyThese = links ) ] <EOL> if parsed_links : <EOL> try : <EOL> user_id = parsed_links [ <NUM_LIT:2> ] . attrs [ <NUM_LIT:2> ] [ <NUM_LIT:1> ] . split ( '<STR_LIT>' ) [ <NUM_LIT:1> ] <EOL> if not service . auth : <EOL> auth_handler = Auth ( ) <EOL> else : <EOL> auth_handler = service . auth <EOL> auth_handler . username = username <EOL> auth_handler . secret = user_id <EOL> auth_handler . save ( ) <EOL> if not service . auth : <EOL> service . auth = auth_handler <EOL> service . setup = True <EOL> service . public = True <EOL> service . save ( ) <EOL> except : <EOL> messages . error ( <EOL> request , <EOL> '<STR_LIT>' % ( username , ) <EOL> ) <EOL> else : <EOL> messages . error ( <EOL> request , <EOL> '<STR_LIT>' % ( username , ) <EOL> ) <EOL> else : <EOL> messages . error ( request , "<STR_LIT>" ) <EOL> return redirect ( settings_redirect ( request ) ) <EOL> @ can_view_service <EOL> def stats ( request , service_id ) : <EOL> """<STR_LIT>""" <EOL> service = get_object_or_404 ( UserService , pk = service_id ) <EOL> if check_is_service_id ( service , PACKAGE_NAME ) : <EOL> shares = service . handler . get_items ( date . today ( ) - timedelta ( days = <NUM_LIT:7> ) ) <EOL> sources = { } <EOL> for share in shares : <EOL> if sources . has_key ( share . source ) : <EOL> sources [ share . source ] = sources [ share . source ] + <NUM_LIT:1> <EOL> else : <EOL> sources [ share . source ] = <NUM_LIT:1> <EOL> sources = SortedDict ( <EOL> sorted ( sources . items ( ) , reverse = True , key = lambda x : x [ <NUM_LIT:1> ] ) <EOL> ) <EOL> return render ( <EOL> request , <EOL> { <EOL> '<STR_LIT>' : shares , <EOL> '<STR_LIT>' : sources , <EOL> } , <EOL> '<STR_LIT>' <EOL> ) <EOL> else : <EOL> return redirect ( '<STR_LIT>' % ( request . user . username , ) ) </s>
