{"action":"created","comment":{"url":"https://api.github.com/repos/OpenCanada/website/pulls/comments/38573738","id":38573738,"diff_hunk":"@@ -0,0 +1,110 @@\n+from __future__ import absolute_import, division, unicode_literals\n+\n+import os\n+import sys\n+\n+from django.conf import settings\n+from django.core.exceptions import ImproperlyConfigured\n+from django.core.management.base import BaseCommand\n+from django.db import transaction\n+from wagtail.wagtailcore.models import Page\n+\n+from analytics import utils\n+\n+\n+def get_creds_path():\n+    message = 'Setting ANALYTICS_CREDS_PATH must be defined, exist and be a directory.'\n+    try:\n+        path = getattr(settings, 'ANALYTICS_CREDS_PATH')\n+    except AttributeError:\n+        raise ImproperlyConfigured(message)\n+\n+    if not os.path.isdir(path):\n+        raise ImproperlyConfigured(message)\n+\n+    return path\n+\n+\n+def get_service_account_email():\n+    message = 'Setting ANALYTICS_SERVICE_ACCOUNT_EMAIL must be defined and non empty'\n+    try:\n+        email = getattr(settings, 'ANALYTICS_SERVICE_ACCOUNT_EMAIL')\n+    except AttributeError:\n+        raise ImproperlyConfigured(message)\n+\n+    if email is None or email == '':\n+        raise ImproperlyConfigured(message)\n+\n+    return email\n+\n+\n+class Command(BaseCommand):\n+    help = 'Get the analytics data over the last week and update the articles in te system'\n+\n+    def add_arguments(self, parser):\n+        parser.add_argument(\n+            '--dry-run',\n+            action='store_true',\n+            dest='dry_run',\n+            default=False,\n+            help='Output the analytics but do not update the database'\n+        )\n+\n+    def handle(self, *args, **options):\n+        dry_run = options['dry_run']\n+        creds_path = get_creds_path()\n+\n+        key_file_location = os.path.join(creds_path, 'open-canada-analytics.p12')\n+\n+        if not os.path.isfile(key_file_location):\n+            raise ImproperlyConfigured(\n+                '{} must be a file which contains your api key.'.format(key_file_location)\n+            )\n+\n+        scope = ['https://www.googleapis.com/auth/analytics.readonly']\n+        service_account_email = get_service_account_email()\n+\n+        service = utils.get_service(\n+            'analytics',\n+            'v3',\n+            scope,\n+            key_file_location,\n+            service_account_email\n+        )\n+\n+        profile = utils.get_first_profile_id(service)\n+\n+        data = service.data().ga().get(\n+            ids='ga:' + profile,\n+            start_date='7daysAgo',\n+            end_date='today',\n+            dimensions='ga:pagePath',\n+            metrics='ga:sessions',\n+            sort='-ga:sessions',\n+            filters='ga:sessions>=1',\n+        ).execute()\n+\n+        if dry_run:\n+            for row in data['rows']:\n+                url, session = row\n+                print('{}: {}'.format(url, session))\n+            sys.exit(0)\n+\n+        with transaction.atomic():\n+            # TODO should I filter on liveness?\n+            pages = dict([(page.url, page) for page in Page.objects.live()])\n+            utils.reset_analytics(pages)\n+            for row in data['rows']:\n+                url, sessions = row\n+                if url not in pages:\n+                    continue\n+\n+                page = pages[url]\n+\n+                analytics = utils.get_analytics(page)\n+                analytics.last_week_views = sessions\n+                analytics.save()\n+\n+        # TODO Cache: we need to invalid the cache for any page which is effected","path":"analytics/management/commands/update_page_analytics.py","position":108,"original_position":108,"commit_id":"c35d459f2fbdbe101bffc256dc59577708c3f733","original_commit_id":"c35d459f2fbdbe101bffc256dc59577708c3f733","user":{"login":"albertoconnor","id":1960401,"avatar_url":"https://avatars.githubusercontent.com/u/1960401?v=3","gravatar_id":"","url":"https://api.github.com/users/albertoconnor","html_url":"https://github.com/albertoconnor","followers_url":"https://api.github.com/users/albertoconnor/followers","following_url":"https://api.github.com/users/albertoconnor/following{/other_user}","gists_url":"https://api.github.com/users/albertoconnor/gists{/gist_id}","starred_url":"https://api.github.com/users/albertoconnor/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/albertoconnor/subscriptions","organizations_url":"https://api.github.com/users/albertoconnor/orgs","repos_url":"https://api.github.com/users/albertoconnor/repos","events_url":"https://api.github.com/users/albertoconnor/events{/privacy}","received_events_url":"https://api.github.com/users/albertoconnor/received_events","type":"User","site_admin":false},"body":"created OCW-377","created_at":"2015-09-02T19:29:54Z","updated_at":"2015-09-02T19:29:54Z","html_url":"https://github.com/OpenCanada/website/pull/170#discussion_r38573738","pull_request_url":"https://api.github.com/repos/OpenCanada/website/pulls/170","_links":{"self":{"href":"https://api.github.com/repos/OpenCanada/website/pulls/comments/38573738"},"html":{"href":"https://github.com/OpenCanada/website/pull/170#discussion_r38573738"},"pull_request":{"href":"https://api.github.com/repos/OpenCanada/website/pulls/170"}}},"pull_request":{"url":"https://api.github.com/repos/OpenCanada/website/pulls/170","id":44006205,"html_url":"https://github.com/OpenCanada/website/pull/170","diff_url":"https://github.com/OpenCanada/website/pull/170.diff","patch_url":"https://github.com/OpenCanada/website/pull/170.patch","issue_url":"https://api.github.com/repos/OpenCanada/website/issues/170","number":170,"state":"open","locked":false,"title":"OCW 288","user":{"login":"albertoconnor","id":1960401,"avatar_url":"https://avatars.githubusercontent.com/u/1960401?v=3","gravatar_id":"","url":"https://api.github.com/users/albertoconnor","html_url":"https://github.com/albertoconnor","followers_url":"https://api.github.com/users/albertoconnor/followers","following_url":"https://api.github.com/users/albertoconnor/following{/other_user}","gists_url":"https://api.github.com/users/albertoconnor/gists{/gist_id}","starred_url":"https://api.github.com/users/albertoconnor/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/albertoconnor/subscriptions","organizations_url":"https://api.github.com/users/albertoconnor/orgs","repos_url":"https://api.github.com/users/albertoconnor/repos","events_url":"https://api.github.com/users/albertoconnor/events{/privacy}","received_events_url":"https://api.github.com/users/albertoconnor/received_events","type":"User","site_admin":false},"body":"Not ready for merge yet but I want to test it.","created_at":"2015-09-02T15:31:00Z","updated_at":"2015-09-02T19:29:54Z","closed_at":null,"merged_at":null,"merge_commit_sha":"cf74792053d53e589155113966804f3cec1363fb","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/OpenCanada/website/pulls/170/commits","review_comments_url":"https://api.github.com/repos/OpenCanada/website/pulls/170/comments","review_comment_url":"https://api.github.com/repos/OpenCanada/website/pulls/comments{/number}","comments_url":"https://api.github.com/repos/OpenCanada/website/issues/170/comments","statuses_url":"https://api.github.com/repos/OpenCanada/website/statuses/c35d459f2fbdbe101bffc256dc59577708c3f733","head":{"label":"OpenCanada:OCW-288","ref":"OCW-288","sha":"c35d459f2fbdbe101bffc256dc59577708c3f733","user":{"login":"OpenCanada","id":12661031,"avatar_url":"https://avatars.githubusercontent.com/u/12661031?v=3","gravatar_id":"","url":"https://api.github.com/users/OpenCanada","html_url":"https://github.com/OpenCanada","followers_url":"https://api.github.com/users/OpenCanada/followers","following_url":"https://api.github.com/users/OpenCanada/following{/other_user}","gists_url":"https://api.github.com/users/OpenCanada/gists{/gist_id}","starred_url":"https://api.github.com/users/OpenCanada/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/OpenCanada/subscriptions","organizations_url":"https://api.github.com/users/OpenCanada/orgs","repos_url":"https://api.github.com/users/OpenCanada/repos","events_url":"https://api.github.com/users/OpenCanada/events{/privacy}","received_events_url":"https://api.github.com/users/OpenCanada/received_events","type":"Organization","site_admin":false},"repo":{"id":36510047,"name":"website","full_name":"OpenCanada/website","owner":{"login":"OpenCanada","id":12661031,"avatar_url":"https://avatars.githubusercontent.com/u/12661031?v=3","gravatar_id":"","url":"https://api.github.com/users/OpenCanada","html_url":"https://github.com/OpenCanada","followers_url":"https://api.github.com/users/OpenCanada/followers","following_url":"https://api.github.com/users/OpenCanada/following{/other_user}","gists_url":"https://api.github.com/users/OpenCanada/gists{/gist_id}","starred_url":"https://api.github.com/users/OpenCanada/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/OpenCanada/subscriptions","organizations_url":"https://api.github.com/users/OpenCanada/orgs","repos_url":"https://api.github.com/users/OpenCanada/repos","events_url":"https://api.github.com/users/OpenCanada/events{/privacy}","received_events_url":"https://api.github.com/users/OpenCanada/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/OpenCanada/website","description":"The opencanada.org website source","fork":false,"url":"https://api.github.com/repos/OpenCanada/website","forks_url":"https://api.github.com/repos/OpenCanada/website/forks","keys_url":"https://api.github.com/repos/OpenCanada/website/keys{/key_id}","collaborators_url":"https://api.github.com/repos/OpenCanada/website/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/OpenCanada/website/teams","hooks_url":"https://api.github.com/repos/OpenCanada/website/hooks","issue_events_url":"https://api.github.com/repos/OpenCanada/website/issues/events{/number}","events_url":"https://api.github.com/repos/OpenCanada/website/events","assignees_url":"https://api.github.com/repos/OpenCanada/website/assignees{/user}","branches_url":"https://api.github.com/repos/OpenCanada/website/branches{/branch}","tags_url":"https://api.github.com/repos/OpenCanada/website/tags","blobs_url":"https://api.github.com/repos/OpenCanada/website/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/OpenCanada/website/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/OpenCanada/website/git/refs{/sha}","trees_url":"https://api.github.com/repos/OpenCanada/website/git/trees{/sha}","statuses_url":"https://api.github.com/repos/OpenCanada/website/statuses/{sha}","languages_url":"https://api.github.com/repos/OpenCanada/website/languages","stargazers_url":"https://api.github.com/repos/OpenCanada/website/stargazers","contributors_url":"https://api.github.com/repos/OpenCanada/website/contributors","subscribers_url":"https://api.github.com/repos/OpenCanada/website/subscribers","subscription_url":"https://api.github.com/repos/OpenCanada/website/subscription","commits_url":"https://api.github.com/repos/OpenCanada/website/commits{/sha}","git_commits_url":"https://api.github.com/repos/OpenCanada/website/git/commits{/sha}","comments_url":"https://api.github.com/repos/OpenCanada/website/comments{/number}","issue_comment_url":"https://api.github.com/repos/OpenCanada/website/issues/comments{/number}","contents_url":"https://api.github.com/repos/OpenCanada/website/contents/{+path}","compare_url":"https://api.github.com/repos/OpenCanada/website/compare/{base}...{head}","merges_url":"https://api.github.com/repos/OpenCanada/website/merges","archive_url":"https://api.github.com/repos/OpenCanada/website/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/OpenCanada/website/downloads","issues_url":"https://api.github.com/repos/OpenCanada/website/issues{/number}","pulls_url":"https://api.github.com/repos/OpenCanada/website/pulls{/number}","milestones_url":"https://api.github.com/repos/OpenCanada/website/milestones{/number}","notifications_url":"https://api.github.com/repos/OpenCanada/website/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/OpenCanada/website/labels{/name}","releases_url":"https://api.github.com/repos/OpenCanada/website/releases{/id}","created_at":"2015-05-29T14:43:28Z","updated_at":"2015-05-29T18:47:15Z","pushed_at":"2015-09-02T19:28:04Z","git_url":"git://github.com/OpenCanada/website.git","ssh_url":"git@github.com:OpenCanada/website.git","clone_url":"https://github.com/OpenCanada/website.git","svn_url":"https://github.com/OpenCanada/website","homepage":null,"size":9364,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":3,"forks":1,"open_issues":3,"watchers":0,"default_branch":"master"}},"base":{"label":"OpenCanada:master","ref":"master","sha":"1727acb31cb7adcf054daa845841f4a7512536cf","user":{"login":"OpenCanada","id":12661031,"avatar_url":"https://avatars.githubusercontent.com/u/12661031?v=3","gravatar_id":"","url":"https://api.github.com/users/OpenCanada","html_url":"https://github.com/OpenCanada","followers_url":"https://api.github.com/users/OpenCanada/followers","following_url":"https://api.github.com/users/OpenCanada/following{/other_user}","gists_url":"https://api.github.com/users/OpenCanada/gists{/gist_id}","starred_url":"https://api.github.com/users/OpenCanada/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/OpenCanada/subscriptions","organizations_url":"https://api.github.com/users/OpenCanada/orgs","repos_url":"https://api.github.com/users/OpenCanada/repos","events_url":"https://api.github.com/users/OpenCanada/events{/privacy}","received_events_url":"https://api.github.com/users/OpenCanada/received_events","type":"Organization","site_admin":false},"repo":{"id":36510047,"name":"website","full_name":"OpenCanada/website","owner":{"login":"OpenCanada","id":12661031,"avatar_url":"https://avatars.githubusercontent.com/u/12661031?v=3","gravatar_id":"","url":"https://api.github.com/users/OpenCanada","html_url":"https://github.com/OpenCanada","followers_url":"https://api.github.com/users/OpenCanada/followers","following_url":"https://api.github.com/users/OpenCanada/following{/other_user}","gists_url":"https://api.github.com/users/OpenCanada/gists{/gist_id}","starred_url":"https://api.github.com/users/OpenCanada/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/OpenCanada/subscriptions","organizations_url":"https://api.github.com/users/OpenCanada/orgs","repos_url":"https://api.github.com/users/OpenCanada/repos","events_url":"https://api.github.com/users/OpenCanada/events{/privacy}","received_events_url":"https://api.github.com/users/OpenCanada/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/OpenCanada/website","description":"The opencanada.org website source","fork":false,"url":"https://api.github.com/repos/OpenCanada/website","forks_url":"https://api.github.com/repos/OpenCanada/website/forks","keys_url":"https://api.github.com/repos/OpenCanada/website/keys{/key_id}","collaborators_url":"https://api.github.com/repos/OpenCanada/website/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/OpenCanada/website/teams","hooks_url":"https://api.github.com/repos/OpenCanada/website/hooks","issue_events_url":"https://api.github.com/repos/OpenCanada/website/issues/events{/number}","events_url":"https://api.github.com/repos/OpenCanada/website/events","assignees_url":"https://api.github.com/repos/OpenCanada/website/assignees{/user}","branches_url":"https://api.github.com/repos/OpenCanada/website/branches{/branch}","tags_url":"https://api.github.com/repos/OpenCanada/website/tags","blobs_url":"https://api.github.com/repos/OpenCanada/website/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/OpenCanada/website/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/OpenCanada/website/git/refs{/sha}","trees_url":"https://api.github.com/repos/OpenCanada/website/git/trees{/sha}","statuses_url":"https://api.github.com/repos/OpenCanada/website/statuses/{sha}","languages_url":"https://api.github.com/repos/OpenCanada/website/languages","stargazers_url":"https://api.github.com/repos/OpenCanada/website/stargazers","contributors_url":"https://api.github.com/repos/OpenCanada/website/contributors","subscribers_url":"https://api.github.com/repos/OpenCanada/website/subscribers","subscription_url":"https://api.github.com/repos/OpenCanada/website/subscription","commits_url":"https://api.github.com/repos/OpenCanada/website/commits{/sha}","git_commits_url":"https://api.github.com/repos/OpenCanada/website/git/commits{/sha}","comments_url":"https://api.github.com/repos/OpenCanada/website/comments{/number}","issue_comment_url":"https://api.github.com/repos/OpenCanada/website/issues/comments{/number}","contents_url":"https://api.github.com/repos/OpenCanada/website/contents/{+path}","compare_url":"https://api.github.com/repos/OpenCanada/website/compare/{base}...{head}","merges_url":"https://api.github.com/repos/OpenCanada/website/merges","archive_url":"https://api.github.com/repos/OpenCanada/website/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/OpenCanada/website/downloads","issues_url":"https://api.github.com/repos/OpenCanada/website/issues{/number}","pulls_url":"https://api.github.com/repos/OpenCanada/website/pulls{/number}","milestones_url":"https://api.github.com/repos/OpenCanada/website/milestones{/number}","notifications_url":"https://api.github.com/repos/OpenCanada/website/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/OpenCanada/website/labels{/name}","releases_url":"https://api.github.com/repos/OpenCanada/website/releases{/id}","created_at":"2015-05-29T14:43:28Z","updated_at":"2015-05-29T18:47:15Z","pushed_at":"2015-09-02T19:28:04Z","git_url":"git://github.com/OpenCanada/website.git","ssh_url":"git@github.com:OpenCanada/website.git","clone_url":"https://github.com/OpenCanada/website.git","svn_url":"https://github.com/OpenCanada/website","homepage":null,"size":9364,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":3,"forks":1,"open_issues":3,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/OpenCanada/website/pulls/170"},"html":{"href":"https://github.com/OpenCanada/website/pull/170"},"issue":{"href":"https://api.github.com/repos/OpenCanada/website/issues/170"},"comments":{"href":"https://api.github.com/repos/OpenCanada/website/issues/170/comments"},"review_comments":{"href":"https://api.github.com/repos/OpenCanada/website/pulls/170/comments"},"review_comment":{"href":"https://api.github.com/repos/OpenCanada/website/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/OpenCanada/website/pulls/170/commits"},"statuses":{"href":"https://api.github.com/repos/OpenCanada/website/statuses/c35d459f2fbdbe101bffc256dc59577708c3f733"}}}}