Skip to content

Instantly share code, notes, and snippets.

@Xyborg
Created October 17, 2019 15:59
Show Gist options
  • Save Xyborg/07a0b5bf9686eedcebe649c955006d95 to your computer and use it in GitHub Desktop.
Save Xyborg/07a0b5bf9686eedcebe649c955006d95 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
"""Uso de Google Search Analytics API (part of Search Console API).
Mas info en: https://developers.google.com/webmaster-tools/
To use:
1) Install the Google Python client library, as shown at https://developers.google.com/webmaster-tools/v3/libraries.
2) Sign up for a new project in the Google APIs console at https://code.google.com/apis/console.
3) Register the project to use OAuth2.0 for installed applications.
4) Copy your client ID, client secret, and redirect URL into the client_secrets.json file included in this package.
5) Run the app in the command-line as shown below.
Sample usage:
$ python search_analytics_api.py 'https://www.example.com/' '2015-05-01' '2015-05-30' --limit 50
"""
from __future__ import print_function
import argparse
import sys
import csv
import json
from datetime import datetime, timedelta
from googleapiclient import sample_tools
# Declare command-line flags.
argparser = argparse.ArgumentParser(add_help=False)
argparser.add_argument('property_uri', type=str, help=('Site or app URI to query data for (including ''trailing slash).'))
argparser.add_argument('start_date', type=str, help=('Start date of the requested date range in ''YYYY-MM-DD format.'))
argparser.add_argument('end_date', type=str, help=('End date of the requested date range in ''YYYY-MM-DD format.'))
argparser.add_argument('--limit', '-n', type=int, default=20, help=('Rows limit.'))
def main(argv):
service, flags = sample_tools.init(
argv, 'webmasters', 'v3', __doc__, __file__, parents=[argparser],
scope='https://www.googleapis.com/auth/webmasters.readonly')
start_date = datetime.strptime(flags.start_date, "%Y-%m-%d")
end_date = datetime.strptime(flags.end_date, "%Y-%m-%d")
for day in date_range(start_date, end_date):
day = day.strftime("%Y-%m-%d")
# Get top queries for the date range, sorted by click count, descending.
# List of countries https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3
request = {
'startDate': day,
'endDate': day,
'searchType': ['web'],
'dimensions': ["page", "query", "country", "device"],
'dimensionFilterGroups': [{
'filters': [{
'dimension': 'country',
'expression': 'arg'
}]
}],
'rowLimit': flags.limit
}
response = execute_request(service, flags.property_uri, request)
print_table(response, flags.property_uri, day)
def date_range(start_date, end_date, delta=timedelta(days=1)):
"""
Yields a stream of datetime objects, for all days within a range.
The range is inclusive, so both start_date and end_date will be returned,
as well as all dates in between.
Args:
start_date: The datetime object representing the first day in the range.
end_date: The datetime object representing the second day in the range.
delta: A datetime.timedelta instance, specifying the step interval. Defaults to one day.
Yields:
Each datetime object in the range.
"""
current_date = start_date
while current_date <= end_date:
yield current_date
current_date += delta
def execute_request(service, property_uri, request):
"""Executes a searchAnalytics.query request.
Args:
service: The webmasters service to use when executing the query.
property_uri: The site or app URI to request data for.
request: The request to be executed.
Returns:
An array of response rows.
"""
return service.searchanalytics().query(
siteUrl=property_uri, body=request).execute()
def print_table(response, gsc_property, day):
"""Prints out a response table.
Each row contains key(s), clicks, impressions, CTR, and average position.
Args:
response: The server response to be printed as a table.
gsc_property: The GSC property we are targeting.
"""
if 'rows' not in response:
print('Empty response')
return
rows = response['rows']
# print(json.dumps(rows, indent=4, separators=(". ", " = ")))
# print('Page', 'Keywords', 'Clicks', 'Impressions', 'CTR', 'Position', 'Device', 'Country', 'GSC Property',sep=',')
filas = 0
f = open("data_gsc_all_vc.csv", 'a')
for row in rows:
page = row['keys'][0].encode('utf-8').lower()
keywords = row['keys'][1].encode('utf-8').lower()
device = row['keys'][2].encode('utf-8').lower()
country = row['keys'][3].encode('utf-8').lower()
clicks = row['clicks']
impressions = row['impressions']
ctr = row['ctr']
position = row['position']
filas += 1
writer = csv.writer(f)
# with open("data_gsc.csv", "a") as myfile:
# print(day, keywords, page, clicks, impressions, ctr, position, gsc_property, country, device, negocio, sep=',', file=myfile)
gsc_property = gsc_property.replace("https://","")
gsc_property = gsc_property.replace("http://","")
gsc_property = gsc_property.replace("/","")
writer.writerow( (day, keywords, page, clicks, impressions, ctr, position, gsc_property, country, device) )
print(day, ": ", filas, " resultados importados")
f.close()
# ['Date','Query','Page','Clicks','Impressions','CTR','AVG_position','Property','Country','Device']
if __name__ == '__main__':
main(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment