Skip to content

Commit

Permalink
fill in attribute even on failure
Browse files Browse the repository at this point in the history
  • Loading branch information
jhshi committed Sep 27, 2016
1 parent 917460a commit 42aeb47
Showing 1 changed file with 30 additions and 33 deletions.
63 changes: 30 additions & 33 deletions ga_page_view.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@

from pelican import signals
from pelican.generators import ArticlesGenerator, PagesGenerator

from apiclient.discovery import build
from oauth2client.client import SignedJwtAssertionCredentials

import httplib2
from oauth2client import client
from oauth2client import file
from oauth2client import tools

import parsedatetime as pdt
import datetime
import sys


def get_service(api_name, api_version, scope, key_file_location,
Expand Down Expand Up @@ -79,36 +76,38 @@ def get_page_view(generators):
service_account_email = generator.settings.get(
'GOOGLE_SERVICE_ACCOUNT', None)
key_file_path = generator.settings.get('GOOGLE_KEY_FILE', None)
if service_account_email is None or key_file_path is None:
return

scope = ['https://www.googleapis.com/auth/analytics.readonly']
service = get_service('analytics', 'v3', scope, key_file_path,
service_account_email)
profile_id = get_first_profile_id(service)

start_date = generator.settings.get('GA_START_DATE', '2005-01-01')
end_date = generator.settings.get('GA_END_DATE', 'today')
metric = generator.settings.get('GA_METRIC', 'ga:pageviews')

result = service.data().ga().get(ids='ga:' + profile_id, start_date=start_date,
end_date=end_date, max_results=999999, metrics=metric,
dimensions='ga:pagePath').execute()
page_view = dict()
for slug, pv in result['rows']:
page_view[slug] = int(pv)

popular_start_str = generator.settings.get('POPULAR_POST_START', 'a month ago')
popular_start_date = str(pdt.Calendar().parseDT(
popular_start_str, datetime.datetime.now())[0].date())
popular_result = service.data().ga().get(
ids='ga:' + profile_id, start_date=popular_start_date,
end_date=end_date, max_results=999999, metrics=metric,
dimensions='ga:pagePath').execute()

popular_page_view = dict()
for slug, pv in popular_result['rows']:
popular_page_view[slug] = int(pv)

try:
scope = ['https://www.googleapis.com/auth/analytics.readonly']
service = get_service('analytics', 'v3', scope, key_file_path,
service_account_email)
profile_id = get_first_profile_id(service)

start_date = generator.settings.get('GA_START_DATE', '2005-01-01')
end_date = generator.settings.get('GA_END_DATE', 'today')
metric = generator.settings.get('GA_METRIC', 'ga:pageviews')

result = service.data().ga().get(ids='ga:' + profile_id, start_date=start_date,
end_date=end_date, max_results=999999, metrics=metric,
dimensions='ga:pagePath').execute()
for slug, pv in result['rows']:
page_view[slug] = int(pv)

popular_start_str = generator.settings.get('POPULAR_POST_START', 'a month ago')
popular_start_date = str(pdt.Calendar().parseDT(
popular_start_str, datetime.datetime.now())[0].date())
popular_result = service.data().ga().get(
ids='ga:' + profile_id, start_date=popular_start_date,
end_date=end_date, max_results=999999, metrics=metric,
dimensions='ga:pagePath').execute()

for slug, pv in popular_result['rows']:
popular_page_view[slug] = int(pv)
except:
sys.stderr.write("[ga_page_view] Failed to fetch page view information.\n")

article_generator = [g for g in generators if type(g) is ArticlesGenerator][0]
page_generator = [g for g in generators if type(g) is PagesGenerator][0]
Expand All @@ -122,8 +121,6 @@ def get_page_view(generators):
for page in pages:
url = '/%s' % (url_pattern.format(**page.__dict__))
pv = page_view.get(url, 0)
if url not in page_view:
print "Page not found: %s" % (url)
setattr(page, 'pageview', pv)
setattr(page, 'popular_pageview', popular_page_view.get(url, 0))
total_page_view += pv
Expand Down

0 comments on commit 42aeb47

Please sign in to comment.