Merge pull request #121 from ZIXANLABS/master
Upgrade django sample project to use render function.
diff --git a/samples/README.md b/samples/README.md
index b2fbcbc..2bf8a39 100644
--- a/samples/README.md
+++ b/samples/README.md
@@ -170,6 +170,21 @@
</tr>
</table>
+##  Search Console API
+
+Add sites to a Search Console account, retrieve site data and diagnostics, and submit sitemaps.
+
+Documentation for the Search Console API in
+[Google Developers](https://developers.google.com/webmaster-tools/v3/).
+
+<table>
+ <tr>
+ <td><a href="searchconsole">samples/searchconsole</a></td>
+ <td>Command-line samples for Search Console API (formerly known as Webmaster Tools API)</td>
+ </tr>
+</table>
+
+
##  Search API For Shopping
Lets you search over product data.
diff --git a/samples/searchconsole/README b/samples/searchconsole/README
new file mode 100644
index 0000000..4e10c74
--- /dev/null
+++ b/samples/searchconsole/README
@@ -0,0 +1,4 @@
+Command-line samples for Search Console API (formerly known as Webmaster Tools API).
+
+api: webmasters
+keywords: cmdline
diff --git a/samples/searchconsole/client_secrets.json b/samples/searchconsole/client_secrets.json
new file mode 100644
index 0000000..323ffd0
--- /dev/null
+++ b/samples/searchconsole/client_secrets.json
@@ -0,0 +1,9 @@
+{
+ "installed": {
+ "client_id": "[[INSERT CLIENT ID HERE]]",
+ "client_secret": "[[INSERT CLIENT SECRET HERE]]",
+ "redirect_uris": [],
+ "auth_uri": "https://accounts.google.com/o/oauth2/auth",
+ "token_uri": "https://accounts.google.com/o/oauth2/token"
+ }
+}
diff --git a/samples/searchconsole/search_analytics_api_sample.py b/samples/searchconsole/search_analytics_api_sample.py
new file mode 100644
index 0000000..7fd4b7a
--- /dev/null
+++ b/samples/searchconsole/search_analytics_api_sample.py
@@ -0,0 +1,169 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+#
+# Copyright 2015 Google Inc. All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""Example for using the Google Search Analytics API (part of Search Console API).
+
+A basic python command-line example that uses the searchAnalytics.query method
+of the Google Search Console API. This example demonstrates how to query Google
+search results data for your property. Learn more at
+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_sample.py 'https://www.example.com/' '2015-05-01' '2015-05-30'
+
+"""
+
+import argparse
+import sys
+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.'))
+
+
+def main(argv):
+ service, flags = sample_tools.init(
+ argv, 'webmasters', 'v3', __doc__, __file__, parents=[argparser],
+ scope='https://www.googleapis.com/auth/webmasters.readonly')
+
+ # First run a query to learn which dates we have data for. You should always
+ # check which days in a date range have data before running your main query.
+ # This query shows data for the entire range, grouped and sorted by day,
+ # descending; any days without data will be missing from the results.
+ request = {
+ 'startDate': flags.start_date,
+ 'endDate': flags.end_date,
+ 'dimensions': ['date']
+ }
+ response = execute_request(service, flags.property_uri, request)
+ print_table(response, 'Available dates')
+
+ # Get totals for the date range.
+ request = {
+ 'startDate': flags.start_date,
+ 'endDate': flags.end_date
+ }
+ response = execute_request(service, flags.property_uri, request)
+ print_table(response, 'Totals')
+
+ # Get top 10 queries for the date range, sorted by click count, descending.
+ request = {
+ 'startDate': flags.start_date,
+ 'endDate': flags.end_date,
+ 'dimensions': ['query'],
+ 'rowLimit': 10
+ }
+ response = execute_request(service, flags.property_uri, request)
+ print_table(response, 'Top Queries')
+
+ # Get top 10 pages for the date range, sorted by click count, descending.
+ request = {
+ 'startDate': flags.start_date,
+ 'endDate': flags.end_date,
+ 'dimensions': ['page'],
+ 'rowLimit': 10
+ }
+ response = execute_request(service, flags.property_uri, request)
+ print_table(response, 'Top Pages')
+
+ # Get the top 10 queries in India, sorted by click count, descending.
+ request = {
+ 'startDate': flags.start_date,
+ 'endDate': flags.end_date,
+ 'dimensions': ['query'],
+ 'dimensionFilterGroups': [{
+ 'filters': [{
+ 'dimension': 'country',
+ 'expression': 'ind'
+ }]
+ }],
+ 'rowLimit': 10
+ }
+ response = execute_request(service, flags.property_uri, request)
+ print_table(response, 'Top queries in India')
+
+ # Group by both country and device.
+ request = {
+ 'startDate': flags.start_date,
+ 'endDate': flags.end_date,
+ 'dimensions': ['country', 'device'],
+ 'rowLimit': 10
+ }
+ response = execute_request(service, flags.property_uri, request)
+ print_table(response, 'Group by country and device')
+
+
+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, title):
+ """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.
+ title: The title of the table.
+ """
+ print title + ':'
+
+ if 'rows' not in response:
+ print 'Empty response'
+ return
+
+ rows = response['rows']
+ row_format = '{:<20}' + '{:>20}' * 4
+ print row_format.format('Keys', 'Clicks', 'Impressions', 'CTR', 'Position')
+ for row in rows:
+ keys = ''
+ # Keys are returned only if one or more dimensions are requested.
+ if 'keys' in row:
+ keys = u','.join(row['keys']).encode('utf-8')
+ print row_format.format(
+ keys, row['clicks'], row['impressions'], row['ctr'], row['position'])
+
+if __name__ == '__main__':
+ main(sys.argv)