{"action":"created","comment":{"url":"https://api.github.com/repos/Calligre/lambda-functions/pulls/comments/85868287","pull_request_review_id":6557127,"id":85868287,"diff_hunk":"@@ -0,0 +1,155 @@\n+import json\n+import logging\n+import os\n+\n+import boto3\n+import requests\n+import tweepy\n+\n+AUTH0_BASE = \"https://calligre.auth0.com\"\n+FB_BASE = \"https://graph.facebook.com/v2.7\"\n+S3_BUCKET = \"calligre-media\"\n+\n+s3_client = boto3.client('s3')  # pylint: disable=C0103\n+\n+secrets = {}  # pylint: disable=C0103\n+\n+log = logging.getLogger()  # pylint: disable=C0103\n+log.setLevel(logging.DEBUG)\n+\n+\n+def set_secrets():\n+    log.info(\"Fetching secrets from S3\")\n+    try:\n+        response = s3_client.get_object(\n+            Bucket=\"calligre-config\",\n+            Key=\"secrets.json\",\n+        )\n+    except Exception as ex:\n+        log.error(ex)\n+        raise ex\n+\n+    content = response.get(\"Body\").read()\n+    retrieved_secrets = json.loads(content)\n+    if len(retrieved_secrets) == 0:\n+        log.error(\"Didn't fetch any secrets!\")\n+        raise Exception\n+    elif len(retrieved_secrets) < 4:\n+        log.warning(\"Number of secrets seems oddly small...\")\n+    secrets.update(retrieved_secrets)\n+\n+\n+def set_auth0_token():\n+    log.info(\"Fetching Auth0 token\")\n+    if not (secrets.get(\"AUTH0_CLIENT_ID\") and\n+            secrets.get(\"AUTH0_CLIENT_SECRET\")):\n+        log.error(\"Auth0 Client Tokens not set!\")\n+        raise Exception\n+    payload = {\n+        \"client_id\": secrets.get(\"AUTH0_CLIENT_ID\"),\n+        \"client_secret\": secrets.get(\"AUTH0_CLIENT_SECRET\"),\n+        \"audience\": \"%s/api/v2/\" % AUTH0_BASE,\n+        \"grant_type\": \"client_credentials\"\n+    }\n+    try:\n+        token = requests.post(\"%s/oauth/token\" % AUTH0_BASE, json=payload).\\\n+            json()\n+    except Exception as ex:\n+        log.error(ex)\n+        raise ex\n+    secrets[\"AUTH0_TOKEN\"] = token.get(\"access_token\")\n+    if not secrets[\"AUTH0_TOKEN\"]:\n+        log.error(\"Failed to fetch Auth0 Token!\")\n+        log.error(token)\n+        raise Exception\n+    log.info(\"Auth0 token set: %s\", secrets[\"AUTH0_TOKEN\"])\n+\n+\n+def get_auth0_user_tokens(user_id):\n+    if not secrets.get(\"AUTH0_TOKEN\"):\n+        set_auth0_token()\n+    headers = {\"Authorization\": \"Bearer %s\" % secrets.get(\"AUTH0_TOKEN\")}\n+    try:\n+        return requests.get(\"%s/api/v2/users/%s\" % (AUTH0_BASE, user_id),\n+                            headers=headers).\\\n+                            json().\\\n+                            get(\"identities\")\n+\n+    except Exception as ex:\n+        log.error(ex)\n+        raise ex\n+\n+\n+def post_fb_message(user_token, message):\n+    fb_data = {\n+        \"message\": message,\n+        \"access_token\": user_token\n+    }\n+    requests.post(\"%s/me/feed\" % FB_BASE, data=fb_data)\n+\n+\n+def post_fb_photo(user_token, message, file_path):\n+    fb_photo_data = {\n+        \"caption\": message,\n+        \"access_token\": user_token\n+    }\n+    files = {\"file\": open(file_path, \"rb\")}\n+    requests.post(\"%s/me/photos\" % FB_BASE, data=fb_photo_data, files=files)\n+\n+\n+def post_tw_message(access_token, access_secret, message, media_path=None):\n+    # dev ref: https://dev.twitter.com/rest/reference/post/statuses/update\n+    if not (secrets.get(\"TWITTER_CLIENT_ID\") and\n+            secrets.get(\"TWITTER_CLIENT_SECRET\")):\n+        log.error(\"Twitter Client Tokens not set, failing!\")\n+        raise Exception","path":"PostToExternalServices.py","position":105,"original_position":105,"commit_id":"aba60ed5cda80428eff627f3e543680844542585","original_commit_id":"aba60ed5cda80428eff627f3e543680844542585","user":{"login":"TheKevJames","id":3341822,"avatar_url":"https://avatars.githubusercontent.com/u/3341822?v=3","gravatar_id":"","url":"https://api.github.com/users/TheKevJames","html_url":"https://github.com/TheKevJames","followers_url":"https://api.github.com/users/TheKevJames/followers","following_url":"https://api.github.com/users/TheKevJames/following{/other_user}","gists_url":"https://api.github.com/users/TheKevJames/gists{/gist_id}","starred_url":"https://api.github.com/users/TheKevJames/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TheKevJames/subscriptions","organizations_url":"https://api.github.com/users/TheKevJames/orgs","repos_url":"https://api.github.com/users/TheKevJames/repos","events_url":"https://api.github.com/users/TheKevJames/events{/privacy}","received_events_url":"https://api.github.com/users/TheKevJames/received_events","type":"User","site_admin":false},"body":"More specific","created_at":"2016-11-01T02:31:19Z","updated_at":"2016-11-01T02:31:19Z","html_url":"https://github.com/Calligre/lambda-functions/pull/3#discussion_r85868287","pull_request_url":"https://api.github.com/repos/Calligre/lambda-functions/pulls/3","_links":{"self":{"href":"https://api.github.com/repos/Calligre/lambda-functions/pulls/comments/85868287"},"html":{"href":"https://github.com/Calligre/lambda-functions/pull/3#discussion_r85868287"},"pull_request":{"href":"https://api.github.com/repos/Calligre/lambda-functions/pulls/3"}}},"pull_request":{"url":"https://api.github.com/repos/Calligre/lambda-functions/pulls/3","id":91555687,"html_url":"https://github.com/Calligre/lambda-functions/pull/3","diff_url":"https://github.com/Calligre/lambda-functions/pull/3.diff","patch_url":"https://github.com/Calligre/lambda-functions/pull/3.patch","issue_url":"https://api.github.com/repos/Calligre/lambda-functions/issues/3","number":3,"state":"open","locked":false,"title":"Dump old code, add new","user":{"login":"kyl191","id":499035,"avatar_url":"https://avatars.githubusercontent.com/u/499035?v=3","gravatar_id":"","url":"https://api.github.com/users/kyl191","html_url":"https://github.com/kyl191","followers_url":"https://api.github.com/users/kyl191/followers","following_url":"https://api.github.com/users/kyl191/following{/other_user}","gists_url":"https://api.github.com/users/kyl191/gists{/gist_id}","starred_url":"https://api.github.com/users/kyl191/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kyl191/subscriptions","organizations_url":"https://api.github.com/users/kyl191/orgs","repos_url":"https://api.github.com/users/kyl191/repos","events_url":"https://api.github.com/users/kyl191/events{/privacy}","received_events_url":"https://api.github.com/users/kyl191/received_events","type":"User","site_admin":false},"body":"Threw out the lambda functions now that they're about to be merged into the main app.\r\n\r\nBut the post to FB/Twitter functionality is now a Lambda function. It takes ~3-4 seconds to execute, so yay  using Lambda as async worker.","created_at":"2016-10-31T04:33:00Z","updated_at":"2016-11-01T02:34:38Z","closed_at":null,"merged_at":null,"merge_commit_sha":"384573cb6a81416fef853c4ba7ca62d0ed027172","assignee":{"login":"TheKevJames","id":3341822,"avatar_url":"https://avatars.githubusercontent.com/u/3341822?v=3","gravatar_id":"","url":"https://api.github.com/users/TheKevJames","html_url":"https://github.com/TheKevJames","followers_url":"https://api.github.com/users/TheKevJames/followers","following_url":"https://api.github.com/users/TheKevJames/following{/other_user}","gists_url":"https://api.github.com/users/TheKevJames/gists{/gist_id}","starred_url":"https://api.github.com/users/TheKevJames/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TheKevJames/subscriptions","organizations_url":"https://api.github.com/users/TheKevJames/orgs","repos_url":"https://api.github.com/users/TheKevJames/repos","events_url":"https://api.github.com/users/TheKevJames/events{/privacy}","received_events_url":"https://api.github.com/users/TheKevJames/received_events","type":"User","site_admin":false},"assignees":[{"login":"TheKevJames","id":3341822,"avatar_url":"https://avatars.githubusercontent.com/u/3341822?v=3","gravatar_id":"","url":"https://api.github.com/users/TheKevJames","html_url":"https://github.com/TheKevJames","followers_url":"https://api.github.com/users/TheKevJames/followers","following_url":"https://api.github.com/users/TheKevJames/following{/other_user}","gists_url":"https://api.github.com/users/TheKevJames/gists{/gist_id}","starred_url":"https://api.github.com/users/TheKevJames/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TheKevJames/subscriptions","organizations_url":"https://api.github.com/users/TheKevJames/orgs","repos_url":"https://api.github.com/users/TheKevJames/repos","events_url":"https://api.github.com/users/TheKevJames/events{/privacy}","received_events_url":"https://api.github.com/users/TheKevJames/received_events","type":"User","site_admin":false}],"milestone":null,"commits_url":"https://api.github.com/repos/Calligre/lambda-functions/pulls/3/commits","review_comments_url":"https://api.github.com/repos/Calligre/lambda-functions/pulls/3/comments","review_comment_url":"https://api.github.com/repos/Calligre/lambda-functions/pulls/comments{/number}","comments_url":"https://api.github.com/repos/Calligre/lambda-functions/issues/3/comments","statuses_url":"https://api.github.com/repos/Calligre/lambda-functions/statuses/aba60ed5cda80428eff627f3e543680844542585","head":{"label":"Calligre:lambda-merge","ref":"lambda-merge","sha":"aba60ed5cda80428eff627f3e543680844542585","user":{"login":"Calligre","id":19350741,"avatar_url":"https://avatars.githubusercontent.com/u/19350741?v=3","gravatar_id":"","url":"https://api.github.com/users/Calligre","html_url":"https://github.com/Calligre","followers_url":"https://api.github.com/users/Calligre/followers","following_url":"https://api.github.com/users/Calligre/following{/other_user}","gists_url":"https://api.github.com/users/Calligre/gists{/gist_id}","starred_url":"https://api.github.com/users/Calligre/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Calligre/subscriptions","organizations_url":"https://api.github.com/users/Calligre/orgs","repos_url":"https://api.github.com/users/Calligre/repos","events_url":"https://api.github.com/users/Calligre/events{/privacy}","received_events_url":"https://api.github.com/users/Calligre/received_events","type":"Organization","site_admin":false},"repo":{"id":64955098,"name":"lambda-functions","full_name":"Calligre/lambda-functions","owner":{"login":"Calligre","id":19350741,"avatar_url":"https://avatars.githubusercontent.com/u/19350741?v=3","gravatar_id":"","url":"https://api.github.com/users/Calligre","html_url":"https://github.com/Calligre","followers_url":"https://api.github.com/users/Calligre/followers","following_url":"https://api.github.com/users/Calligre/following{/other_user}","gists_url":"https://api.github.com/users/Calligre/gists{/gist_id}","starred_url":"https://api.github.com/users/Calligre/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Calligre/subscriptions","organizations_url":"https://api.github.com/users/Calligre/orgs","repos_url":"https://api.github.com/users/Calligre/repos","events_url":"https://api.github.com/users/Calligre/events{/privacy}","received_events_url":"https://api.github.com/users/Calligre/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/Calligre/lambda-functions","description":null,"fork":false,"url":"https://api.github.com/repos/Calligre/lambda-functions","forks_url":"https://api.github.com/repos/Calligre/lambda-functions/forks","keys_url":"https://api.github.com/repos/Calligre/lambda-functions/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Calligre/lambda-functions/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Calligre/lambda-functions/teams","hooks_url":"https://api.github.com/repos/Calligre/lambda-functions/hooks","issue_events_url":"https://api.github.com/repos/Calligre/lambda-functions/issues/events{/number}","events_url":"https://api.github.com/repos/Calligre/lambda-functions/events","assignees_url":"https://api.github.com/repos/Calligre/lambda-functions/assignees{/user}","branches_url":"https://api.github.com/repos/Calligre/lambda-functions/branches{/branch}","tags_url":"https://api.github.com/repos/Calligre/lambda-functions/tags","blobs_url":"https://api.github.com/repos/Calligre/lambda-functions/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Calligre/lambda-functions/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Calligre/lambda-functions/git/refs{/sha}","trees_url":"https://api.github.com/repos/Calligre/lambda-functions/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Calligre/lambda-functions/statuses/{sha}","languages_url":"https://api.github.com/repos/Calligre/lambda-functions/languages","stargazers_url":"https://api.github.com/repos/Calligre/lambda-functions/stargazers","contributors_url":"https://api.github.com/repos/Calligre/lambda-functions/contributors","subscribers_url":"https://api.github.com/repos/Calligre/lambda-functions/subscribers","subscription_url":"https://api.github.com/repos/Calligre/lambda-functions/subscription","commits_url":"https://api.github.com/repos/Calligre/lambda-functions/commits{/sha}","git_commits_url":"https://api.github.com/repos/Calligre/lambda-functions/git/commits{/sha}","comments_url":"https://api.github.com/repos/Calligre/lambda-functions/comments{/number}","issue_comment_url":"https://api.github.com/repos/Calligre/lambda-functions/issues/comments{/number}","contents_url":"https://api.github.com/repos/Calligre/lambda-functions/contents/{+path}","compare_url":"https://api.github.com/repos/Calligre/lambda-functions/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Calligre/lambda-functions/merges","archive_url":"https://api.github.com/repos/Calligre/lambda-functions/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Calligre/lambda-functions/downloads","issues_url":"https://api.github.com/repos/Calligre/lambda-functions/issues{/number}","pulls_url":"https://api.github.com/repos/Calligre/lambda-functions/pulls{/number}","milestones_url":"https://api.github.com/repos/Calligre/lambda-functions/milestones{/number}","notifications_url":"https://api.github.com/repos/Calligre/lambda-functions/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Calligre/lambda-functions/labels{/name}","releases_url":"https://api.github.com/repos/Calligre/lambda-functions/releases{/id}","deployments_url":"https://api.github.com/repos/Calligre/lambda-functions/deployments","created_at":"2016-08-04T17:43:16Z","updated_at":"2016-08-04T17:49:12Z","pushed_at":"2016-10-31T04:33:01Z","git_url":"git://github.com/Calligre/lambda-functions.git","ssh_url":"git@github.com:Calligre/lambda-functions.git","clone_url":"https://github.com/Calligre/lambda-functions.git","svn_url":"https://github.com/Calligre/lambda-functions","homepage":null,"size":31,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":2,"forks":0,"open_issues":2,"watchers":0,"default_branch":"master"}},"base":{"label":"Calligre:master","ref":"master","sha":"ee5ad943ca163eb544cdff979c5588ab7ab7a4c8","user":{"login":"Calligre","id":19350741,"avatar_url":"https://avatars.githubusercontent.com/u/19350741?v=3","gravatar_id":"","url":"https://api.github.com/users/Calligre","html_url":"https://github.com/Calligre","followers_url":"https://api.github.com/users/Calligre/followers","following_url":"https://api.github.com/users/Calligre/following{/other_user}","gists_url":"https://api.github.com/users/Calligre/gists{/gist_id}","starred_url":"https://api.github.com/users/Calligre/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Calligre/subscriptions","organizations_url":"https://api.github.com/users/Calligre/orgs","repos_url":"https://api.github.com/users/Calligre/repos","events_url":"https://api.github.com/users/Calligre/events{/privacy}","received_events_url":"https://api.github.com/users/Calligre/received_events","type":"Organization","site_admin":false},"repo":{"id":64955098,"name":"lambda-functions","full_name":"Calligre/lambda-functions","owner":{"login":"Calligre","id":19350741,"avatar_url":"https://avatars.githubusercontent.com/u/19350741?v=3","gravatar_id":"","url":"https://api.github.com/users/Calligre","html_url":"https://github.com/Calligre","followers_url":"https://api.github.com/users/Calligre/followers","following_url":"https://api.github.com/users/Calligre/following{/other_user}","gists_url":"https://api.github.com/users/Calligre/gists{/gist_id}","starred_url":"https://api.github.com/users/Calligre/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Calligre/subscriptions","organizations_url":"https://api.github.com/users/Calligre/orgs","repos_url":"https://api.github.com/users/Calligre/repos","events_url":"https://api.github.com/users/Calligre/events{/privacy}","received_events_url":"https://api.github.com/users/Calligre/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/Calligre/lambda-functions","description":null,"fork":false,"url":"https://api.github.com/repos/Calligre/lambda-functions","forks_url":"https://api.github.com/repos/Calligre/lambda-functions/forks","keys_url":"https://api.github.com/repos/Calligre/lambda-functions/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Calligre/lambda-functions/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Calligre/lambda-functions/teams","hooks_url":"https://api.github.com/repos/Calligre/lambda-functions/hooks","issue_events_url":"https://api.github.com/repos/Calligre/lambda-functions/issues/events{/number}","events_url":"https://api.github.com/repos/Calligre/lambda-functions/events","assignees_url":"https://api.github.com/repos/Calligre/lambda-functions/assignees{/user}","branches_url":"https://api.github.com/repos/Calligre/lambda-functions/branches{/branch}","tags_url":"https://api.github.com/repos/Calligre/lambda-functions/tags","blobs_url":"https://api.github.com/repos/Calligre/lambda-functions/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Calligre/lambda-functions/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Calligre/lambda-functions/git/refs{/sha}","trees_url":"https://api.github.com/repos/Calligre/lambda-functions/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Calligre/lambda-functions/statuses/{sha}","languages_url":"https://api.github.com/repos/Calligre/lambda-functions/languages","stargazers_url":"https://api.github.com/repos/Calligre/lambda-functions/stargazers","contributors_url":"https://api.github.com/repos/Calligre/lambda-functions/contributors","subscribers_url":"https://api.github.com/repos/Calligre/lambda-functions/subscribers","subscription_url":"https://api.github.com/repos/Calligre/lambda-functions/subscription","commits_url":"https://api.github.com/repos/Calligre/lambda-functions/commits{/sha}","git_commits_url":"https://api.github.com/repos/Calligre/lambda-functions/git/commits{/sha}","comments_url":"https://api.github.com/repos/Calligre/lambda-functions/comments{/number}","issue_comment_url":"https://api.github.com/repos/Calligre/lambda-functions/issues/comments{/number}","contents_url":"https://api.github.com/repos/Calligre/lambda-functions/contents/{+path}","compare_url":"https://api.github.com/repos/Calligre/lambda-functions/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Calligre/lambda-functions/merges","archive_url":"https://api.github.com/repos/Calligre/lambda-functions/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Calligre/lambda-functions/downloads","issues_url":"https://api.github.com/repos/Calligre/lambda-functions/issues{/number}","pulls_url":"https://api.github.com/repos/Calligre/lambda-functions/pulls{/number}","milestones_url":"https://api.github.com/repos/Calligre/lambda-functions/milestones{/number}","notifications_url":"https://api.github.com/repos/Calligre/lambda-functions/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Calligre/lambda-functions/labels{/name}","releases_url":"https://api.github.com/repos/Calligre/lambda-functions/releases{/id}","deployments_url":"https://api.github.com/repos/Calligre/lambda-functions/deployments","created_at":"2016-08-04T17:43:16Z","updated_at":"2016-08-04T17:49:12Z","pushed_at":"2016-10-31T04:33:01Z","git_url":"git://github.com/Calligre/lambda-functions.git","ssh_url":"git@github.com:Calligre/lambda-functions.git","clone_url":"https://github.com/Calligre/lambda-functions.git","svn_url":"https://github.com/Calligre/lambda-functions","homepage":null,"size":31,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":2,"forks":0,"open_issues":2,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/Calligre/lambda-functions/pulls/3"},"html":{"href":"https://github.com/Calligre/lambda-functions/pull/3"},"issue":{"href":"https://api.github.com/repos/Calligre/lambda-functions/issues/3"},"comments":{"href":"https://api.github.com/repos/Calligre/lambda-functions/issues/3/comments"},"review_comments":{"href":"https://api.github.com/repos/Calligre/lambda-functions/pulls/3/comments"},"review_comment":{"href":"https://api.github.com/repos/Calligre/lambda-functions/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/Calligre/lambda-functions/pulls/3/commits"},"statuses":{"href":"https://api.github.com/repos/Calligre/lambda-functions/statuses/aba60ed5cda80428eff627f3e543680844542585"}}}}
{"actor":{"display_login":"TheKevJames"}}