Get google adsense earnings from api in python

Adsense Management API

The google adsense management api can be used to fetch adsense reports inside a web or desktop application. In this article we are going to see how to use the api in python to fetch the earnings. The documentation is available here. The code shown here is build using the samples.

Setup Client ID at google api console

Your python application that will try to fetch the adsense reports for some account is a client and needs to be identified properly. To do this, it needs a client id. To do this visit the google api console.

1. In there, click the "Services" tab on the left. Enable the Adsense Management API if it is not already enabled.

2. Then click API Access on the left pane. Click Create another Client ID. This will create a client id that would be used by the python application to identify itself at google and do various api calls.

3. Once the client id is created, click Download JSON to download the client_secrets.json file. This json file contains the client id, client secret and details and will be embedded inside the python app.

Install the google api client library

The code to fetch adsense reports would need the google api python client library. It can be installed from the command by issuing either of the following commands

$ sudo pip install --upgrade google-api-python-client

OR

$ sudo easy_install --upgrade google-api-python-client

Code

The code contains 2 discrete parts. The first part does the authentication and provides a service object. The second part uses the service object to fetch reports. The reports for example are in json format which can be read and processed easily.

Create a directory somewhere and put the client_secrets.json file inside it. Create another file called sample_utils.py and paste inside it the code shown next. The first file is sample_utils.py that does the authentication stuff etc.

#!/usr/bin/python

"""
Auxiliary file for AdSense Management API code samples.
Handles various tasks to do with logging, authentication and initialization.
"""

# python libs
import logging , os , sys

#google libraries
from apiclient.discovery import build
import gflags
import httplib2

# to get this install, easy_install --upgrade google-api-python-client
from oauth2client.client import flow_from_clientsecrets
from oauth2client.client import OOB_CALLBACK_URN
from oauth2client.file import Storage
from oauth2client.tools import run

FLAGS = gflags.FLAGS

# CLIENT_SECRETS, name of a file containing the OAuth 2.0 information for this
# application, including client_id and client_secret, which are found
# on the API Access tab on the Google APIs
# Console <http://code.google.com/apis/console>
CLIENT_SECRETS = 'client_secrets.json'

# Helpful message to display in the browser if the CLIENT_SECRETS file is missing.
MISSING_CLIENT_SECRETS_MESSAGE = """
WARNING: Please configure OAuth 2.0

To make this sample run you will need to populate the client_secrets.json file
found at:

   %s

with information from the APIs Console <https://code.google.com/apis/console>.

""" % os.path.join(os.path.dirname(__file__), CLIENT_SECRETS)

# Set up a Flow object to be used if we need to authenticate.
FLOW = flow_from_clientsecrets(CLIENT_SECRETS,
    scope='https://www.googleapis.com/auth/adsense.readonly',
    redirect_uri=OOB_CALLBACK_URN,
    message=MISSING_CLIENT_SECRETS_MESSAGE)

# The gflags module makes defining command-line options easy for applications.
# Run this program with the '--help' argument to see all the flags that it
# understands.
gflags.DEFINE_enum('logging_level', 'ERROR',
                   ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
                   'Set the level of logging detail.')


def process_flags(argv):
	"""Uses the command-line flags to set the logging level."""
	
	# Let the gflags module process the command-line arguments.
	try:
		argv = FLAGS(argv)
	except gflags.FlagsError, e:
		print '%snUsage: %s ARGSn%s' % (e, argv[0], FLAGS)
		sys.exit(1)

	# Set the logging according to the command-line flag.
	logging.getLogger().setLevel(getattr(logging, FLAGS.logging_level))


def prepare_credentials():
	"""Handles auth. Reuses credentialss if available or runs the auth flow."""

	# If the credentials don't exist or are invalid run through the native client
	# flow. The Storage object will ensure that if successful the good
	# Credentials will get written back to a file.
	storage = Storage('adsense.dat')
	credentials = storage.get()
	
	if credentials is None or credentials.invalid:
		credentials = run(FLOW, storage)
	
	return credentials


def retrieve_service(http):
	"""Retrieves an AdSense Management API service via the discovery service."""

	# Construct a service object via the discovery service.
	service = build("adsense", "v1.1", http=http)
	return service


def initialize_service():
	"""Builds instance of service from discovery data and does auth."""

	# Create an httplib2.Http object to handle our HTTP requests.
	http = httplib2.Http()

	# Prepare credentials, and authorize HTTP object with them.
	credentials = prepare_credentials()
	http = credentials.authorize(http)

	# Retrieve service.
	return retrieve_service(http)
 






The next file is the report.py which fetches the reports and prints them out

#!/usr/bin/python

# python libraries
import sys, datetime, json
from pytz import timezone

#google libraries
from oauth2client.client import AccessTokenRefreshError

# app files
import sample_utils

def main(argv):
	get_stats()
	
def get_stats() :
	
	# Authenticate and construct service.
	service = sample_utils.initialize_service()
	
	#client_id - get this from your adsense login
	ad_client_id = 'ca-pub-0000000000000000'
	
	#adsense timezone is usa pacific http://support.google.com/adsense/bin/answer.py?hl=en&answer=59143
	now_pacific = datetime.datetime.now(timezone('US/Pacific'))
	today_pacific = now_pacific.strftime('%Y-%m-%d')
	
	yesterday_pacific = now_pacific - datetime.timedelta(1)
	yesterday_pacific = yesterday_pacific.strftime('%Y-%m-%d')
	
	month_first = now_pacific.strftime('%Y-%m-01')
	
	# print the dates of today and yesterday, these are used to define timespans for the report
	print today_pacific
	print yesterday_pacific
	
	#print today_pacific
	try:
		all_data = {}
		
		sets = {
			'today' : [today_pacific , today_pacific] ,
			'yesterday' : [yesterday_pacific , yesterday_pacific] ,
			'this_month' : [month_first , today_pacific]
			}
		
		for k,v in sets.items() :
			# Retrieve report. result is a json object
			result = service.reports().generate(
				startDate = v[0] , 
				endDate = v[1] ,
				filter=['AD_CLIENT_ID==' + ad_client_id],
				metric=['PAGE_VIEWS', 'CLICKS', 'PAGE_VIEWS_CTR', 'COST_PER_CLICK', 'AD_REQUESTS_RPM', 'EARNINGS'],
				#dimension=['DATE'],
				#sort=['+DATE']
			).execute()
			
			#dumping json object - you may want to dump, to see the structure and use next
			#print json.dumps(result, sort_keys=True, indent=4)
			
			# Display headers
			'''for header in result['headers']:
				print '%15s' % header['name'],
			print'''
			data = {}
			# Display results
			if 'rows' in result :
				row = result['rows'][0]
				
				data['page_view'] = row[0]
				data['clicks'] = row[1]
				data['ctr'] = row[2]
				data['cpc'] = row[3]
				data['rpm'] = row[4]
				data['earnings'] = row[5]
				
				'''for row in result['rows']:
					for column in row:
						print '%15s' % column ,
					print'''
			
			all_data[k] = data
		
		print str(all_data)
	
	#authorization problem
	except AccessTokenRefreshError:
		print ('The credentials have been revoked or expired, please re-run the application to re-authorize')

# main function
if __name__ == '__main__':
	main(sys.argv) 

Fill your adsense client id in the line

ad_client_id = 'ca-pub-0000000000000000'

Output

Now run the program from a terminal like this

$ python report.py

When running the first time, the program would popup a browser that would take to google login page and ask whether the program should be allowed to access the adsense data. Once approved the program would output the adsense earnings. Something like this

$ python report.py
2013-03-13
2013-03-12
{'this_month': {'ctr': u'0.0076', 'cpc': u'0.20', 'earnings': u'51.33', 'rpm': u'0.52', 'clicks': u'258', 'page_view': u'33868'}, 'yesterday': {'ctr': u'0.007', 'cpc': u'0.19', 'earnings': u'4.22', 'rpm': u'0.48', 'clicks': u'22', 'page_view': u'3157'}, 'today': {'ctr': u'0', 'cpc': None, 'earnings': u'0.02', 'rpm': u'0.04', 'clicks': u'0', 'page_view': u'203'}}

After the first time authorisation is done, the script creates a file called adsense.dat that is stored in the same directory. It contains the authorisation credentials that are used next time to show the results straightaway.

Last Updated On : 13th March 2013

Subscribe to get updates delivered to your inbox

Leave a comment