#NEWLINE# def main( options ) : #NEWLINE# #INDENT# global VERBOSE #NEWLINE# account = options . account_id #NEWLINE# headers = options . headers #NEWLINE# if ( options . veryverbose is True ) : #NEWLINE# #INDENT# VERBOSE = 2 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( options . verbose is True ) : #NEWLINE# #INDENT# VERBOSE = 1 #NEWLINE# #UNINDENT# #UNINDENT# start = time . clock ( ) #NEWLINE# user_twurl = twurlauth ( ) #NEWLINE# print ( ( 'Fetching stats for :account_id %s' % account ) ) #NEWLINE# linesep ( ) #NEWLINE# now = datetime . datetime . utcnow ( ) #NEWLINE# if options . start_time : #NEWLINE# #INDENT# start_time = options . start_time #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# st = ( datetime . datetime . utcnow ( ) - datetime . timedelta ( days = 7 ) ) #NEWLINE# st = st . replace ( minute = 0 , second = 0 , microsecond = 0 ) #NEWLINE# start_time = st . isoformat ( ) #NEWLINE# #UNINDENT# if options . end_time : #NEWLINE# #INDENT# end_time = options . end_time #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# et = datetime . datetime . utcnow ( ) #NEWLINE# et = et . replace ( minute = 0 , second = 0 , microsecond = 0 ) #NEWLINE# et -= datetime . timedelta ( seconds = 1 ) #NEWLINE# end_time = et . isoformat ( ) #NEWLINE# #UNINDENT# print ( ( 'Current time:\t%s' % now ) ) #NEWLINE# print ( ( 'Start time:\t%s' % start_time ) ) #NEWLINE# print ( ( 'End time:\t%s' % end_time ) ) #NEWLINE# linesep ( ) #NEWLINE# resource_path = ( '/0/accounts/%s' % account ) #NEWLINE# data = get_data ( user_twurl , 'GET' , headers , ( DOMAIN + resource_path ) ) #NEWLINE# if ( len ( data ) == 0 ) : #NEWLINE# #INDENT# print ( ( 'ERROR: Could not locate :account_id %s' % account ) ) #NEWLINE# sys . exit ( 0 ) #NEWLINE# #UNINDENT# total_query_count = 0 #NEWLINE# total_request_cost = 0 #NEWLINE# total_rate_limited_query_count = 0 #NEWLINE# if ( len ( options . campaigns ) > 0 ) : #NEWLINE# #INDENT# print ( ( '\tfetching stats for %s campaigns' % len ( options . campaigns ) ) ) #NEWLINE# ( query_count , cost_total , rate_limited_query_count ) = gather_stats ( user_twurl , headers , account , 'campaigns' , start_time , end_time , options . campaigns ) #NEWLINE# for campaign_id in options . campaigns : #NEWLINE# #INDENT# st = re . sub ( '-\\d{4}' , '' , start_time ) #NEWLINE# et = re . sub ( '-\\d{4}' , '' , end_time ) #NEWLINE# params = { #NEWLINE# #INDENT# 'campaign' : int ( campaign_id , 36 ) , #NEWLINE# 'segment' : 'tweets' , #NEWLINE# 'endString' : '{0}.000' . format ( et ) , #NEWLINE# 'startString' : '{0}.000' . format ( st ) , #NEWLINE# #UNINDENT# } #NEWLINE# qs = urllib . urlencode ( params ) #NEWLINE# ui_url = ( 'https://ads.twitter.com/accounts/%s/campaigns_dashboard?%s' % ( account , qs ) ) #NEWLINE# print ( ( 'UI URL: %s' % ui_url ) ) #NEWLINE# #UNINDENT# total_query_count += query_count #NEWLINE# total_request_cost += cost_total #NEWLINE# #UNINDENT# linesep ( ) #NEWLINE# print ( ( 'Total Stats Queries:\t\t%s' % total_query_count ) ) #NEWLINE# print ( ( 'Total Stats Request Cost:\t%s' % total_request_cost ) ) #NEWLINE# if ( VERBOSE > 0 ) : #NEWLINE# #INDENT# print ( ( 'Avg Cost per Query:\t\t%s' % str ( ( total_request_cost / total_query_count ) ) ) ) #NEWLINE# #UNINDENT# print ( ( 'Queries Rate Limited:\t\t%s' % total_rate_limited_query_count ) ) #NEWLINE# linesep ( ) #NEWLINE# elapsed = ( time . clock ( ) - start ) #NEWLINE# print ( ( 'Time elapsed:\t\t\t%s' % elapsed ) )