Skip to content

Commit

Permalink
JD: Adjusts procfile and scorer_app.py (underscores in fn names), add…
Browse files Browse the repository at this point in the history
…s scripts dir.
  • Loading branch information
John Demos committed Sep 13, 2019
1 parent 8fb3f22 commit 6523671
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.env
.DS_Store
.DS_Store?
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
web: gunicorn Main:app --log-file=-
web: gunicorn scorer_app:app
19 changes: 13 additions & 6 deletions scorer_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
from flask import Flask, request, send_from_directory, make_response
from http import HTTPStatus

import Twitter, hashlib, hmac, base64, os, logging, json
import base64
import hashlib
import hmac
import logging
import json
import os

import twitter

CONSUMER_SECRET = os.environ.get('CONSUMER_SECRET', None)
CURRENT_USER_ID = os.environ.get('CURRENT_USER_ID', None)
Expand All @@ -17,7 +24,7 @@ def default_route():
#The GET method for webhook should be used for the CRC check
#TODO: add header validation (compare_digest https://docs.python.org/3.6/library/hmac.html)
@app.route("/webhook", methods=["GET"])
def twitterCrcValidation():
def twitter_crc_validation():

crc = request.args['crc_token']

Expand All @@ -37,7 +44,7 @@ def twitterCrcValidation():
#The POST method for webhook should be used for all other API events
#TODO: add event-specific behaviours beyond Direct Message and Like
@app.route("/webhook", methods=["POST"])
def twitterEventReceived():
def twitter_event_received():

requestJson = request.get_json()

Expand Down Expand Up @@ -81,7 +88,7 @@ def twitterEventReceived():
if __name__ == '__main__':
# Bind to PORT if defined, otherwise default to 65010.
port = int(os.environ.get('PORT', 65010))
gunicorn_logger = logging.getLogger('gunicorn.error')
app.logger.handlers = gunicorn_logger.handlers
app.logger.setLevel(gunicorn_logger.level)
# gunicorn_logger = logging.getLogger('gunicorn.error')
# app.logger.handlers = gunicorn_logger.handlers
# app.logger.setLevel(gunicorn_logger.level)
app.run(host='0.0.0.0', port=port, debug=True)
26 changes: 26 additions & 0 deletions scripts/post_tweet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import json
import os
import sys

import requests
from requests_oauthlib import OAuth1
from dotenv import load_dotenv
load_dotenv(verbose=True) # Throws error if it can't find .env file

# Retrieves and stores credential information from the '.env' file
CONSUMER_KEY = os.getenv("TWITTER_CONSUMER_KEY")
CONSUMER_SECRET = os.getenv("TWITTER_CONSUMER_SECRET")
ACCESS_TOKEN = os.getenv("TWITTER_ACCESS_TOKEN")
TOKEN_SECRET = os.getenv("TWITTER_ACCESS_TOKEN_SECRET")

# Generate user context auth (OAuth1)
user_context_auth = OAuth1(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, TOKEN_SECRET)

payload = {'status': 'Fore!'}

def post_tweet(status):
endpoint = "https://api.twitter.com/1.1/statuses/update.json"
response = requests.post(endpoint, auth=user_context_auth, params=payload)
print(response.status_code, response.text)

post_tweet(payload)
42 changes: 42 additions & 0 deletions scripts/register_webhook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import json
import os
import sys

import requests
from requests_oauthlib import OAuth1
from dotenv import load_dotenv
load_dotenv(verbose=True) # Throws error if it can't find .env file

# Retrieves and stores credential information from the '.env' file
CONSUMER_KEY = os.getenv("TWITTER_CONSUMER_KEY")
CONSUMER_SECRET = os.getenv("TWITTER_CONSUMER_SECRET")
ACCESS_TOKEN = os.getenv("TWITTER_ACCESS_TOKEN")
TOKEN_SECRET = os.getenv("TWITTER_ACCESS_TOKEN_SECRET")

# Generate user context auth (OAuth1)
user_context_auth = OAuth1(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, TOKEN_SECRET)

# Assign the resource and webhook urls
resource_url = "https://api.twitter.com/1.1/account_activity/all/dev/webhooks.json"

url_dict = {'url': 'https://hacker-scorer.herokuapp.com/webhook'}

headers = {"Content-Type": "application/x-www-form-urlencoded"}


response = requests.post(resource_url, auth=user_context_auth, headers=headers, params=url_dict)
print(response.status_code, response.text)

# def register_webhook(webhook_url):
# # Generate user context auth (OAuth1)
# user_context_auth = OAuth1(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, TOKEN_SECRET)
# try:
# response = requests.post(resource_url, auth=user_context_auth, params=webhook_url)
# except requests.exceptions.RequestException as e:
# print(e)
# sys.exit(120)

# print(response.status_code, response.text)

# Call the register_webhook function
# register_webhook(my_webhook_url)

0 comments on commit 6523671

Please sign in to comment.