動作確認まではしてないので、ご参考程度に。。
まず、example-simple-download.py
の DoDownload
の 呼び出しで start_date
, end_date
を追加します。(範囲指定)
diff -Nurb -i --strip-trailing-cr -Nur a/example-simple-download.py b/example-simple-download.py
--- a/example-simple-download.py 1979-12-31 00:00:00.000000000 +0900
+++ b/example-simple-download.py 2015-01-07 12:55:22.000000000 +0900
@@ -23,6 +23,7 @@
# Import the downloader
from downloader import Downloader
+from datetime import date
# Email address and password used to sign-in to Webmaster Tools
email = '[email protected]'
@@ -31,9 +32,12 @@
website = 'http://www.example.com/'
selected_downloads = ['TOP_QUERIES']
+start_date = date(2014,12,1)
+end_date = date(2015,1,1)
+
# Instantiate the downloader object
downloader = Downloader()
# Authenticate with your Webmaster Tools sign-in info
downloader.LogIn(email, password)
# Initiate the download
-downloader.DoDownload(website, selected_downloads)
+downloader.DoDownload(website, selected_downloads, start_date, end_date)
次に、downloader.py
にて
URL を生成するネタの LIST_PATH
に &db=%s&de=%s
を追加して、DoDownload
と、そこから呼ばれる _GetDownloadList
の引数に start_date
, end_date
を増やして、引き回して 期間指定のパラメータ付きの URL を生成、あとは既存の処理へ。。
diff -Nurb -i --strip-trailing-cr -Nur a/downloader.py b/downloader.py
--- a/downloader.py 1979-12-31 00:00:00.000000000 +0900
+++ b/downloader.py 2015-01-07 12:56:19.000000000 +0900
@@ -37,7 +37,7 @@
"""
HOST = 'www.google.com'
APP_NAME = 'Google-WMTdownloadscript-0.1'
- LIST_PATH = '/webmasters/tools/downloads-list?hl=%s&siteUrl=%s'
+ LIST_PATH = '/webmasters/tools/downloads-list?hl=%s&siteUrl=%s&db=%s&de=%s'
FILE_NAME_HEADER = 'content-disposition'
TEXT_BEFORE_NAME = 'attachment; filename='
@@ -72,7 +72,7 @@
self._logged_in = True
- def DoDownload(self, site, tables_to_download):
+ def DoDownload(self, site, tables_to_download, start_date, end_date):
"""Download CSV files and write them to disk.
Downloader must be logged in before this method can be called, otherwise
@@ -88,7 +88,7 @@
if not self.IsLoggedIn():
raise ValueError('Client not logged in.')
- available = self._GetDownloadList(site)
+ available = self._GetDownloadList(site, start_date, end_date)
sites_json = json.loads(available)
for key in tables_to_download:
@@ -111,7 +111,7 @@
def SetLanguage(self, language_code):
self._language = language_code
- def _GetDownloadList(self, site):
+ def _GetDownloadList(self, site, start_date, end_date):
"""Query the server for list of download URLs.
Args:
@@ -123,7 +123,7 @@
{'TOP_QUERIES':'webmaster/tools/top-search-queries-dl?...'}
"""
- url = self._GetFullUrl(self.LIST_PATH % (self._language, site))
+ url = self._GetFullUrl(self.LIST_PATH % (self._language, site, start_date, end_date))
res_stream = self._client.request('GET', url)
download_list = res_stream.read()
res_stream.close()